Skip to content

Commit

Permalink
Address MyPy issues
Browse files Browse the repository at this point in the history
  • Loading branch information
twm committed Jul 21, 2024
1 parent 66aea8d commit e27a4a7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
1 change: 1 addition & 0 deletions requirements_mypy.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mypy==0.812
twisted
hatchling # for types
12 changes: 12 additions & 0 deletions requirements_mypy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 7 additions & 5 deletions src/incremental/_hatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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]
20 changes: 13 additions & 7 deletions src/incremental/tests/test_pyproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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`
Expand All @@ -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):
Expand Down

0 comments on commit e27a4a7

Please sign in to comment.