@@ -160,6 +160,24 @@ def setuptools_dynamic_version(*, pyproject_toml: dict, pyproject_toml_path: Pat
160
160
return clean_version (ver_str )
161
161
162
162
163
+ def hatchling_dynamic_version (* , pyproject_toml : dict , pyproject_toml_path : Path ) -> Version | None :
164
+ dynamic = dict_get (pyproject_toml , 'project' , 'dynamic' )
165
+ if dynamic and 'version' in dynamic :
166
+ if dict_get (pyproject_toml , 'tool' , 'hatch' , 'version' , 'path' ):
167
+ # Project used "dynamic metadata" from hatchling for the version
168
+
169
+ from hatchling .metadata .core import ProjectMetadata
170
+ from hatchling .plugin .manager import PluginManager
171
+
172
+ plugin_manager = PluginManager ()
173
+ metadata = ProjectMetadata (root = pyproject_toml_path .parent , plugin_manager = plugin_manager )
174
+ version = metadata .hatch .version
175
+ source = version .source
176
+ version_data = source .get_version_data ()
177
+ if ver_str := version_data .get ('version' ):
178
+ return clean_version (ver_str )
179
+
180
+
163
181
def get_pyproject_toml_version (package_path : Path ) -> Version | None :
164
182
pyproject_toml_path = Path (package_path , 'pyproject.toml' )
165
183
assert_is_file (pyproject_toml_path )
@@ -173,7 +191,13 @@ def get_pyproject_toml_version(package_path: Path) -> Version | None:
173
191
if ver_str :
174
192
return clean_version (ver_str )
175
193
176
- return setuptools_dynamic_version (pyproject_toml = pyproject_toml , pyproject_toml_path = pyproject_toml_path )
194
+ if version := hatchling_dynamic_version (pyproject_toml = pyproject_toml , pyproject_toml_path = pyproject_toml_path ):
195
+ return version
196
+
197
+ if version := setuptools_dynamic_version (pyproject_toml = pyproject_toml , pyproject_toml_path = pyproject_toml_path ):
198
+ return version
199
+
200
+ return None
177
201
178
202
179
203
def check_version (* , module , package_path : Path , distribution_name : str | None = None ) -> Version :
0 commit comments