From e27a4a7bc575bf707fe54ccc46b460faada8d3d5 Mon Sep 17 00:00:00 2001 From: Tom Most Date: Sat, 20 Jul 2024 15:40:19 -0700 Subject: [PATCH] Address MyPy issues --- requirements_mypy.in | 1 + requirements_mypy.txt | 12 ++++++++++++ src/incremental/_hatch.py | 12 +++++++----- src/incremental/tests/test_pyproject.py | 20 +++++++++++++------- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/requirements_mypy.in b/requirements_mypy.in index dc49e89..1c3f8f8 100644 --- a/requirements_mypy.in +++ b/requirements_mypy.in @@ -1,2 +1,3 @@ mypy==0.812 twisted +hatchling # for types diff --git a/requirements_mypy.txt b/requirements_mypy.txt index dbf10d5..9228c07 100644 --- a/requirements_mypy.txt +++ b/requirements_mypy.txt @@ -12,6 +12,8 @@ automat==22.10.0 # via twisted constantly==23.10.4 # via twisted +hatchling==1.25.0 + # via -r requirements_mypy.in hyperlink==21.0.0 # via twisted idna==3.7 @@ -22,8 +24,18 @@ mypy==0.812 # via -r requirements_mypy.in mypy-extensions==0.4.4 # via mypy +packaging==24.1 + # via hatchling +pathspec==0.12.1 + # via hatchling +pluggy==1.5.0 + # via hatchling six==1.16.0 # via automat +tomli==2.0.1 + # via hatchling +trove-classifiers==2024.7.2 + # via hatchling twisted==24.3.0 # via -r requirements_mypy.in typed-ast==1.4.3 diff --git a/src/incremental/_hatch.py b/src/incremental/_hatch.py index 2f3f2d9..8d677c4 100644 --- a/src/incremental/_hatch.py +++ b/src/incremental/_hatch.py @@ -3,7 +3,7 @@ import os import shlex -from typing import TypedDict +from typing import Any, Dict, List, Type, TypedDict from hatchling.version.source.plugin.interface import VersionSourceInterface from hatchling.plugin import hookimpl @@ -18,11 +18,13 @@ class _VersionData(TypedDict): class IncrementalVersionSource(VersionSourceInterface): PLUGIN_NAME = "incremental" - def get_version_data(self) -> _VersionData: - config = _load_pyproject_toml(os.path.join(self.root, "./pyproject.toml")) + def get_version_data(self) -> _VersionData: # type: ignore[override] + path = os.path.join(self.root, "./pyproject.toml") + config = _load_pyproject_toml(path) + assert config is not None, "Failed to read {}".format(path) return {"version": _existing_version(config.path).public()} - def set_version(self, version: str, version_data: dict): + def set_version(self, version: str, version_data: Dict[Any, Any]) -> None: raise NotImplementedError( f"Run `python -m incremental.version --newversion" f" {shlex.quote(version)}` to set the version.\n\n" @@ -31,5 +33,5 @@ def set_version(self, version: str, version_data: dict): @hookimpl -def hatch_register_version_source(): +def hatch_register_version_source() -> List[Type[VersionSourceInterface]]: return [IncrementalVersionSource] diff --git a/src/incremental/tests/test_pyproject.py b/src/incremental/tests/test_pyproject.py index 0d41194..df5b0ca 100644 --- a/src/incremental/tests/test_pyproject.py +++ b/src/incremental/tests/test_pyproject.py @@ -4,7 +4,7 @@ """Test handling of ``pyproject.toml`` configuration""" import os -from typing import Optional +from typing import cast, Optional, Union from pathlib import Path from twisted.trial.unittest import TestCase @@ -15,7 +15,7 @@ class VerifyPyprojectDotTomlTests(TestCase): """Test the `_load_pyproject_toml` helper function""" def _loadToml( - self, toml: str, *, path: Optional[os.PathLike] = None + self, toml: str, *, path: Union[Path, str, None] = None ) -> Optional[_IncrementalConfig]: """ Read a TOML snipped from a temporary file with `_load_pyproject_toml` @@ -24,23 +24,29 @@ def _loadToml( @param path: Path to which the TOML is written """ - path = path or self.mktemp() + path_: str + if path is None: + path_ = self.mktemp() # type: ignore + else: + path_ = str(path) - with open(path, "w") as f: + with open(path_, "w") as f: f.write(toml) try: - return _load_pyproject_toml(path) + return _load_pyproject_toml(path_) except Exception as e: if hasattr(e, "add_note"): - e.add_note(f"While loading:\n\n{toml}") # pragma: no coverage + e.add_note( # type: ignore[attr-defined] + f"While loading:\n\n{toml}" + ) # pragma: no coverage raise def test_fileNotFound(self): """ An absent ``pyproject.toml`` file produces no result """ - path = os.path.join(self.mktemp(), "pyproject.toml") + path = os.path.join(cast(str, self.mktemp()), "pyproject.toml") self.assertIsNone(_load_pyproject_toml(path)) def test_nameMissing(self):