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 contextlib import asynccontextmanager
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
@@ -79,7 +80,7 @@ def create_app() -> FastAPI:
|
|||||||
register_exception_handlers(app)
|
register_exception_handlers(app)
|
||||||
|
|
||||||
@app.get("/index", response_class=HTMLResponse)
|
@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 templates.TemplateResponse("index.html", {"request": request})
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|||||||
+1
-1
@@ -35,7 +35,7 @@ class Stuff(Base):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@compile_sql_or_scalar
|
@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)
|
stmt = select(cls).options(joinedload(cls.nonsense)).where(cls.name == name)
|
||||||
return stmt
|
return stmt
|
||||||
|
|
||||||
|
|||||||
+3
-3
@@ -21,15 +21,15 @@ class User(Base):
|
|||||||
_password: bytes = Column(LargeBinary, nullable=False)
|
_password: bytes = Column(LargeBinary, nullable=False)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def password(self):
|
def password(self) -> str:
|
||||||
return self._password.decode("utf-8")
|
return self._password.decode("utf-8")
|
||||||
|
|
||||||
@password.setter
|
@password.setter
|
||||||
def password(self, password: SecretStr):
|
def password(self, password: SecretStr) -> None:
|
||||||
_password_string = password.get_secret_value().encode("utf-8")
|
_password_string = password.get_secret_value().encode("utf-8")
|
||||||
self._password = bcrypt.hashpw(_password_string, bcrypt.gensalt())
|
self._password = bcrypt.hashpw(_password_string, bcrypt.gensalt())
|
||||||
|
|
||||||
def check_password(self, password: SecretStr):
|
def check_password(self, password: SecretStr) -> bool:
|
||||||
return bcrypt.checkpw(
|
return bcrypt.checkpw(
|
||||||
password.get_secret_value().encode("utf-8"), self._password
|
password.get_secret_value().encode("utf-8"), self._password
|
||||||
)
|
)
|
||||||
|
|||||||
+2
-2
@@ -1,10 +1,10 @@
|
|||||||
from granian import Granian
|
from granian import Granian
|
||||||
|
|
||||||
|
|
||||||
def startup():
|
def startup() -> None:
|
||||||
print("Server starting up...")
|
print("Server starting up...")
|
||||||
|
|
||||||
def shutdown():
|
def shutdown() -> None:
|
||||||
print("Server shutting down...")
|
print("Server shutting down...")
|
||||||
|
|
||||||
server = Granian(
|
server = Granian(
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ async def verify_jwt(request: Request, token: str) -> bool:
|
|||||||
|
|
||||||
|
|
||||||
class AuthBearer(HTTPBearer):
|
class AuthBearer(HTTPBearer):
|
||||||
def __init__(self, auto_error: bool = True):
|
def __init__(self, auto_error: bool = True) -> None:
|
||||||
super().__init__(auto_error=auto_error)
|
super().__init__(auto_error=auto_error)
|
||||||
|
|
||||||
async def __call__(self, request: Request):
|
async def __call__(self, request: Request):
|
||||||
|
|||||||
+1
-1
@@ -5,7 +5,7 @@ import orjson
|
|||||||
|
|
||||||
|
|
||||||
class StreamLLMService:
|
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.base_url = base_url
|
||||||
self.model = "llama3.2"
|
self.model = "llama3.2"
|
||||||
|
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ class SMTPEmailService(metaclass=SingletonMetaNoArgs):
|
|||||||
)
|
)
|
||||||
server: smtplib.SMTP = field(init=False) # Deferred initialization in post-init
|
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.
|
Initializes the SMTP server connection after the object is created.
|
||||||
|
|
||||||
@@ -98,7 +98,7 @@ class SMTPEmailService(metaclass=SingletonMetaNoArgs):
|
|||||||
subject: str,
|
subject: str,
|
||||||
body_text: str = "",
|
body_text: str = "",
|
||||||
body_html: str = None,
|
body_html: str = None,
|
||||||
):
|
) -> None:
|
||||||
"""
|
"""
|
||||||
Sends an email to the specified recipients.
|
Sends an email to the specified recipients.
|
||||||
|
|
||||||
@@ -130,7 +130,7 @@ class SMTPEmailService(metaclass=SingletonMetaNoArgs):
|
|||||||
template: str,
|
template: str,
|
||||||
context: dict,
|
context: dict,
|
||||||
sender: EmailStr,
|
sender: EmailStr,
|
||||||
):
|
) -> None:
|
||||||
"""
|
"""
|
||||||
Sends an email using a Jinja2 template.
|
Sends an email using a Jinja2 template.
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ def compile_sql_or_scalar(func):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
@wraps(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.
|
Wrapper function that either compiles the SQL statement or executes it.
|
||||||
|
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ class Stuff(HttpUser):
|
|||||||
wait_time = between(1, 3)
|
wait_time = between(1, 3)
|
||||||
|
|
||||||
@task
|
@task
|
||||||
def find_stuff(self):
|
def find_stuff(self) -> None:
|
||||||
self.client.get("/v1/stuff/string")
|
self.client.get("/v1/stuff/string")
|
||||||
|
|
||||||
@task
|
@task
|
||||||
def find_stuff_with_pool(self):
|
def find_stuff_with_pool(self) -> None:
|
||||||
self.client.get("/v1/stuff/pool/string")
|
self.client.get("/v1/stuff/pool/string")
|
||||||
|
|||||||
@@ -42,6 +42,9 @@ dev-dependencies = [
|
|||||||
"tryceratops==2.4.1",
|
"tryceratops==2.4.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[tool.pyrefly.errors]
|
||||||
|
redundant-cast = "warn"
|
||||||
|
|
||||||
|
|
||||||
[tool.mypy]
|
[tool.mypy]
|
||||||
strict = true
|
strict = true
|
||||||
@@ -83,3 +86,11 @@ format-command="ruff format --stdin-filename {filename}"
|
|||||||
[tool.inline-snapshot.shortcuts]
|
[tool.inline-snapshot.shortcuts]
|
||||||
review=["review"]
|
review=["review"]
|
||||||
fix=["create","fix"]
|
fix=["create","fix"]
|
||||||
|
|
||||||
|
[tool.pyrefly]
|
||||||
|
project-excludes = [
|
||||||
|
"**/venv*",
|
||||||
|
"**/.venv*",
|
||||||
|
"**/alembic*",
|
||||||
|
]
|
||||||
|
preset = "legacy"
|
||||||
|
|||||||
Reference in New Issue
Block a user