Skip to content

Commit

Permalink
add mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
jonra1993 committed Aug 12, 2023
1 parent 9143001 commit c4f0bf4
Show file tree
Hide file tree
Showing 12 changed files with 455 additions and 391 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ lint:
cd backend/app && \
poetry run ruff app && poetry run black --check app

mypy:
cd backend/app && \
poetry run mypy .

lint-watch:
cd backend/app && \
poetry run ruff app --watch
Expand Down
3 changes: 2 additions & 1 deletion backend/app/app/api/deps.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections.abc import AsyncGenerator
from typing import Callable
from fastapi import Depends, HTTPException, status
from app.utils.token import get_valid_tokens
from app.utils.minio_client import MinioClient
Expand Down Expand Up @@ -46,7 +47,7 @@ async def get_general_meta() -> IMetaGeneral:
return IMetaGeneral(roles=current_roles)


def get_current_user(required_roles: list[str] = None) -> User:
def get_current_user(required_roles: list[str] = None) -> Callable[[], User]:
async def current_user(
token: str = Depends(reusable_oauth2),
redis_client: Redis = Depends(get_redis_client),
Expand Down
7 changes: 2 additions & 5 deletions backend/app/app/api/v1/endpoints/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ async def get_new_access_token(

access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
user = await crud.user.get(id=user_id)
if user.is_active:
if getattr(user, "is_active"):
access_token = security.create_access_token(
payload["sub"], expires_delta=access_token_expires
)
Expand Down Expand Up @@ -218,7 +218,4 @@ async def login_access_token(
TokenType.ACCESS,
settings.ACCESS_TOKEN_EXPIRE_MINUTES,
)
return {
"access_token": access_token,
"token_type": "bearer",
}
return TokenRead(access_token=access_token, token_type="bearer")
1 change: 0 additions & 1 deletion backend/app/app/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class ModeEnum(str, Enum):
class Settings(BaseSettings):
MODE: ModeEnum = ModeEnum.development
API_VERSION: str = "v1"
API_VERSION: str = "v1"
API_V1_STR: str = f"/api/{API_VERSION}"
PROJECT_NAME: str
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 1 # 1 hour
Expand Down
1 change: 0 additions & 1 deletion backend/app/app/crud/base_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from fastapi_async_sqlalchemy import db
from fastapi_async_sqlalchemy.middleware import DBSessionMeta
from fastapi_pagination import Params, Page
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel
from sqlmodel import SQLModel, select, func
from sqlmodel.ext.asyncio.session import AsyncSession
Expand Down
7 changes: 4 additions & 3 deletions backend/app/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ async def user_id_identifier(request: Request):
if forwarded:
return forwarded.split(",")[0]

ip = request.client.host
client = request.client
ip = getattr(client, "host", "0.0.0.0")
return ip + ":" + request.scope["path"]


Expand Down Expand Up @@ -133,8 +134,8 @@ class CustomException(Exception):
code: str
message: str

def __init__(self, http_code: int = None, code: str = None, message: str = None):
self.http_code = http_code if http_code else 500
def __init__(self, http_code: int = 500, code: str | None = None, message: str = 'This is an error message'):
self.http_code = http_code
self.code = code if code else str(self.http_code)
self.message = message

Expand Down
11 changes: 9 additions & 2 deletions backend/app/app/schemas/response_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,17 @@ class IDeleteResponseBase(IResponseBase[DataType], Generic[DataType]):


def create_response(
data: DataType | None,
data: DataType,
message: str | None = None,
meta: dict | Any | None = {},
) -> dict[str, DataType] | DataType:
) -> (
IResponseBase[DataType]
| IGetResponsePaginated[DataType]
| IGetResponseBase[DataType]
| IPutResponseBase[DataType]
| IDeleteResponseBase[DataType]
| IPostResponseBase[DataType]
):
if isinstance(data, IGetResponsePaginated):
data.message = "Data paginated correctly" if message is None else message
data.meta = meta
Expand Down
790 changes: 418 additions & 372 deletions backend/app/poetry.lock

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions backend/app/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ ignore = [
[tool.ruff.per-file-ignores]
"__init__.py" = ["F401"]

[tool.mypy]
warn_return_any = true
warn_unused_configs = true
ignore_missing_imports = true
exclude = ["alembic", "__pycache__"]


[tool.poetry.dependencies]
python = ">3.9,<3.12"
alembic = "^1.10.2"
Expand Down Expand Up @@ -65,16 +72,16 @@ torch = [
requests = "^2.29.0"
wheel = "^0.40.0"
setuptools = "^67.7.2"
langchain = "^0.0.151"
langchain = "^0.0.262"
openai = "^0.27.5"
fastapi-limiter = "^0.1.5"
pexpect = "^4.8.0"

[tool.poetry.group.dev.dependencies]
black = "^23.1.0"
ruff = "^0.0.256"
pytest = "^7.4.0"
pytest-asyncio = "^0.21.1"
mypy = "^1.5.0"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
3 changes: 2 additions & 1 deletion backend/app/test/api/test_login.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import pytest
from httpx import AsyncClient
from typing import AsyncGenerator
from app.main import app
from app.core.config import settings

url = "http://fastapi.localhost/api/v1"

@pytest.fixture(scope='function')
async def test_client() -> AsyncClient:
async def test_client() -> AsyncGenerator[AsyncClient, None]:
async with AsyncClient(app=app, base_url=url) as client:
yield client

Expand Down
3 changes: 2 additions & 1 deletion backend/app/test/api/test_user.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import pytest
from httpx import AsyncClient
from app.main import app
from typing import AsyncGenerator
from app.core.config import settings

url = "http://fastapi.localhost/api/v1"

@pytest.fixture(scope='function')
async def test_client() -> AsyncClient:
async def test_client() -> AsyncGenerator[AsyncClient, None]:
async with AsyncClient(app=app, base_url=url) as client:
yield client

Expand Down
5 changes: 3 additions & 2 deletions backend/app/test/test_main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import pytest
from httpx import AsyncClient, Response
from httpx import AsyncClient
from typing import AsyncGenerator
from app.main import app
client = AsyncClient(app=app)

url = "http://fastapi.localhost"

@pytest.fixture(scope='function')
async def test_client() -> AsyncClient:
async def test_client() -> AsyncGenerator[AsyncClient, None]:
async with AsyncClient(app=app, base_url=url) as client:
yield client

Expand Down

0 comments on commit c4f0bf4

Please sign in to comment.