diff --git a/README.md b/README.md index 446d94e1..19387561 100644 --- a/README.md +++ b/README.md @@ -202,7 +202,7 @@ hatch run test ### Format the code -Execute the following command to apply `isort` and `black` formatting: +Execute the following command to apply linting and check typing: ```bash hatch run lint diff --git a/examples/beanie/app/db.py b/examples/beanie/app/db.py index fbe716e6..14f50851 100644 --- a/examples/beanie/app/db.py +++ b/examples/beanie/app/db.py @@ -1,6 +1,7 @@ import motor.motor_asyncio from beanie import Document -from fastapi_users.db import BeanieBaseUser, BeanieUserDatabase +from fastapi_users.db import BeanieBaseUser +from fastapi_users_db_beanie import BeanieUserDatabase DATABASE_URL = "mongodb://localhost:27017" client = motor.motor_asyncio.AsyncIOMotorClient( diff --git a/fastapi_users/authentication/strategy/jwt.py b/fastapi_users/authentication/strategy/jwt.py index 2b28aa90..bf3fd903 100644 --- a/fastapi_users/authentication/strategy/jwt.py +++ b/fastapi_users/authentication/strategy/jwt.py @@ -11,6 +11,12 @@ from fastapi_users.jwt import SecretType, decode_jwt, generate_jwt from fastapi_users.manager import BaseUserManager +class JWTStrategyDestroyNotSupportedError(StrategyDestroyNotSupportedError): + def __init__(self) -> None: + message = "A JWT can't be invalidated: it's valid until it expires." + super().__init__(message) + + class JWTStrategy(Strategy[models.UP, models.ID], Generic[models.UP, models.ID]): def __init__( self, @@ -63,6 +69,4 @@ class JWTStrategy(Strategy[models.UP, models.ID], Generic[models.UP, models.ID]) ) async def destroy_token(self, token: str, user: models.UP) -> None: - raise StrategyDestroyNotSupportedError( - "A JWT can't be invalidated: it's valid until it expires." - ) + raise JWTStrategyDestroyNotSupportedError() diff --git a/pyproject.toml b/pyproject.toml index 8b105c97..3d31113f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -32,6 +32,10 @@ markers = [ ] [tool.ruff] +target-version = "py38" + +[tool.ruff.lint] +extend-select = ["UP", "TRY"] [tool.hatch] @@ -58,7 +62,6 @@ dependencies = [ "mkdocs", "mkdocs-material", "mkdocs-mermaid2-plugin", - "black", "mypy", "pytest-cov", "pytest-mock", @@ -78,7 +81,7 @@ lint = [ "isort ./fastapi_users ./tests", "isort ./docs/src -o fastapi_users", "isort ./examples -o fastapi_users -p app", - "black . ", + "ruff format .", "ruff --fix .", "mypy fastapi_users/", ] @@ -86,7 +89,7 @@ lint-check = [ "isort --check-only ./fastapi_users ./tests", "isort --check-only ./docs/src -o fastapi_users", "isort --check-only ./examples -o fastapi_users -p app", - "black --check .", + "ruff format .", "ruff .", "mypy fastapi_users/", ] diff --git a/tests/test_authentication_strategy_db.py b/tests/test_authentication_strategy_db.py index 201f8949..c81a7626 100644 --- a/tests/test_authentication_strategy_db.py +++ b/tests/test_authentication_strategy_db.py @@ -36,9 +36,10 @@ class AccessTokenDatabaseMock(AccessTokenDatabase[AccessTokenModel]): access_token = self.store[token] if max_age is not None and access_token.created_at < max_age: return None - return access_token except KeyError: return None + else: + return access_token async def create(self, create_dict: Dict[str, Any]) -> AccessTokenModel: access_token = AccessTokenModel(**create_dict) diff --git a/tests/test_authentication_strategy_jwt.py b/tests/test_authentication_strategy_jwt.py index 767619a5..6df77ca2 100644 --- a/tests/test_authentication_strategy_jwt.py +++ b/tests/test_authentication_strategy_jwt.py @@ -71,7 +71,7 @@ def jwt_strategy(request, secret: SecretType): return JWTStrategy( ECC_PRIVATE_KEY, LIFETIME, algorithm="ES256", public_key=ECC_PUBLIC_KEY ) - raise ValueError(f"Unrecognized algorithm: {request.param}") + raise ValueError(f"Unrecognized algorithm: {request.param}") # noqa: TRY003 @pytest.fixture diff --git a/tests/test_authentication_strategy_redis.py b/tests/test_authentication_strategy_redis.py index 7d178906..f7c48635 100644 --- a/tests/test_authentication_strategy_redis.py +++ b/tests/test_authentication_strategy_redis.py @@ -18,9 +18,10 @@ class RedisMock: value, expiration = self.store[key] if expiration is not None and expiration < datetime.now().timestamp(): return None - return value except KeyError: return None + else: + return value async def set(self, key: str, value: str, ex: Optional[int] = None): expiration = None diff --git a/tests/test_fastapi_users.py b/tests/test_fastapi_users.py index c6797dc5..5b12de6d 100644 --- a/tests/test_fastapi_users.py +++ b/tests/test_fastapi_users.py @@ -61,7 +61,7 @@ async def test_app_client( def current_superuser( user: UserModel = Depends( fastapi_users.current_user(active=True, superuser=True) - ) + ), ): return user