4
4
"""
5
5
import os
6
6
import warnings
7
+ from typing import NoReturn
8
+ from typing import Optional
7
9
10
+ from . import _types
8
11
from ._entrypoints import _call_entrypoint_fn
9
12
from ._entrypoints import _version_from_entrypoints
10
13
from ._overrides import _read_pretended_version_for
22
25
from .utils import trace
23
26
from .version import format_version
24
27
from .version import meta
28
+ from .version import ScmVersion
25
29
26
30
TEMPLATES = {
27
31
".py" : """\
@@ -42,14 +46,16 @@ def version_from_scm(root):
42
46
stacklevel = 2 ,
43
47
)
44
48
config = Configuration (root = root )
45
- # TODO: Is it API?
46
49
return _version_from_entrypoints (config )
47
50
48
51
49
- def dump_version (root , version : str , write_to , template : "str | None" = None ):
52
+ def dump_version (
53
+ root : _types .PathT ,
54
+ version : str ,
55
+ write_to : _types .PathT ,
56
+ template : "str | None" = None ,
57
+ ):
50
58
assert isinstance (version , str )
51
- if not write_to :
52
- return
53
59
target = os .path .normpath (os .path .join (root , write_to ))
54
60
ext = os .path .splitext (target )[1 ]
55
61
template = template or TEMPLATES .get (ext )
@@ -66,7 +72,7 @@ def dump_version(root, version: str, write_to, template: "str | None" = None):
66
72
fp .write (template .format (version = version , version_tuple = version_tuple ))
67
73
68
74
69
- def _do_parse (config ) :
75
+ def _do_parse (config : Configuration ) -> "ScmVersion|None" :
70
76
pretended = _read_pretended_version_for (config )
71
77
if pretended is not None :
72
78
return pretended
@@ -77,74 +83,68 @@ def _do_parse(config):
77
83
raise TypeError (
78
84
"version parse result was a string\n please return a parsed version"
79
85
)
80
- version = parse_result or _version_from_entrypoints (config , fallback = True )
86
+ version : Optional [ScmVersion ]
87
+ if parse_result :
88
+ assert isinstance (parse_result , ScmVersion )
89
+ version = parse_result
90
+ else :
91
+ version = _version_from_entrypoints (config , fallback = True )
81
92
else :
82
93
# include fallbacks after dropping them from the main entrypoint
83
94
version = _version_from_entrypoints (config ) or _version_from_entrypoints (
84
95
config , fallback = True
85
96
)
86
97
87
- if version :
88
- return version
98
+ return version
99
+
89
100
101
+ def _version_missing (config ) -> NoReturn :
90
102
raise LookupError (
91
- "setuptools-scm was unable to detect version for %r .\n \n "
103
+ f "setuptools-scm was unable to detect version for { config . absolute_root } .\n \n "
92
104
"Make sure you're either building from a fully intact git repository "
93
105
"or PyPI tarballs. Most other sources (such as GitHub's tarballs, a "
94
106
"git checkout without the .git folder) don't contain the necessary "
95
107
"metadata and will not work.\n \n "
96
108
"For example, if you're using pip, instead of "
97
109
"https://github.com/user/proj/archive/master.zip "
98
- "use git+https://github.com/user/proj.git#egg=proj" % config . absolute_root
110
+ "use git+https://github.com/user/proj.git#egg=proj"
99
111
)
100
112
101
113
102
- def get_version (
103
- root = "." ,
104
- version_scheme = DEFAULT_VERSION_SCHEME ,
105
- local_scheme = DEFAULT_LOCAL_SCHEME ,
106
- write_to = None ,
107
- write_to_template = None ,
108
- relative_to = None ,
109
- tag_regex = DEFAULT_TAG_REGEX ,
110
- parentdir_prefix_version = None ,
111
- fallback_version = None ,
112
- fallback_root = "." ,
113
- parse = None ,
114
- git_describe_command = None ,
115
- dist_name = None ,
116
- version_cls = None ,
117
- normalize = True ,
118
- search_parent_directories = False ,
119
- ):
114
+ @_types .transfer_input_args (Configuration )
115
+ def get_version (** kw ) -> str :
120
116
"""
121
117
If supplied, relative_to should be a file from which root may
122
118
be resolved. Typically called by a script or module that is not
123
119
in the root of the repository to direct setuptools_scm to the
124
120
root of the repository by supplying ``__file__``.
125
121
"""
126
122
127
- config = Configuration (** locals ())
128
- return _get_version (config )
123
+ config = Configuration (** kw )
124
+ maybe_version = _get_version (config )
125
+ if maybe_version is None :
126
+ _version_missing (config )
127
+ return maybe_version
129
128
130
129
131
- def _get_version (config ) :
130
+ def _get_version (config : Configuration ) -> "str|None" :
132
131
parsed_version = _do_parse (config )
133
-
134
- if parsed_version :
135
- version_string = format_version (
136
- parsed_version ,
137
- version_scheme = config .version_scheme ,
138
- local_scheme = config .local_scheme ,
139
- )
132
+ if parsed_version is None :
133
+ return None
134
+ version_string = format_version (
135
+ parsed_version ,
136
+ version_scheme = config .version_scheme ,
137
+ local_scheme = config .local_scheme ,
138
+ )
139
+ if config .write_to is not None :
140
140
dump_version (
141
141
root = config .root ,
142
142
version = version_string ,
143
143
write_to = config .write_to ,
144
144
template = config .write_to_template ,
145
145
)
146
146
147
- return version_string
147
+ return version_string
148
148
149
149
150
150
# Public API
0 commit comments