5
5
from typing import Any
6
6
from typing import Union
7
7
8
+ from setuptools import Distribution
9
+
8
10
from .. import _log
9
11
10
12
if TYPE_CHECKING :
@@ -21,7 +23,7 @@ class VersionInferenceConfig:
21
23
pyproject_data : PyProjectData | None
22
24
overrides : dict [str , Any ] | None
23
25
24
- def apply (self , dist : Any ) -> None :
26
+ def apply (self , dist : Distribution ) -> None :
25
27
"""Apply version inference to the distribution."""
26
28
from .. import _config as _config_module
27
29
from .._get_version_impl import _get_version
@@ -42,34 +44,32 @@ def apply(self, dist: Any) -> None:
42
44
43
45
# Mark that this version was set by infer_version if overrides is None (infer_version context)
44
46
if self .overrides is None :
45
- dist ._setuptools_scm_version_set_by_infer = True
47
+ dist ._setuptools_scm_version_set_by_infer = True # type: ignore[attr-defined]
46
48
47
49
48
50
@dataclass
49
- class VersionInferenceError :
51
+ class VersionInferenceWarning :
50
52
"""Error message for user."""
51
53
52
54
message : str
53
- should_warn : bool = False
54
55
55
- def apply (self , dist : Any ) -> None :
56
+ def apply (self , dist : Distribution ) -> None :
56
57
"""Apply error handling to the distribution."""
57
58
import warnings
58
59
59
- if self .should_warn :
60
- warnings .warn (self .message )
60
+ warnings .warn (self .message )
61
61
62
62
63
63
class VersionInferenceNoOp :
64
64
"""No operation result - silent skip."""
65
65
66
- def apply (self , dist : Any ) -> None :
66
+ def apply (self , dist : Distribution ) -> None :
67
67
"""Apply no-op to the distribution."""
68
68
69
69
70
70
VersionInferenceResult = Union [
71
71
VersionInferenceConfig , # Proceed with inference
72
- VersionInferenceError , # Show error/ warning
72
+ VersionInferenceWarning , # Show warning
73
73
VersionInferenceNoOp , # Don't infer (silent)
74
74
]
75
75
@@ -92,37 +92,21 @@ def get_version_inference_config(
92
92
Returns:
93
93
VersionInferenceResult with the decision and configuration
94
94
"""
95
- # Normalize name from project metadata when not provided
96
- if dist_name is None :
97
- dist_name = pyproject_data .project_name
98
95
99
- # Never infer a version for setuptools-scm itself
100
- if dist_name == "setuptools-scm" :
101
- return VersionInferenceNoOp ()
96
+ config = VersionInferenceConfig (
97
+ dist_name = dist_name ,
98
+ pyproject_data = pyproject_data ,
99
+ overrides = overrides ,
100
+ )
101
+
102
+ inference_implied = pyproject_data .should_infer () or overrides is not None
102
103
103
- # If a version already exists, short-circuit by context
104
- if current_version is not None :
105
- if overrides is None :
106
- # infer_version called and a version is already present → do nothing
107
- return VersionInferenceNoOp ()
104
+ if inference_implied :
105
+ if current_version is None :
106
+ return config
108
107
else :
109
- # version_keyword context - always warn if version already set
110
- return VersionInferenceError (
108
+ return VersionInferenceWarning (
111
109
f"version of { dist_name } already set" ,
112
- should_warn = pyproject_data .should_infer (),
113
110
)
114
-
115
- # No version present yet
116
- if overrides is not None :
117
- # version_keyword path: any overrides (empty or not) mean we should infer
118
- return VersionInferenceConfig (
119
- dist_name = dist_name , pyproject_data = pyproject_data , overrides = overrides
120
- )
121
-
122
- # infer_version path: only infer when [tool.setuptools_scm] is present
123
- if pyproject_data .should_infer ():
124
- return VersionInferenceConfig (
125
- dist_name = dist_name , pyproject_data = pyproject_data , overrides = overrides
126
- )
127
-
128
- return VersionInferenceNoOp ()
111
+ else :
112
+ return VersionInferenceNoOp ()
0 commit comments