Skip to content

Commit 3729e96

Browse files
committed
Fixed all ruff errors
1 parent a4a20db commit 3729e96

32 files changed

+334
-206
lines changed

backend/app/auth/dependencies.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from app.users.models import User
1616

1717
reusable_oauth2 = OAuth2PasswordBearer(
18-
tokenUrl=f"{settings.API_V1_STR}/login/access-token"
18+
tokenUrl=f"{settings.API_V1_STR}/login/access-token",
1919
)
2020

2121

@@ -40,11 +40,13 @@ def get_current_user(session: SessionDep, token: TokenDep) -> User:
4040
user = session.get(User, token_data.sub)
4141
if not user:
4242
raise HTTPException(
43-
status_code=status.HTTP_404_NOT_FOUND, detail="User not found"
43+
status_code=status.HTTP_404_NOT_FOUND,
44+
detail="User not found",
4445
)
4546
if not user.is_active:
4647
raise HTTPException(
47-
status_code=status.HTTP_400_BAD_REQUEST, detail="Inactive user"
48+
status_code=status.HTTP_400_BAD_REQUEST,
49+
detail="Inactive user",
4850
)
4951
return user
5052

backend/app/auth/router.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from datetime import timedelta
2-
from typing import Annotated, Any
2+
from typing import Annotated
33

44
from fastapi import APIRouter, Depends, HTTPException, status
55
from fastapi.responses import HTMLResponse
@@ -20,31 +20,35 @@
2020

2121
@router.post("/login/access-token")
2222
def login_access_token(
23-
session: SessionDep, form_data: Annotated[OAuth2PasswordRequestForm, Depends()]
23+
session: SessionDep,
24+
form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
2425
) -> Token:
2526
"""
2627
OAuth2 compatible token login, get an access token for future requests
2728
"""
2829
user = auth_service.authenticate(
29-
session=session, email=form_data.username, password=form_data.password
30+
session=session,
31+
email=form_data.username,
32+
password=form_data.password,
3033
)
3134
if not user:
3235
raise HTTPException(
3336
status_code=status.HTTP_400_BAD_REQUEST,
3437
detail="Incorrect email or password",
3538
)
36-
elif not user.is_active:
39+
if not user.is_active:
3740
raise HTTPException(
38-
status_code=status.HTTP_400_BAD_REQUEST, detail="Inactive user"
41+
status_code=status.HTTP_400_BAD_REQUEST,
42+
detail="Inactive user",
3943
)
4044
access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
4145
return Token(
42-
access_token=create_access_token(user.id, expires_delta=access_token_expires)
46+
access_token=create_access_token(user.id, expires_delta=access_token_expires),
4347
)
4448

4549

4650
@router.post("/login/test-token", response_model=UserPublic)
47-
def test_token(current_user: CurrentUser) -> Any:
51+
def test_token(current_user: CurrentUser) -> CurrentUser:
4852
"""
4953
Test access token
5054
"""
@@ -63,15 +67,17 @@ def recover_password(email: str, session: SessionDep) -> Message:
6367
if user:
6468
password_reset_token = auth_service.generate_password_reset_token(email=email)
6569
email_data = email_service.generate_reset_password_email(
66-
email_to=user.email, email=email, token=password_reset_token
70+
email_to=user.email,
71+
email=email,
72+
token=password_reset_token,
6773
)
6874
email_service.send_email(
6975
email_to=user.email,
7076
subject=email_data.subject,
7177
html_content=email_data.html_content,
7278
)
7379
return Message(
74-
message="If that email is registered, we sent a password recovery link"
80+
message="If that email is registered, we sent a password recovery link",
7581
)
7682

7783

