Skip to content

Commit ac09b66

Browse files
committed
feat: include new version of hatch-vcs-footgun
adjust __init__.py, and pyproject.toml, include version.py, details see https://github.com/maresb/hatch-vcs-footgun-example
1 parent b73319d commit ac09b66

File tree

3 files changed

+73
-27
lines changed

3 files changed

+73
-27
lines changed

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ local_scheme = "dirty-tag"
6868
[tool.hatch.build]
6969
packages = ["src/utils_COMobjects"]
7070

71-
[tool.hatch.build.hooks.vcs]
72-
version-file = "src/utils_COMobjects/_version.py"
71+
# [tool.hatch.build.hooks.vcs]
72+
# version-file = "src/utils_COMobjects/_version.py"
7373

7474
[tool.hatch.build.targets.sdist]
7575
artifacts = ["_version.py"]

src/utils_COMobjects/__init__.py

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,30 @@
1313
"""
1414

1515

16+
# ruff and mypy per file settings
17+
#
18+
# empty lines
19+
# ruff: noqa: E302, E303
20+
# naming conventions
21+
# ruff: noqa: N801, N802, N803, N806, N812, N813, N815, N816, N818, N999
1622

17-
from importlib.metadata import PackageNotFoundError, version
23+
# fmt: off
1824

25+
26+
27+
# version determination
28+
29+
# original Hatchlor version
30+
# from importlib.metadata import PackageNotFoundError, version
1931
# try:
20-
# __version__ = version('utils-COMobjects')
32+
# __version__ = version('{{ cookiecutter.project_slug }}')
2133
# except PackageNotFoundError: # pragma: no cover
2234
# __version__ = 'unknown'
2335
# finally:
2436
# del version, PackageNotFoundError
2537

26-
# up-to-date version tag for modules installed in editable mode inspired by
27-
# https://github.com/maresb/hatch-vcs-footgun-example/blob/main/hatch_vcs_footgun_example/__init__.py
28-
# Define the variable '__version__':
29-
try:
30-
31-
# own developed alternative variant to hatch-vcs-footgun overcoming problem of ignored setuptools_scm settings
32-
# from hatch-based pyproject.toml libraries
33-
from hatch.cli import hatch
34-
from click.testing import CliRunner
35-
# determine version via hatch
36-
__version__ = CliRunner().invoke(hatch, ["version"]).output.strip()
37-
38-
except (ImportError, LookupError):
39-
# As a fallback, use the version that is hard-coded in the file.
40-
try:
41-
from ._version import __version__ # noqa: F401
42-
except ModuleNotFoundError:
43-
# The user is probably trying to run this without having installed the
44-
# package, so complain.
45-
raise RuntimeError(
46-
f"Package {__package__} is not correctly installed. Please install it with pip."
47-
)
48-
38+
# latest import requirement for hatch-vcs-footgun-example
39+
from utils_COMobjects.version import __version__
4940

5041

5142
import sys

src/utils_COMobjects/version.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""Compute the version number and store it in the `__version__` variable.
2+
3+
Based on <https://github.com/maresb/hatch-vcs-footgun-example>.
4+
"""
5+
6+
7+
# ruff and mypy per file settings
8+
# others
9+
# ruff: noqa: PLC0415
10+
11+
# fmt: off
12+
13+
14+
def _get_hatch_version():
15+
"""Compute the most up-to-date version number in a development environment.
16+
17+
Returns `None` if Hatchling is not installed, e.g. in a production environment.
18+
19+
For more details, see <https://github.com/maresb/hatch-vcs-footgun-example/>.
20+
"""
21+
import os
22+
23+
try:
24+
from hatchling.metadata.core import ProjectMetadata
25+
from hatchling.plugin.manager import PluginManager
26+
from hatchling.utils.fs import locate_file
27+
except ImportError:
28+
# Hatchling is not installed, so probably we are not in a development environment.
29+
return None
30+
31+
pyproject_toml = locate_file(__file__, "pyproject.toml")
32+
if pyproject_toml is None:
33+
err_msg = "pyproject.toml not found although hatchling is installed"
34+
raise RuntimeError(err_msg)
35+
root = os.path.dirname(pyproject_toml)
36+
metadata = ProjectMetadata(root=root, plugin_manager=PluginManager())
37+
# Version can be either statically set in pyproject.toml or computed dynamically:
38+
return metadata.core.version or metadata.hatch.version.cached
39+
40+
41+
def _get_importlib_metadata_version():
42+
"""Compute the version number using importlib.metadata.
43+
44+
This is the official Pythonic way to get the version number of an installed
45+
package. However, it is only updated when a package is installed. Thus, if a
46+
package is installed in editable mode, and a different version is checked out,
47+
then the version number will not be updated.
48+
"""
49+
from importlib.metadata import version
50+
51+
__version__ = version(__package__)
52+
return __version__
53+
54+
55+
__version__ = _get_hatch_version() or _get_importlib_metadata_version()

0 commit comments

Comments
 (0)