mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2026-07-28 05:00:38 +03:00
refactor: add type hints to function signatures across multiple files
This commit is contained in:
+2
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
|
||||
+3
-3
@@ -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
|
||||
)
|
||||
|
||||
+2
-2
@@ -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(
|
||||
|
||||
@@ -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):
|
||||
|
||||
+1
-1
@@ -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"
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user