From 6fe2efe5297d0ee8279e45266c8d88c1e252ecbc Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Mon, 9 Sep 2024 21:13:23 +0300 Subject: [PATCH] Generate __version__ at build to avoid slow importlib.metadata import --- .gitignore | 3 +++ pyproject.toml | 3 +++ src/humanize/__init__.py | 5 +---- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 68bc17f9..bddede8e 100644 --- a/.gitignore +++ b/.gitignore @@ -158,3 +158,6 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ + +# hatch-vcs +src/*/_version.py diff --git a/pyproject.toml b/pyproject.toml index d6e9aa33..d670783c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -53,6 +53,9 @@ version.source = "vcs" [tool.hatch.build] artifacts = [ "*.mo" ] +[tool.hatch.build.hooks.vcs] +version-file = "src/humanize/_version.py" + [tool.hatch.version.raw-options] local_scheme = "no-local-version" diff --git a/src/humanize/__init__.py b/src/humanize/__init__.py index 19c7c912..60390e2d 100644 --- a/src/humanize/__init__.py +++ b/src/humanize/__init__.py @@ -2,8 +2,6 @@ from __future__ import annotations -import importlib.metadata - from humanize.filesize import naturalsize from humanize.i18n import activate, deactivate, decimal_separator, thousands_separator from humanize.number import ( @@ -24,8 +22,7 @@ precisedelta, ) -__version__ = importlib.metadata.version(__name__) - +from ._version import __version__ __all__ = [ "__version__",