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
Execute the following command to apply `isort` and `black` formatting:
Execute the following command to apply linting and check typing:
```bash
hatch run lint

View File

@ -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(

View File

@ -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()

View File

@ -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/",
]

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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