Skip to content

Commit db199f7

Browse files
fix: use Self return type for PyProjectData.for_testing
Using Self as the return type for classmethod PyProjectData.for_testing allows subclasses (like setuptools_scm's extended PyProjectData) to have the correct return type without needing overrides or type ignore comments. Changes: - Added Self import to vcs_versioning._pyproject_reading - Changed return type from PyProjectData to Self for for_testing and empty methods - Removed now-unnecessary type: ignore comments in setuptools.py and test_cli.py This fixes all remaining mypy errors related to PyProjectData type compatibility.
1 parent 16f2cc9 commit db199f7

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

nextgen/vcs-versioning/src/vcs_versioning/_pyproject_reading.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
from __future__ import annotations
44

55
import logging
6+
import sys
67
import warnings
78

89
from dataclasses import dataclass
910
from pathlib import Path
1011
from typing import Sequence
1112

13+
if sys.version_info >= (3, 11):
14+
from typing import Self
15+
else:
16+
from typing_extensions import Self
17+
1218
from . import _types as _t
1319
from ._requirement_cls import extract_package_name
1420
from ._toml import TOML_RESULT
@@ -48,7 +54,7 @@ def for_testing(
4854
has_dynamic_version: bool = True,
4955
build_requires: list[str] | None = None,
5056
local_scheme: str | None = None,
51-
) -> PyProjectData:
57+
) -> Self:
5258
"""Create a PyProjectData instance for testing purposes."""
5359
project: TOML_RESULT
5460
if project_name is not None:
@@ -82,7 +88,7 @@ def for_testing(
8288
@classmethod
8389
def empty(
8490
cls, path: Path = DEFAULT_PYPROJECT_PATH, tool_name: str = DEFAULT_TOOL_NAME
85-
) -> PyProjectData:
91+
) -> Self:
8692
return cls(
8793
path=path,
8894
tool_name=tool_name,

src/setuptools_scm/_integration/setuptools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def version_keyword(
103103
pyproject_data = read_pyproject(_given_result=_given_pyproject_data)
104104
except FileNotFoundError:
105105
log.debug("pyproject.toml not found, proceeding with empty configuration")
106-
pyproject_data = PyProjectData.empty() # type: ignore[assignment]
106+
pyproject_data = PyProjectData.empty()
107107
except InvalidTomlError as e:
108108
log.debug("Configuration issue in pyproject.toml: %s", e)
109109
return

testing/test_cli.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ def _create_version_file_pyproject_data() -> PyProjectData:
4040
section_present=True, project_present=True, project_name="test"
4141
)
4242
data.section["version_file"] = "ver.py"
43-
# Type: PyProjectData.for_testing returns the correct type
44-
return data # type: ignore[return-value]
43+
return data
4544

4645

4746
def get_output(

0 commit comments

Comments
 (0)