From eb8acd7b7cc4ed5b7f3c7ddfadcb25403f32a245 Mon Sep 17 00:00:00 2001 From: grillazz Date: Mon, 13 Jul 2026 14:25:49 +0200 Subject: [PATCH] refactor: add type hints to function signatures across multiple files --- app/main.py | 3 ++- app/models/stuff.py | 2 +- app/models/user.py | 6 +++--- app/server.py | 4 ++-- app/services/auth.py | 2 +- app/services/llm.py | 2 +- app/services/smtp.py | 6 +++--- app/utils/decorators.py | 2 +- performance/locustfile.py | 4 ++-- pyproject.toml | 11 +++++++++++ 10 files changed, 27 insertions(+), 15 deletions(-) diff --git a/app/main.py b/app/main.py index 5a3f50a..a484155 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,4 @@ +from starlette.templating import _TemplateResponse from contextlib import asynccontextmanager from pathlib import Path @@ -79,7 +80,7 @@ def create_app() -> FastAPI: register_exception_handlers(app) @app.get("/index", response_class=HTMLResponse) - def get_index(request: Request): + def get_index(request: Request) -> _TemplateResponse: return templates.TemplateResponse("index.html", {"request": request}) return app diff --git a/app/models/stuff.py b/app/models/stuff.py index 0954863..21332f8 100644 --- a/app/models/stuff.py +++ b/app/models/stuff.py @@ -35,7 +35,7 @@ class Stuff(Base): @classmethod @compile_sql_or_scalar - async def get_by_name(cls, db_session: AsyncSession, name: str, compile_sql=False): + async def get_by_name(cls, db_session: AsyncSession, name: str, compile_sql: bool=False): stmt = select(cls).options(joinedload(cls.nonsense)).where(cls.name == name) return stmt diff --git a/app/models/user.py b/app/models/user.py index 005c71d..5e6ce42 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -21,15 +21,15 @@ class User(Base): _password: bytes = Column(LargeBinary, nullable=False) @property - def password(self): + def password(self) -> str: return self._password.decode("utf-8") @password.setter - def password(self, password: SecretStr): + def password(self, password: SecretStr) -> None: _password_string = password.get_secret_value().encode("utf-8") self._password = bcrypt.hashpw(_password_string, bcrypt.gensalt()) - def check_password(self, password: SecretStr): + def check_password(self, password: SecretStr) -> bool: return bcrypt.checkpw( password.get_secret_value().encode("utf-8"), self._password ) diff --git a/app/server.py b/app/server.py index 744463d..eb30527 100644 --- a/app/server.py +++ b/app/server.py @@ -1,10 +1,10 @@ from granian import Granian -def startup(): +def startup() -> None: print("Server starting up...") -def shutdown(): +def shutdown() -> None: print("Server shutting down...") server = Granian( diff --git a/app/services/auth.py b/app/services/auth.py index 144384e..0a4f973 100644 --- a/app/services/auth.py +++ b/app/services/auth.py @@ -25,7 +25,7 @@ async def verify_jwt(request: Request, token: str) -> bool: class AuthBearer(HTTPBearer): - def __init__(self, auto_error: bool = True): + def __init__(self, auto_error: bool = True) -> None: super().__init__(auto_error=auto_error) async def __call__(self, request: Request): diff --git a/app/services/llm.py b/app/services/llm.py index b3b899a..ceaa455 100644 --- a/app/services/llm.py +++ b/app/services/llm.py @@ -5,7 +5,7 @@ import orjson class StreamLLMService: - def __init__(self, base_url: str = "http://localhost:11434/v1"): + def __init__(self, base_url: str = "http://localhost:11434/v1") -> None: self.base_url = base_url self.model = "llama3.2" diff --git a/app/services/smtp.py b/app/services/smtp.py index bb5ea24..3d09932 100644 --- a/app/services/smtp.py +++ b/app/services/smtp.py @@ -45,7 +45,7 @@ class SMTPEmailService(metaclass=SingletonMetaNoArgs): ) server: smtplib.SMTP = field(init=False) # Deferred initialization in post-init - def __attrs_post_init__(self): + def __attrs_post_init__(self) -> None: """ Initializes the SMTP server connection after the object is created. @@ -98,7 +98,7 @@ class SMTPEmailService(metaclass=SingletonMetaNoArgs): subject: str, body_text: str = "", body_html: str = None, - ): + ) -> None: """ Sends an email to the specified recipients. @@ -130,7 +130,7 @@ class SMTPEmailService(metaclass=SingletonMetaNoArgs): template: str, context: dict, sender: EmailStr, - ): + ) -> None: """ Sends an email using a Jinja2 template. diff --git a/app/utils/decorators.py b/app/utils/decorators.py index 2b71919..cccde01 100644 --- a/app/utils/decorators.py +++ b/app/utils/decorators.py @@ -15,7 +15,7 @@ def compile_sql_or_scalar(func): """ @wraps(func) - async def wrapper(cls, db_session, name, compile_sql=False, *args, **kwargs): + async def wrapper(cls, db_session, name, compile_sql: bool=False, *args, **kwargs): """ Wrapper function that either compiles the SQL statement or executes it. diff --git a/performance/locustfile.py b/performance/locustfile.py index a9a1bd3..c1f5122 100644 --- a/performance/locustfile.py +++ b/performance/locustfile.py @@ -5,9 +5,9 @@ class Stuff(HttpUser): wait_time = between(1, 3) @task - def find_stuff(self): + def find_stuff(self) -> None: self.client.get("/v1/stuff/string") @task - def find_stuff_with_pool(self): + def find_stuff_with_pool(self) -> None: self.client.get("/v1/stuff/pool/string") diff --git a/pyproject.toml b/pyproject.toml index cf6982c..bb6e282 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -42,6 +42,9 @@ dev-dependencies = [ "tryceratops==2.4.1", ] +[tool.pyrefly.errors] +redundant-cast = "warn" + [tool.mypy] strict = true @@ -83,3 +86,11 @@ format-command="ruff format --stdin-filename {filename}" [tool.inline-snapshot.shortcuts] review=["review"] fix=["create","fix"] + +[tool.pyrefly] +project-excludes = [ + "**/venv*", + "**/.venv*", + "**/alembic*", +] +preset = "legacy"