From 8e7d68d1b785223d0f982bb113c368d7ca9f20f3 Mon Sep 17 00:00:00 2001 From: Jakub Frejlach Date: Thu, 19 Dec 2024 18:03:26 +0100 Subject: [PATCH] Improve code formatting and linting * switch out black,isort and flake8 for ruff * update tox * reformat/lint/sort imports to comply with ruff settings --- .github/workflows/build.yml | 2 +- .github/workflows/lint.yml | 21 +++------- devel-requirements.in | 1 + devel-requirements.txt | 81 ++++++++++++++++++++----------------- osidb_bindings/__init__.py | 5 ++- osidb_bindings/constants.py | 1 - osidb_bindings/helpers.py | 1 + osidb_bindings/iterators.py | 3 -- osidb_bindings/session.py | 23 ++++------- osidb_bindings/utils.py | 1 - tests/test_core.py | 3 +- tox.ini | 36 +++-------------- 12 files changed, 70 insertions(+), 108 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 85846b4..74591b7 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -11,7 +11,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: - python-version: 3.9 + python-version: 3.12 - name: Install dependencies run: python3 -m pip install --upgrade pip build - name: Build python package diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 22b72a4..747a6b4 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -1,25 +1,14 @@ name: Lint on: [push] jobs: - black: + ruff: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: - python-version: 3.9 + python-version: 3.12 - name: Install dependencies - run: pip install tox==3.24.5 - - name: Check that code is formatted with black - run: tox -e black - isort: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 - with: - python-version: 3.9 - - name: Install dependencies - run: pip install tox==3.24.5 - - name: Check that imports are formatted with isort - run: tox -e isort + run: pip install tox==4.23.2 + - name: Check that code is formatted and linted with ruff + run: tox -e ruff diff --git a/devel-requirements.in b/devel-requirements.in index 51a0ce9..06c39d8 100644 --- a/devel-requirements.in +++ b/devel-requirements.in @@ -3,4 +3,5 @@ openapi-python-client pip-tools pytest +ruff tox diff --git a/devel-requirements.txt b/devel-requirements.txt index 2c141f9..4c1361b 100644 --- a/devel-requirements.txt +++ b/devel-requirements.txt @@ -16,27 +16,31 @@ attrs==24.2.0 \ --hash=sha256:5cfb1b9148b5b086569baec03f20d7b6bf3bcacc9a42bebf87ffaaca362f6346 \ --hash=sha256:81921eb96de3191c8258c199618104dd27ac608d9366f5e35d011eae1867ede2 # via - # -c requirements.txt + # -c /home/jfrejlac/work/osidb-bindings/requirements.txt # openapi-python-client -backports-entry-points-selectable==1.1.1 \ - --hash=sha256:7fceed9532a7aa2bd888654a7314f864a3c16a4e710b34a58cfc0f08114c663b \ - --hash=sha256:914b21a479fde881635f7af5adc7f6e38d6b274be32269070c53b698c60d5386 - # via virtualenv build==1.2.2.post1 \ --hash=sha256:1d61c0887fa860c01971625baae8bdd338e517b836a2f70dd1f7aa3a6b2fc5b5 \ --hash=sha256:b36993e92ca9375a219c99e606a122ff365a760a2d4bba0caa09bd5278b608b7 # via pip-tools +cachetools==5.5.0 \ + --hash=sha256:02134e8439cdc2ffb62023ce1debca2944c3f289d66bb17ead3ab3dede74b292 \ + --hash=sha256:2cc24fb4cbe39633fb7badd9db9ca6295d766d9c2995f245725a46715d050f2a + # via tox certifi==2024.8.30 \ --hash=sha256:922820b53db7a7257ffbda3f597266d435245903d80737e34f8a45ff3e3230d8 \ --hash=sha256:bec941d2aa8195e248a60b31ff9f0558284cf01a52591ceda73ea9afffd69fd9 # via - # -c requirements.txt + # -c /home/jfrejlac/work/osidb-bindings/requirements.txt # httpx +chardet==5.2.0 \ + --hash=sha256:1b3b6ff479a8c414bc3fa2c0852995695c4a026dcd6d0633b2dd092ca39c1cf7 \ + --hash=sha256:e1cf59446890a00105fe7b7912492ea04b6e6f06d4b742b2c788469e34c82970 + # via tox charset-normalizer==2.0.7 \ --hash=sha256:e019de665e2bcf9c2b64e2e5aa025fa991da8720daa3c1138cadd2fd1856aed0 \ --hash=sha256:f7af805c321bfa1ce6714c51f254e0d5bb5e5834039bc17db7ebe3a4cec9492b # via - # -c requirements.txt + # -c /home/jfrejlac/work/osidb-bindings/requirements.txt # httpx click==8.1.7 \ --hash=sha256:ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28 \ @@ -44,13 +48,17 @@ click==8.1.7 \ # via # pip-tools # typer -distlib==0.3.3 \ - --hash=sha256:c8b54e8454e5bf6237cc84c20e8264c3e991e824ef27e8f1e81049867d861e31 \ - --hash=sha256:d982d0751ff6eaaab5e2ec8e691d949ee80eddf01a62eaa96ddb11531fe16b05 +colorama==0.4.6 \ + --hash=sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44 \ + --hash=sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6 + # via tox +distlib==0.3.9 \ + --hash=sha256:47f8c22fd27c27e25a65601af709b38e4f0a45ea4fc2e710f65755fa8caaaf87 \ + --hash=sha256:a60f20dea646b8a33f3e7772f74dc0b2d0772d2837ee1342a00645c81edf9403 # via virtualenv -filelock==3.4.0 \ - --hash=sha256:2e139a228bcf56dd8b2274a65174d005c4a6b68540ee0bdbb92c76f43f29f7e8 \ - --hash=sha256:93d512b32a23baf4cac44ffd72ccf70732aeff7b8050fcaf6d3ec406d954baf4 +filelock==3.16.1 \ + --hash=sha256:2082e5703d51fbf98ea75855d9d5527e33d8ff23099bec374a134febee6946b0 \ + --hash=sha256:c249fbfcd5db47e5e2d6d62198e565475ee65e4831e2561c8e313fa7eb961435 # via # tox # virtualenv @@ -70,7 +78,7 @@ idna==3.10 \ --hash=sha256:12f65c9b470abda6dc35cf8e63cc574b1c52b11df2c86030af0ac09b01b13ea9 \ --hash=sha256:946d195a0d259cbba61165e88e65941f16e9b36ea6ddb97f00452bae8b1287d3 # via - # -c requirements.txt + # -c /home/jfrejlac/work/osidb-bindings/requirements.txt # anyio # rfc3986 iniconfig==1.1.1 \ @@ -169,26 +177,25 @@ packaging==24.2 \ --hash=sha256:c228a6dc5e932d346bc5739379109d49e8853dd8223571c7c5b55260edc0b97f # via # build + # pyproject-api # pytest # tox pip-tools==7.4.1 \ --hash=sha256:4c690e5fbae2f21e87843e89c26191f0d9454f362d8acdbd695716493ec8b3a9 \ --hash=sha256:864826f5073864450e24dbeeb85ce3920cdfb09848a3d69ebf537b521f14bcc9 # via -r devel-requirements.in -platformdirs==2.4.0 \ - --hash=sha256:367a5e80b3d04d2428ffa76d33f124cf11e8fff2acdaa9b43d545f5c7d661ef2 \ - --hash=sha256:8868bbe3c3c80d42f20156f22e7131d2fb321f5bc86a2a345375c6481a67021d - # via virtualenv +platformdirs==4.3.6 \ + --hash=sha256:357fb2acbc885b0419afd3ce3ed34564c13c9b95c89360cd9563f73aa5e2b907 \ + --hash=sha256:73e575e1408ab8103900836b97580d5307456908a03e92031bab39e4554cc3fb + # via + # tox + # virtualenv pluggy==1.5.0 \ --hash=sha256:2cffa88e94fdc978c4c574f15f9e59b7f4201d439195c3715ca9e2486f1d0cf1 \ --hash=sha256:44e1ad92c8ca002de6377e165f3e0f1be63266ab4d554740532335b9d75ea669 # via # pytest # tox -py==1.11.0 \ - --hash=sha256:51c75c4126074b472f746a24399ad32f6053d1b34b68d2fa41e558e6f4a98719 \ - --hash=sha256:607c53218732647dff4acdfcd50cb62615cedf612e72d1724fb1a0cc6405b378 - # via tox pydantic==2.10.3 \ --hash=sha256:be04d85bbc7b65651c5f8e6b9976ed9c6f41782a55524cef079a34a0bb82144d \ --hash=sha256:cb5ac360ce894ceacd69c403187900a02c4b20b693a9dd1d643e1effab9eadf9 @@ -299,6 +306,10 @@ pygments==2.18.0 \ --hash=sha256:786ff802f32e91311bff3889f6e9a86e81505fe99f2735bb6d60ae0c5004f199 \ --hash=sha256:b8e6aca0523f3ab76fee51799c488e38782ac06eafcf95e7ba832985c8e7b13a # via rich +pyproject-api==1.8.0 \ + --hash=sha256:3d7d347a047afe796fd5d1885b1e391ba29be7169bd2f102fcd378f04273d228 \ + --hash=sha256:77b8049f2feb5d33eefcc21b57f1e279636277a8ac8ad6b5871037b243778496 + # via tox pyproject-hooks==1.2.0 \ --hash=sha256:1e859bd5c40fae9448642dd871adf459e5e2084186e8d2c2a79a824c970da1f8 \ --hash=sha256:9e5c6bfa8dcc30091c74b0cf803c81fdd29d94f01992a7707bc97babb1141913 @@ -313,7 +324,7 @@ python-dateutil==2.8.2 \ --hash=sha256:0123cacc1627ae19ddf3c27a5de5bd67ee4586fbdd6440d9748f8abb483d3e86 \ --hash=sha256:961d03dc3453ebbc59dbdea9e4e11c5651520a876d0f4db161e8674aae935da9 # via - # -c requirements.txt + # -c /home/jfrejlac/work/osidb-bindings/requirements.txt # openapi-python-client rfc3986[idna2008]==1.5.0 \ --hash=sha256:270aaf10d87d0d4e095063c65bf3ddbc6ee3d0b226328ce21e036f946e421835 \ @@ -394,7 +405,9 @@ ruff==0.8.2 \ --hash=sha256:ec016beb69ac16be416c435828be702ee694c0d722505f9c1f35e1b9c0cc1bf5 \ --hash=sha256:f05cdf8d050b30e2ba55c9b09330b51f9f97d36d4673213679b965d25a785f3c \ --hash=sha256:fb88e2a506b70cfbc2de6fae6681c4f944f7dd5f2fe87233a7233d888bad73e8 - # via openapi-python-client + # via + # -r devel-requirements.in + # openapi-python-client shellingham==1.4.0 \ --hash=sha256:4855c2458d6904829bd34c299f11fdeed7cfefbf8a2c522e4caea6cd76b3171e \ --hash=sha256:536b67a0697f2e4af32ab176c00a50ac2899c5a05e0d8e2dadac8e58888283f9 @@ -405,10 +418,8 @@ six==1.16.0 \ --hash=sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926 \ --hash=sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254 # via - # -c requirements.txt + # -c /home/jfrejlac/work/osidb-bindings/requirements.txt # python-dateutil - # tox - # virtualenv sniffio==1.2.0 \ --hash=sha256:471b71698eac1c2112a40ce2752bb2f4a4814c22a54a3eed3676bc0f5ca9f663 \ --hash=sha256:c4666eecec1d3f50960c6bdf61ab7bc350648da6c126e3cf6898d8cd4ddcd3de @@ -416,13 +427,9 @@ sniffio==1.2.0 \ # anyio # httpcore # httpx -toml==0.10.2 \ - --hash=sha256:806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b \ - --hash=sha256:b3bda1d108d5dd99f4a20d24d9c348e91c4db7ab1b749200bded2f839ccbe68f - # via tox -tox==3.24.4 \ - --hash=sha256:5e274227a53dc9ef856767c21867377ba395992549f02ce55eb549f9fb9a8d10 \ - --hash=sha256:c30b57fa2477f1fb7c36aa1d83292d5c2336cd0018119e1b1c17340e2c2708ca +tox==4.23.2 \ + --hash=sha256:452bc32bb031f2282881a2118923176445bac783ab97c874b8770ab4c3b76c38 \ + --hash=sha256:86075e00e555df6e82e74cfc333917f91ecb47ffbc868dcafbd2672e332f4a2c # via -r devel-requirements.in typer==0.13.1 \ --hash=sha256:5b59580fd925e89463a29d363e0a43245ec02765bde9fb77d39e5d0f29dd7157 \ @@ -436,9 +443,9 @@ typing-extensions==4.12.2 \ # pydantic # pydantic-core # typer -virtualenv==20.10.0 \ - --hash=sha256:4b02e52a624336eece99c96e3ab7111f469c24ba226a53ec474e8e787b365814 \ - --hash=sha256:576d05b46eace16a9c348085f7d0dc8ef28713a2cabaa1cf0aea41e8f12c9218 +virtualenv==20.28.0 \ + --hash=sha256:23eae1b4516ecd610481eda647f3a7c09aea295055337331bb4e6892ecce47b0 \ + --hash=sha256:2c9c3262bb8e7b87ea801d715fae4495e6032450c71d2309be9550e7364049aa # via tox wheel==0.45.1 \ --hash=sha256:661e1abd9198507b1409a20c02106d9670b2576e916d58f520316666abca6729 \ diff --git a/osidb_bindings/__init__.py b/osidb_bindings/__init__.py index 08cd613..0c862ca 100644 --- a/osidb_bindings/__init__.py +++ b/osidb_bindings/__init__.py @@ -1,2 +1,3 @@ -from .session import Paginator, new_session -from .utils import cve_ids +from .session import Paginator as Paginator +from .session import new_session as new_session +from .utils import cve_ids as cve_ids diff --git a/osidb_bindings/constants.py b/osidb_bindings/constants.py index 098d283..7af47e2 100644 --- a/osidb_bindings/constants.py +++ b/osidb_bindings/constants.py @@ -2,7 +2,6 @@ osidb-bindings constants """ - from typing import List OSIDB_API_VERSION: str = "v1" diff --git a/osidb_bindings/helpers.py b/osidb_bindings/helpers.py index 23b4a1e..f0d8ef8 100644 --- a/osidb_bindings/helpers.py +++ b/osidb_bindings/helpers.py @@ -1,6 +1,7 @@ """ osidb-registry-bindings helpers """ + import json from os import getenv from typing import Any, Union diff --git a/osidb_bindings/iterators.py b/osidb_bindings/iterators.py index 23413aa..3046c5c 100644 --- a/osidb_bindings/iterators.py +++ b/osidb_bindings/iterators.py @@ -29,7 +29,6 @@ def __init__( init_response=None, **kwargs, ): - if not init_response and not retrieve_list_fn: raise OSIDBBindingsException( ( @@ -59,7 +58,6 @@ def __iter__(self): def __next__(self): if self.current_response is None: - # no current response page - obtain the first page response = self.retrieve_list_fn( *self.args, @@ -73,7 +71,6 @@ def __next__(self): self.current_response = response return response else: - # existing current response - call next page response = self.current_response.next() if response is not None: diff --git a/osidb_bindings/session.py b/osidb_bindings/session.py index 37ea369..4621216 100644 --- a/osidb_bindings/session.py +++ b/osidb_bindings/session.py @@ -18,7 +18,6 @@ auth_token_refresh_create, auth_token_retrieve, ) -from .bindings.python_client.types import UNSET from .constants import ( ALL_SESSION_OPERATIONS, DEFAULT_LIMIT, @@ -55,8 +54,8 @@ def file_trackers(self, form_data: Dict[str, Any], *args, **kwargs): model = getattr(method_module, "REQUEST_BODY_TYPE", None) if model is None: raise UndefinedRequestBody( - f'The request body for the resource "trackers" ' - f'and the operation "file" is not defined.' + 'The request body for the resource "trackers" ' + 'and the operation "file" is not defined.' ) transformed_data = serialize_data(form_data, model) @@ -92,8 +91,8 @@ def reject_flaw(self, id: str, form_data: Dict[str, Any], *args, **kwargs): model = getattr(method_module, "REQUEST_BODY_TYPE", None) if model is None: raise UndefinedRequestBody( - f'The request body for the resource "flaw" ' - f'and the operation "reject" is not defined.' + 'The request body for the resource "flaw" ' + 'and the operation "reject" is not defined.' ) transformed_data = serialize_data(form_data, model) return sync_fn( @@ -168,11 +167,9 @@ def new_session( """Create a new session for selected OSIDB URI""" if username and password: - # OSIDB instances with the username/password auth for token acquirement auth = (username, password) else: - # OSIDB instances with the kerberos auth for token acquirement auth = "kerberos" @@ -185,7 +182,6 @@ class Session: """Simple session wrapper which encapsulates the client""" def __init__(self, base_url, auth=None, verify_ssl=True): - # Store auth for the refresh token acquirement self.auth = auth @@ -316,19 +312,14 @@ def __get_access_token(self) -> str: try: response = auth_token_refresh_create.sync( client=self.__client, - body=models.TokenRefresh.from_dict( - {"refresh": self.refresh_token} - ), + body=models.TokenRefresh.from_dict({"refresh": self.refresh_token}), ) except requests.HTTPError: - # expired refresh token, renew it and try again self.refresh_token = self.__get_refresh_token() response = auth_token_refresh_create.sync( client=self.__client, - body=models.TokenRefresh.from_dict( - {"refresh": self.refresh_token} - ), + body=models.TokenRefresh.from_dict({"refresh": self.refresh_token}), ) return response.access @@ -464,6 +455,7 @@ def bulk_create(self, form_data: Dict[str, Any], *args, **kwargs): return sync_fn( *args, client=self.client(), + body=serialized_data, **kwargs, ) else: @@ -504,6 +496,7 @@ def bulk_update(self, form_data: Dict[str, Any], *args, **kwargs): return sync_fn( *args, client=self.client(), + body=serialized_data, **kwargs, ) else: diff --git a/osidb_bindings/utils.py b/osidb_bindings/utils.py index 6eee1d3..488b876 100644 --- a/osidb_bindings/utils.py +++ b/osidb_bindings/utils.py @@ -2,7 +2,6 @@ osidb-bindings user utilities """ - from typing import List from .session import Session diff --git a/tests/test_core.py b/tests/test_core.py index 915cd03..7a3e120 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1,6 +1,7 @@ -import pytest import logging +import pytest + logger = logging.getLogger(__name__) pytestmark = pytest.mark.unit diff --git a/tox.ini b/tox.ini index b1c2c32..dff03b9 100644 --- a/tox.ini +++ b/tox.ini @@ -10,35 +10,9 @@ deps = -r requirements.txt commands = pytest -m "unit" {posargs} -[testenv:record-new] -passenv = * -basepython = python3.9 -deps = -r requirements.txt - -r devel-requirements.txt +[testenv:ruff] +deps = + ruff==0.8.2 commands = - pytest --record-mode=once {posargs} - -[testenv:record-rewrite] -passenv = * -basepython = python3.9 -deps = -r requirements.txt - -r devel-requirements.txt -commands = - pytest --record-mode=rewrite {posargs} - -[testenv:black] -deps = black==21.11b1 - click==7.1.2 -commands = black --check . - -[testenv:isort] -profile = black -skip = venv,.tox - -[flake8] -ignore = W504,W503,E203,E501,F401,F403,F405,F841 - -[testenv:flake8] -deps = flake8 -commands = flake8 - + ruff check . --fix --extend-select I --config ruff.toml + ruff format .