Skip to content

Commit 74dcc60

Browse files
authored
Speedup x 1.85: Generate __version__ at build to avoid slow importlib.metadata import (#200)
2 parents a72a36d + 6fe2efe commit 74dcc60

6 files changed

Lines changed: 31 additions & 12 deletions

File tree

.github/workflows/lint.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on: [push, pull_request, workflow_dispatch]
44

55
env:
66
FORCE_COLOR: 1
7+
PIP_DISABLE_PIP_VERSION_CHECK: 1
78

89
permissions:
910
contents: read
@@ -17,4 +18,12 @@ jobs:
1718
- uses: actions/setup-python@v5
1819
with:
1920
python-version: "3.x"
21+
cache: pip
2022
- uses: pre-commit/action@v3.0.1
23+
24+
- name: Install dependencies
25+
run: |
26+
python3 -m pip install -U tox
27+
28+
- name: Mypy
29+
run: tox -e mypy

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,6 @@ cython_debug/
158158
# and can be added to the global gitignore or merged into this file. For a more nuclear
159159
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160160
#.idea/
161+
162+
# hatch-vcs
163+
src/*/_version.py

.pre-commit-config.yaml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,6 @@ repos:
3636
hooks:
3737
- id: actionlint
3838

39-
- repo: https://github.com/pre-commit/mirrors-mypy
40-
rev: v1.11.2
41-
hooks:
42-
- id: mypy
43-
additional_dependencies: [pytest, types-freezegun, types-setuptools]
44-
args: [--strict, --pretty, --show-error-codes, .]
45-
pass_filenames: false
46-
4739
- repo: https://github.com/tox-dev/pyproject-fmt
4840
rev: 2.2.3
4941
hooks:

pyproject.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ version.source = "vcs"
5353
[tool.hatch.build]
5454
artifacts = [ "*.mo" ]
5555

56+
[tool.hatch.build.hooks.vcs]
57+
version-file = "src/humanize/_version.py"
58+
5659
[tool.hatch.version.raw-options]
5760
local_scheme = "no-local-version"
5861

@@ -105,3 +108,8 @@ filterwarnings = [
105108
"ignore:sys.monitoring isn't available, using default core:coverage.exceptions.CoverageWarning",
106109
]
107110
testpaths = [ "tests" ]
111+
112+
[tool.mypy]
113+
pretty = true
114+
strict = true
115+
show_error_codes = true

src/humanize/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
from __future__ import annotations
44

5-
import importlib.metadata
6-
75
from humanize.filesize import naturalsize
86
from humanize.i18n import activate, deactivate, decimal_separator, thousands_separator
97
from humanize.number import (
@@ -24,8 +22,7 @@
2422
precisedelta,
2523
)
2624

27-
__version__ = importlib.metadata.version(__name__)
28-
25+
from ._version import __version__
2926

3027
__all__ = [
3128
"__version__",

tox.ini

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ requires =
44
env_list =
55
docs
66
lint
7+
mypy
78
py{py3, 313, 312, 311, 310, 39, 38}
89

910
[testenv]
@@ -37,5 +38,14 @@ pass_env =
3738
commands =
3839
pre-commit run --all-files --show-diff-on-failure
3940

41+
[testenv:mypy]
42+
deps =
43+
mypy==1.11.2
44+
pytest
45+
types-freezegun
46+
types-setuptools
47+
commands =
48+
mypy --strict --pretty --show-error-codes . {posargs}
49+
4050
[pytest]
4151
addopts = --color=yes --doctest-modules

0 commit comments

Comments
 (0)