From d4d9a210048e320bc9b2ca7cc20e713065fa34a6 Mon Sep 17 00:00:00 2001 From: Tom Kuson Date: Sat, 28 Sep 2024 23:00:31 +0100 Subject: [PATCH] Remove nominal typing via abc.ABC --- pyproject.toml | 2 +- src/screener/py.typed | 0 src/screener/reader/abstract.py | 22 ---------------------- src/screener/reader/epub.py | 33 +++++++++++++++++++++++++-------- src/screener/reader/kindle.py | 27 ++++++++++++++++++--------- tests/test_parser.py | 6 +----- 6 files changed, 45 insertions(+), 45 deletions(-) create mode 100644 src/screener/py.typed delete mode 100644 src/screener/reader/abstract.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..86e5b37 100644 --- a/src/screener/reader/epub.py +++ b/src/screener/reader/epub.py @@ -1,24 +1,41 @@ from __future__ import annotations +import typing import warnings -from typing import TYPE_CHECKING -if TYPE_CHECKING: +if typing.TYPE_CHECKING: from pathlib import Path + from types import TracebackType from ebooklib import epub -from .abstract import AbstractReader - -class EpubFileReader(AbstractReader): - def __init__(self: EpubFileReader, file_path: Path) -> None: - super().__init__(file_path) +class EpubFileReader: + def __init__(self, file_path: Path) -> None: + self.file_path = file_path self.book: epub.EpubBook - def __enter__(self: EpubFileReader) -> EpubFileReader: + def __enter__(self) -> EpubFileReader: with warnings.catch_warnings(): # Have to do this because of bug in ebooklib. warnings.simplefilter("ignore") self.book = epub.read_epub(self.file_path, options={"ignore_ncx": False}) return self + + @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: ... + + def __exit__( + self, + exc_type: type[BaseException] | None, + exc_val: BaseException | None, + exc_tb: TracebackType | None, + ) -> None: ... diff --git a/src/screener/reader/kindle.py b/src/screener/reader/kindle.py index 7ece231..a61ed4b 100644 --- a/src/screener/reader/kindle.py +++ b/src/screener/reader/kindle.py @@ -1,24 +1,22 @@ from __future__ import annotations import shutil -from typing import TYPE_CHECKING +import typing -if TYPE_CHECKING: +if typing.TYPE_CHECKING: from pathlib import Path from types import TracebackType from mobi import extract -from .abstract import AbstractReader - -class KindleFileReader(AbstractReader): - def __init__(self: KindleFileReader, file_path: Path) -> None: - super().__init__(file_path) +class KindleFileReader: + def __init__(self, file_path: Path) -> None: + self.file_path = file_path self._temp_dir = None self.generated_translation = None - def __enter__(self: KindleFileReader) -> KindleFileReader: + def __enter__(self) -> KindleFileReader: # The `mobi` library likes file paths as strings for whatever reason. self._temp_dir, self.generated_translation = extract(str(self.file_path)) if self.generated_translation is None or self._temp_dir is None: @@ -26,8 +24,19 @@ def __enter__(self: KindleFileReader) -> KindleFileReader: raise ValueError(msg) return self + @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: ... + def __exit__( - self: KindleFileReader, + self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None, diff --git a/tests/test_parser.py b/tests/test_parser.py index 5b1cd13..1f2094f 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -1,13 +1,9 @@ -"""Test the parser module; for use with `pytest`.""" - from __future__ import annotations from pathlib import Path from screener.checker import Checker -from screener.diagnostic import ( - JavaScriptDiagnostic, -) +from screener.diagnostic import JavaScriptDiagnostic from screener.parser import parse_epub, parse_kindle