Skip to content

Commit

Permalink
Speedup x 1.85: Generate __version__ at build to avoid slow `import…
Browse files Browse the repository at this point in the history
…lib.metadata` import (#200)
  • Loading branch information
hugovk authored Sep 9, 2024
2 parents a72a36d + 6fe2efe commit 74dcc60
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 12 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on: [push, pull_request, workflow_dispatch]

env:
FORCE_COLOR: 1
PIP_DISABLE_PIP_VERSION_CHECK: 1

permissions:
contents: read
Expand All @@ -17,4 +18,12 @@ jobs:
- uses: actions/setup-python@v5
with:
python-version: "3.x"
cache: pip
- uses: pre-commit/action@v3.0.1

- name: Install dependencies
run: |
python3 -m pip install -U tox
- name: Mypy
run: tox -e mypy
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 0 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@ repos:
hooks:
- id: actionlint

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.11.2
hooks:
- id: mypy
additional_dependencies: [pytest, types-freezegun, types-setuptools]
args: [--strict, --pretty, --show-error-codes, .]
pass_filenames: false

- repo: https://github.com/tox-dev/pyproject-fmt
rev: 2.2.3
hooks:
Expand Down
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -105,3 +108,8 @@ filterwarnings = [
"ignore:sys.monitoring isn't available, using default core:coverage.exceptions.CoverageWarning",
]
testpaths = [ "tests" ]

[tool.mypy]
pretty = true
strict = true
show_error_codes = true
5 changes: 1 addition & 4 deletions src/humanize/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -24,8 +22,7 @@
precisedelta,
)

__version__ = importlib.metadata.version(__name__)

from ._version import __version__

__all__ = [
"__version__",
Expand Down
10 changes: 10 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ requires =
env_list =
docs
lint
mypy
py{py3, 313, 312, 311, 310, 39, 38}

[testenv]
Expand Down Expand Up @@ -37,5 +38,14 @@ pass_env =
commands =
pre-commit run --all-files --show-diff-on-failure

[testenv:mypy]
deps =
mypy==1.11.2
pytest
types-freezegun
types-setuptools
commands =
mypy --strict --pretty --show-error-codes . {posargs}

[pytest]
addopts = --color=yes --doctest-modules

0 comments on commit 74dcc60

Please sign in to comment.