@@ -83,17 +89,20 @@ def reset_password(session: SessionDep, body: NewPassword) -> Message:
8389
email = auth_service.verify_password_reset_token(token=body.token)
8490
if not email:
8591
raise HTTPException(
86-
status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid token"
92+
status_code=status.HTTP_400_BAD_REQUEST,
93+
detail="Invalid token",
8794
)
8895
user = user_service.get_user_by_email(session=session, email=email)
8996
if not user:
9097
# Don't reveal that the user doesn't exist - use same error as invalid token
9198
raise HTTPException(
92-
status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid token"
99+
status_code=status.HTTP_400_BAD_REQUEST,
100+
detail="Invalid token",
93101
)
94-
elif not user.is_active:
102+
if not user.is_active:
95103
raise HTTPException(
96-
status_code=status.HTTP_400_BAD_REQUEST, detail="Inactive user"
104+
status_code=status.HTTP_400_BAD_REQUEST,
105+
detail="Inactive user",
97106
)
98107
user_in_update = UserUpdate(password=body.new_password)
99108
user_service.update_user(
@@ -109,7 +118,7 @@ def reset_password(session: SessionDep, body: NewPassword) -> Message:
109118
dependencies=[Depends(get_current_active_superuser)],
110119
response_class=HTMLResponse,
111120
)
112-
def recover_password_html_content(email: str, session: SessionDep) -> Any:
121+
def recover_password_html_content(email: str, session: SessionDep) -> HTMLResponse:
113122
"""
114123
HTML Content for Password Recovery
115124
"""
@@ -122,9 +131,12 @@ def recover_password_html_content(email: str, session: SessionDep) -> Any:
122131
)
123132
password_reset_token = auth_service.generate_password_reset_token(email=email)
124133
email_data = email_service.generate_reset_password_email(
125-
email_to=user.email, email=email, token=password_reset_token
134+
email_to=user.email,
135+
email=email,
136+
token=password_reset_token,
126137
)
127138

128139
return HTMLResponse(
129-
content=email_data.html_content, headers={"subject:": email_data.subject}
140+
content=email_data.html_content,
141+
headers={"subject:": email_data.subject},
130142
)

backend/app/auth/schemas.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
# JSON payload containing access token
55
class Token(SQLModel):
66
access_token: str
7-
token_type: str = "bearer"
7+
# S105 - This is not a hardcoded password.
8+
token_type: str = "bearer" # noqa: S105
89

910

1011
# Contents of JWT token

backend/app/auth/security.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from datetime import datetime, timedelta, timezone
2-
from typing import Any
2+
from uuid import UUID
33

44
import jwt
55
from pwdlib import PasswordHash
@@ -13,19 +13,19 @@
1313
(
1414
Argon2Hasher(),
1515
BcryptHasher(),
16-
)
16+
),
1717
)
1818

1919

20-
def create_access_token(subject: str | Any, expires_delta: timedelta) -> str:
20+
def create_access_token(subject: str | UUID, expires_delta: timedelta) -> str:
2121
expire = datetime.now(timezone.utc) + expires_delta
2222
to_encode = {"exp": expire, "sub": str(subject)}
23-
encoded_jwt = jwt.encode(to_encode, settings.SECRET_KEY, algorithm=ALGORITHM)
24-
return encoded_jwt
23+
return jwt.encode(to_encode, settings.SECRET_KEY, algorithm=ALGORITHM)
2524

2625

2726
def verify_password(
28-
plain_password: str, hashed_password: str
27+
plain_password: str,
28+
hashed_password: str,
2929
) -> tuple[bool, str | None]:
3030
return password_hash.verify_and_update(plain_password, hashed_password)
3131

backend/app/auth/service.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,11 @@ def generate_password_reset_token(email: str) -> str:
3434
now = datetime.now(timezone.utc)
3535
expires = now + delta
3636
exp = expires.timestamp()
37-
encoded_jwt = jwt.encode(
37+
return jwt.encode(
3838
{"exp": exp, "nbf": now, "sub": email},
3939
settings.SECRET_KEY,
4040
algorithm=ALGORITHM,
4141
)
42-
return encoded_jwt
4342

4443

4544
def verify_password_reset_token(token: str) -> str | None:

backend/app/backend_pre_start.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717
stop=stop_after_attempt(max_tries),
1818
wait=wait_fixed(wait_seconds),
1919
before=before_log(logger, logging.INFO),
20-
after=after_log(logger, logging.WARN),
20+
after=after_log(logger, logging.WARNING),
2121
)
2222
def init(db_engine: Engine) -> None:
2323
try:
2424
with Session(db_engine) as session:
2525
# Try to create session to check if DB is awake
2626
session.exec(select(1))
27-
except Exception as e:
28-
logger.error(e)
29-
raise e
27+
except Exception:
28+
logger.exception("")
29+
raise
3030

