diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index abc2571..d1ddbb9 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,34 +5,49 @@ repos: - id: check-added-large-files - repo: local hooks: + - id: flake8 + name: flake8 + entry: flake8 missil sample tests + language: system + always_run: true + pass_filenames: false + - id: pyupgrade + name: pyupgrade + entry: pyupgrade missil/__init__.py missil/bearers.py missil/exceptions.py missil/jwt_utilities.py --py310-plus --py311-plus --py312-plus + language: system + always_run: true + pass_filenames: false - id: black name: black - entry: black missil tests + entry: black missil sample tests + language: system + always_run: true + pass_filenames: false + - id: refurb + name: refurb + entry: refurb missil sample tests language: system always_run: true pass_filenames: false - id: isort name: isort - entry: isort missil tests + entry: isort missil sample tests language: system always_run: true pass_filenames: false - id: mypy name: mypy - entry: mypy missil - language: system - always_run: true - pass_filenames: false - - id: flake8 - name: flake8 - entry: flake8 missil tests + entry: mypy missil sample language: system always_run: true pass_filenames: false - id: pydocstyle name: pydocstyle - entry: pydocstyle missil + entry: pydocstyle missil sample language: system always_run: true pass_filenames: false + - id: test + name: test + entry: pytest --cov=missil tests/ \ No newline at end of file diff --git a/missil/__init__.py b/missil/__init__.py index aa53278..258d799 100644 --- a/missil/__init__.py +++ b/missil/__init__.py @@ -1,9 +1,9 @@ """Simple FastAPI declarative endpoint-level access control.""" +from collections.abc import Callable from typing import TYPE_CHECKING from typing import Annotated from typing import Any -from typing import Callable from fastapi import Depends as FastAPIDependsFunc from fastapi import status diff --git a/pdm.lock b/pdm.lock index 9fe5717..d852c7d 100644 --- a/pdm.lock +++ b/pdm.lock @@ -5,7 +5,7 @@ groups = ["default", "python-tools", "dev", "docs", "testing"] strategy = ["cross_platform"] lock_version = "4.4.1" -content_hash = "sha256:1e8edf03539d5c60ce983c27e84bb929fe6708a51fa3b99545e22e079b06468f" +content_hash = "sha256:f04be5883de2274eb22919c6369d483810554d38ff4c0ef84df85efcfcbafad1" [[package]] name = "annotated-types" diff --git a/pyproject.toml b/pyproject.toml index de46833..2505d88 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -115,6 +115,7 @@ testing = [ dev = [ "uvicorn>=0.26.0", "types-python-jose>=3.3.4.20240106", + "pre-commit>=3.6.0", ] docs = [ "griffe-typingdoc>=0.2.4", @@ -132,21 +133,27 @@ autoflake = { cmd = [ "--remove-unused-variables", "--in-place", "missil", + "sample", "tests", ] } -flake8 = { cmd = ["flake8", "missil", "tests"] } -black = { cmd = ["black", "missil", "tests"] } -isort = { cmd = ["isort", "missil", "tests"] } -mypy = { cmd = ["mypy", "missil"] } -refurb = { cmd = ["refurb", "missil", "tests"] } +flake8 = { cmd = ["flake8", "missil", "sample", "tests"] } +black = { cmd = ["black", "missil", "sample", "tests"] } +isort = { cmd = ["isort", "missil", "sample", "tests"] } +mypy = { cmd = ["mypy", "missil", "sample"] } +refurb = { cmd = ["refurb", "missil", "sample", "tests"] } pyupgrade = { cmd = [ "pyupgrade", + "missil/__init__.py", + "missil/bearers.py", + "missil/exceptions.py", + "missil/jwt_utilities.py", + "sample/main.py", "--py310-plus", "--py311-plus", "--py312-plus" ] } -pydocstyle = { cmd = ["pydocstyle", "missil"] } +pydocstyle = { cmd = ["pydocstyle", "missil", "sample"] } improve = { composite = ["flake8", "pyupgrade", "refurb"] } format = { composite = ["autoflake", "black", "isort"] } test = { cmd = ["pytest", "--cov=missil", "tests/"] } diff --git a/sample/main.py b/sample/main.py index 7990bf0..122396c 100644 --- a/sample/main.py +++ b/sample/main.py @@ -1,14 +1,14 @@ """Missil sample usage.""" -import missil +from datetime import datetime +from datetime import timedelta +from datetime import timezone from fastapi import FastAPI from fastapi import Response -from typing import Union -from datetime import datetime -from datetime import timezone -from datetime import timedelta +import missil + app = FastAPI() @@ -22,14 +22,14 @@ @app.get("/") -def read_root(): +def read_root() -> dict[str, str]: + """Just a humble and happy endpoint.""" return {"Hello": "World"} @app.get("/set-cookies", status_code=200) def set_cookies(response: Response) -> dict[str, str]: """Just for example purposes.""" - claims = { "username": "JohnDoe", # the key 'userPermissions' name must be the same as in the @@ -56,11 +56,13 @@ def set_cookies(response: Response) -> dict[str, str]: @app.get("/finances/read", dependencies=[rules["finances:read"]]) def finances_read() -> dict[str, str]: + """Requires read permission on finances.""" return {"msg": "you have permission to perform read actions on finances!"} @app.get("/finances/write", dependencies=[rules["finances:write"]]) def finances_write() -> dict[str, str]: + """Requires write permission on finances.""" return {"msg": "you have permission to perform write actions on finances!"} diff --git a/tests/test_jwt_utilities.py b/tests/test_jwt_utilities.py index f9aec21..e6ff759 100644 --- a/tests/test_jwt_utilities.py +++ b/tests/test_jwt_utilities.py @@ -150,7 +150,5 @@ def test_decode_jwt_expired_token(secret_key, encoded_expired_jwt_token): @ignore_warnings def test_decode_jwt_invalid_token(secret_key, encoded_invalid_jwt_token): - with pytest.raises( - TokenErrorException, match="The token signature is invalid." - ) as e: + with pytest.raises(TokenErrorException, match="The token signature is invalid."): jwt_utilities.decode_jwt_token(encoded_invalid_jwt_token, secret_key) diff --git a/tests/utils.py b/tests/utils.py index c4fc23f..955f9ba 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -6,7 +6,7 @@ def ignore_warnings(f): @wraps(f) def inner(*args, **kwargs): - with warnings.catch_warnings(record=True) as w: + with warnings.catch_warnings(record=True) as _: warnings.simplefilter("ignore") response = f(*args, **kwargs) return response