Skip to content

Commit fa6e3ac

Browse files
committed
Support getting the current version from hatchling
1 parent 1603d77 commit fa6e3ac

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ See also git tags: https://github.com/jedie/manageprojects/tags
350350
[comment]: <> (✂✂✂ auto generated history start ✂✂✂)
351351

352352
* [v0.21.3](https://github.com/jedie/manageprojects/compare/v0.21.2...v0.21.3)
353+
* 2025-04-22 - Support getting the current version from hatchling
353354
* 2025-04-22 - replace setuptools with hatchling
354355
* [v0.21.2](https://github.com/jedie/manageprojects/compare/v0.21.1...v0.21.2)
355356
* 2025-03-11 - Fix nox CLI call

manageprojects/tests/test_utilities_publish.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import inspect
22
import sys
3+
import tomllib
34
from pathlib import Path
45
from unittest import TestCase
56
from unittest.mock import patch
@@ -18,17 +19,12 @@
1819
build,
1920
clean_version,
2021
get_pyproject_toml_version,
22+
hatchling_dynamic_version,
2123
setuptools_dynamic_version,
2224
)
2325
from manageprojects.utilities.temp_path import TemporaryDirectory
2426

2527

26-
try:
27-
import tomllib # New in Python 3.11
28-
except ImportError:
29-
import tomli as tomllib
30-
31-
3228
class PublishTestCase(TestCase):
3329
def test_build(self):
3430
def return_callback(popenargs, args, kwargs):
@@ -209,3 +205,14 @@ def return_callback(popenargs, args, kwargs):
209205
['.../git', 'push', '--tags'],
210206
],
211207
)
208+
209+
def test_hatchling_dynamic_version(self):
210+
pyproject_toml_path = PACKAGE_ROOT / 'pyproject.toml'
211+
pyproject_toml = tomllib.loads(pyproject_toml_path.read_text())
212+
213+
version = hatchling_dynamic_version(
214+
pyproject_toml=pyproject_toml,
215+
pyproject_toml_path=PACKAGE_ROOT / 'pyproject.toml',
216+
)
217+
self.assertIsInstance(version, Version)
218+
self.assertEqual(version, Version(manageprojects.__version__))

manageprojects/utilities/publish.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,24 @@ def setuptools_dynamic_version(*, pyproject_toml: dict, pyproject_toml_path: Pat
160160
return clean_version(ver_str)
161161

162162

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+
163181
def get_pyproject_toml_version(package_path: Path) -> Version | None:
164182
pyproject_toml_path = Path(package_path, 'pyproject.toml')
165183
assert_is_file(pyproject_toml_path)
@@ -173,7 +191,13 @@ def get_pyproject_toml_version(package_path: Path) -> Version | None:
173191
if ver_str:
174192
return clean_version(ver_str)
175193

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
177201

178202

179203
def check_version(*, module, package_path: Path, distribution_name: str | None = None) -> Version:

0 commit comments

Comments
 (0)