Skip to content

Commit

Permalink
Remove nominal typing via abc.ABC
Browse files Browse the repository at this point in the history
  • Loading branch information
tjkuson committed Sep 28, 2024
1 parent 635df30 commit d4d9a21
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 45 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ target-version = "py310"
strict = true

[tool.mypy]
exclude = ["dist"]
strict = true

[[tool.mypy.overrides]]
module = ["ebooklib", "ebooklib.epub", "mobi", "kindle"]
Expand Down
Empty file added src/screener/py.typed
Empty file.
22 changes: 0 additions & 22 deletions src/screener/reader/abstract.py

This file was deleted.

33 changes: 25 additions & 8 deletions src/screener/reader/epub.py
Original file line number Diff line number Diff line change
@@ -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: ...
27 changes: 18 additions & 9 deletions src/screener/reader/kindle.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,42 @@
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:
msg = "Could not extract Kindle file"
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,
Expand Down
6 changes: 1 addition & 5 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down

0 comments on commit d4d9a21

Please sign in to comment.