From 85e9f26b1eb47de6805e96c5d64e2a476102f49f Mon Sep 17 00:00:00 2001 From: Tom Kuson Date: Sat, 28 Sep 2024 23:00:31 +0100 Subject: [PATCH] Inherit from abc.ABC If we're doing nominal typing, do it correctly. --- pyproject.toml | 2 +- src/screener/py.typed | 0 src/screener/reader/abstract.py | 22 --------------------- src/screener/reader/epub.py | 2 +- src/screener/reader/kindle.py | 2 +- src/screener/reader/types.py | 35 +++++++++++++++++++++++++++++++++ 6 files changed, 38 insertions(+), 25 deletions(-) create mode 100644 src/screener/py.typed delete mode 100644 src/screener/reader/abstract.py create mode 100644 src/screener/reader/types.py diff --git a/pyproject.toml b/pyproject.toml index 05cd0f5..ec29fb5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -81,7 +81,7 @@ target-version = "py310" strict = true [tool.mypy] -exclude = ["dist"] +strict = true [[tool.mypy.overrides]] module = ["ebooklib", "ebooklib.epub", "mobi", "kindle"] diff --git a/src/screener/py.typed b/src/screener/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/src/screener/reader/abstract.py b/src/screener/reader/abstract.py deleted file mode 100644 index 0574aea..0000000 --- a/src/screener/reader/abstract.py +++ /dev/null @@ -1,22 +0,0 @@ -from __future__ import annotations - -from typing import TYPE_CHECKING - -if TYPE_CHECKING: - from pathlib import Path - from types import TracebackType - - -class AbstractReader: - def __init__(self: AbstractReader, file_path: Path) -> None: - self.file_path = file_path - - def __enter__(self: AbstractReader) -> AbstractReader: - raise NotImplementedError - - def __exit__( - self: AbstractReader, - exc_type: type[BaseException] | None, - exc_val: BaseException | None, - exc_tb: TracebackType | None, - ) -> None: ... diff --git a/src/screener/reader/epub.py b/src/screener/reader/epub.py index 5f68924..fdcfb97 100644 --- a/src/screener/reader/epub.py +++ b/src/screener/reader/epub.py @@ -8,7 +8,7 @@ from ebooklib import epub -from .abstract import AbstractReader +from screener.reader.types import AbstractReader class EpubFileReader(AbstractReader): diff --git a/src/screener/reader/kindle.py b/src/screener/reader/kindle.py index 7ece231..6d1b540 100644 --- a/src/screener/reader/kindle.py +++ b/src/screener/reader/kindle.py @@ -9,7 +9,7 @@ from mobi import extract -from .abstract import AbstractReader +from screener.reader.types import AbstractReader class KindleFileReader(AbstractReader): diff --git a/src/screener/reader/types.py b/src/screener/reader/types.py new file mode 100644 index 0000000..1db0145 --- /dev/null +++ b/src/screener/reader/types.py @@ -0,0 +1,35 @@ +from __future__ import annotations + +import abc +import typing + +if typing.TYPE_CHECKING: + from pathlib import Path + from types import TracebackType + + +class AbstractReader(abc.ABC): + def __init__(self: AbstractReader, file_path: Path) -> None: + self.file_path = file_path + + @abc.abstractmethod + def __enter__(self: AbstractReader) -> AbstractReader: ... + + @typing.overload + def __exit__(self, exc_type: None, exc_val: None, exc_tb: None) -> None: ... + + @typing.overload + def __exit__( + self, + exc_type: type[BaseException], + exc_val: BaseException, + exc_tb: TracebackType, + ) -> None: ... + + @abc.abstractmethod + def __exit__( + self: AbstractReader, + exc_type: type[BaseException] | None, + exc_val: BaseException | None, + exc_tb: TracebackType | None, + ) -> None: ...