Upgrade linting configuration

This commit is contained in:
François Voron
2024-02-05 09:20:29 +01:00
parent cd5eaf1d51
commit 9d07464be1
8 changed files with 22 additions and 12 deletions

View File

@ -202,7 +202,7 @@ hatch run test
### Format the code ### Format the code
Execute the following command to apply `isort` and `black` formatting: Execute the following command to apply linting and check typing:
```bash ```bash
hatch run lint hatch run lint

View File

@ -1,6 +1,7 @@
import motor.motor_asyncio import motor.motor_asyncio
from beanie import Document 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" DATABASE_URL = "mongodb://localhost:27017"
client = motor.motor_asyncio.AsyncIOMotorClient( client = motor.motor_asyncio.AsyncIOMotorClient(

View File

@ -11,6 +11,12 @@ from fastapi_users.jwt import SecretType, decode_jwt, generate_jwt
from fastapi_users.manager import BaseUserManager 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]): class JWTStrategy(Strategy[models.UP, models.ID], Generic[models.UP, models.ID]):
def __init__( def __init__(
self, 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: async def destroy_token(self, token: str, user: models.UP) -> None:
raise StrategyDestroyNotSupportedError( raise JWTStrategyDestroyNotSupportedError()
"A JWT can't be invalidated: it's valid until it expires."
)

View File

@ -32,6 +32,10 @@ markers = [
] ]
[tool.ruff] [tool.ruff]
target-version = "py38"
[tool.ruff.lint]
extend-select = ["UP", "TRY"]
[tool.hatch] [tool.hatch]
@ -58,7 +62,6 @@ dependencies = [
"mkdocs", "mkdocs",
"mkdocs-material", "mkdocs-material",
"mkdocs-mermaid2-plugin", "mkdocs-mermaid2-plugin",
"black",
"mypy", "mypy",
"pytest-cov", "pytest-cov",
"pytest-mock", "pytest-mock",
@ -78,7 +81,7 @@ lint = [
"isort ./fastapi_users ./tests", "isort ./fastapi_users ./tests",
"isort ./docs/src -o fastapi_users", "isort ./docs/src -o fastapi_users",
"isort ./examples -o fastapi_users -p app", "isort ./examples -o fastapi_users -p app",
"black . ", "ruff format .",
"ruff --fix .", "ruff --fix .",
"mypy fastapi_users/", "mypy fastapi_users/",
] ]
@ -86,7 +89,7 @@ lint-check = [
"isort --check-only ./fastapi_users ./tests", "isort --check-only ./fastapi_users ./tests",
"isort --check-only ./docs/src -o fastapi_users", "isort --check-only ./docs/src -o fastapi_users",
"isort --check-only ./examples -o fastapi_users -p app", "isort --check-only ./examples -o fastapi_users -p app",
"black --check .", "ruff format .",
"ruff .", "ruff .",
"mypy fastapi_users/", "mypy fastapi_users/",
] ]

View File

@ -36,9 +36,10 @@ class AccessTokenDatabaseMock(AccessTokenDatabase[AccessTokenModel]):
access_token = self.store[token] access_token = self.store[token]
if max_age is not None and access_token.created_at < max_age: if max_age is not None and access_token.created_at < max_age:
return None return None
return access_token
except KeyError: except KeyError:
return None return None
else:
return access_token
async def create(self, create_dict: Dict[str, Any]) -> AccessTokenModel: async def create(self, create_dict: Dict[str, Any]) -> AccessTokenModel:
access_token = AccessTokenModel(**create_dict) access_token = AccessTokenModel(**create_dict)

View File

@ -71,7 +71,7 @@ def jwt_strategy(request, secret: SecretType):
return JWTStrategy( return JWTStrategy(
ECC_PRIVATE_KEY, LIFETIME, algorithm="ES256", public_key=ECC_PUBLIC_KEY 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 @pytest.fixture

View File

@ -18,9 +18,10 @@ class RedisMock:
value, expiration = self.store[key] value, expiration = self.store[key]
if expiration is not None and expiration < datetime.now().timestamp(): if expiration is not None and expiration < datetime.now().timestamp():
return None return None
return value
except KeyError: except KeyError:
return None return None
else:
return value
async def set(self, key: str, value: str, ex: Optional[int] = None): async def set(self, key: str, value: str, ex: Optional[int] = None):
expiration = None expiration = None

View File

@ -61,7 +61,7 @@ async def test_app_client(
def current_superuser( def current_superuser(
user: UserModel = Depends( user: UserModel = Depends(
fastapi_users.current_user(active=True, superuser=True) fastapi_users.current_user(active=True, superuser=True)
) ),
): ):
return user return user