Skip to content

Commit

Permalink
fix mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
didhat committed Oct 14, 2023
1 parent aff3f37 commit c8b7a02
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 11 deletions.
6 changes: 6 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[mypy]
python_version = 3.11

[mypy-ebooklib.*]
ignore_missing_imports = true

9 changes: 4 additions & 5 deletions src/application/dto.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ def from_chapter(cls, chapter: Chapter):

@attrs.define
class BookForUploadDTO:
format: str
book_title: str
book_author: str
format: str | None
book_title: str | None
book_author: str | None


@attrs.define
class BookForUploadWithFileDTO(BookForUploadDTO):
file: SpooledTemporaryFile
filename: str
filename: str | None


@attrs.define
Expand Down Expand Up @@ -57,4 +57,3 @@ class CoverFormat(enum.StrEnum):
class BookCoverFile:
file: bytes
format: str

2 changes: 1 addition & 1 deletion src/infrastructure/adapters/book_cover_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def __init__(self, path: pathlib.Path):

async def get_cover_file(self, book_id: str) -> dto.BookCoverFile | None:
file_path, fmt = self._get_file_path_with_cover(book_id)
if file_path is None:
if file_path is None or fmt is None:
return None

return dto.BookCoverFile(file=file_path.read_bytes(), format=fmt)
Expand Down
4 changes: 2 additions & 2 deletions src/infrastructure/epub/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from src.infrastructure.epub.sorted import EpubBookWithMethods


def load_epub_from_upload(file: SpooledTemporaryFile, filename: str):
def load_epub_from_upload(file: SpooledTemporaryFile, filename: str | None):
reader = EpubBookWithUploadFromBinary.from_binary(file, filename)

book = reader.book
Expand All @@ -18,7 +18,7 @@ def load_epub_from_upload(file: SpooledTemporaryFile, filename: str):

class EpubBookWithUploadFromBinary(epub.EpubReader):
@classmethod
def from_binary(cls, file: SpooledTemporaryFile, filename: str):
def from_binary(cls, file: SpooledTemporaryFile, filename: str | None):
file_copy = create_file_deepcopy(file)
reader = cls(filename, {"ignore_ncx": True})

Expand Down
8 changes: 6 additions & 2 deletions src/presentation/entrypoints/book.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Annotated
from tempfile import SpooledTemporaryFile
from typing import Annotated, cast

from fastapi import APIRouter, Depends, UploadFile, Form, Response, Request
from fastapi_filter import FilterDepends
Expand Down Expand Up @@ -34,7 +35,7 @@ async def upload_book(
book_service: BookService = Depends(get_book_service),
) -> BookUploadedResponse:
upload = dto.BookForUploadWithFileDTO(
file=book_file.file,
file=cast(SpooledTemporaryFile, book_file.file),
filename=book_file.filename,
format=book_file.content_type,
book_title=title,
Expand Down Expand Up @@ -85,4 +86,7 @@ async def get_book_cover(
):
cover = await book_service.get_book_cover(book_id)

if cover is None:
return None

return Response(content=cover.file, media_type=f"image/{cover.format}")
2 changes: 1 addition & 1 deletion src/presentation/filters/book.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ class BookFilter(Filter):
chapter_number__lte: Optional[int] = None

class Constants(Filter.Constants):
model = book_table.c
model = book_table.c # type: ignore

0 comments on commit c8b7a02

Please sign in to comment.