3131

3232
def main() -> None:

backend/app/config.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import secrets
22
import warnings
3-
from typing import Annotated, Any, Literal
3+
from typing import Annotated, Literal
44

55
from pydantic import (
66
AnyUrl,
@@ -15,10 +15,10 @@
1515
from typing_extensions import Self
1616

1717

18-
def parse_cors(v: Any) -> list[str] | str:
18+
def parse_cors(v: str | list[str] | None) -> list[str] | str:
1919
if isinstance(v, str) and not v.startswith("["):
2020
return [i.strip() for i in v.split(",") if i.strip()]
21-
elif isinstance(v, list | str):
21+
if isinstance(v, list | str):
2222
return v
2323
raise ValueError(v)
2424

@@ -38,14 +38,15 @@ class Settings(BaseSettings):
3838
ENVIRONMENT: Literal["local", "staging", "production"] = "local"
3939

4040
BACKEND_CORS_ORIGINS: Annotated[
41-
list[AnyUrl] | str, BeforeValidator(parse_cors)
41+
list[AnyUrl] | str,
42+
BeforeValidator(parse_cors),
4243
] = []
4344

4445
@computed_field # type: ignore[prop-decorator]
4546
@property
4647
def all_cors_origins(self) -> list[str]:
4748
return [str(origin).rstrip("/") for origin in self.BACKEND_CORS_ORIGINS] + [
48-
self.FRONTEND_HOST
49+
self.FRONTEND_HOST,
4950
]
5051

5152
PROJECT_NAME: str
@@ -58,7 +59,8 @@ def all_cors_origins(self) -> list[str]:
5859

5960
@computed_field # type: ignore[prop-decorator]
6061
@property
61-
def SQLALCHEMY_DATABASE_URI(self) -> PostgresDsn:
62+
# N802 - Error from original template.
63+
def SQLALCHEMY_DATABASE_URI(self) -> PostgresDsn: # noqa: N802
6264
return PostgresDsn.build(
6365
scheme="postgresql+psycopg",
6466
username=self.POSTGRES_USER,
@@ -110,10 +112,12 @@ def _enforce_non_default_secrets(self) -> Self:
110112
self._check_default_secret("SECRET_KEY", self.SECRET_KEY)
111113
self._check_default_secret("POSTGRES_PASSWORD", self.POSTGRES_PASSWORD)
112114
self._check_default_secret(
113-
"FIRST_SUPERUSER_PASSWORD", self.FIRST_SUPERUSER_PASSWORD
115+
"FIRST_SUPERUSER_PASSWORD",
116+
self.FIRST_SUPERUSER_PASSWORD,
114117
)
115118

116119
return self
117120

118121

119-
settings = Settings() # type: ignore
122+
# call-arg - Error from original template.
123+
settings = Settings() # type: ignore[call-arg]

backend/app/database.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ def init_db(session: Session) -> None:
2222
# Tables should be created with Alembic migrations
2323
# But if you don't want to use migrations, create
2424
# the tables un-commenting the next lines
25+
# # ERA001 - Error from original template.
2526
# from sqlmodel import SQLModel # noqa: ERA001
2627

2728
# This works because the models are already imported and registered from app.models
29+
# # ERA001 - Error from original template.
2830
# SQLModel.metadata.create_all(engine) # noqa: ERA001
2931
user = session.exec(
3032
select(User).where(User.email == settings.FIRST_SUPERUSER),

backend/app/items/models.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ class Item(ItemBase, table=True):
2222
id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True)
2323
created_at: datetime | None = Field(
2424
default_factory=get_datetime_utc,
25-
sa_type=DateTime(timezone=True), # type: ignore
25+
sa_type=DateTime(timezone=True), # type: ignore[call-overload]
2626
)
2727
owner_id: uuid.UUID = Field(
28-
foreign_key="user.id", nullable=False, ondelete="CASCADE"
28+
foreign_key="user.id",
29+
nullable=False,
30+
ondelete="CASCADE",
2931
)
3032
owner: User | None = Relationship(back_populates="items")

0 commit comments

Comments
 (0)