Skip to content

Commit

Permalink
Generate __version__ at build to avoid slow importlib.metadata ca…
Browse files Browse the repository at this point in the history
…ll (#321)
  • Loading branch information
hugovk authored Oct 15, 2024
1 parent 442192f commit f1980c0
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 21 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,13 @@ jobs:
python-version: "3.x"
cache: pip
- uses: pre-commit/action@v3.0.1

mypy:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Install uv
uses: hynek/setup-cached-uv@v2
- name: Mypy
run: uvx --with tox-uv tox -e mypy
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,6 @@ dmypy.json

# Pyre type checker
.pyre/

# 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 @@ -34,14 +34,6 @@ repos:
hooks:
- id: actionlint

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.11.2
hooks:
- id: mypy
additional_dependencies: [types-colorama]
args: [--pretty, --show-error-codes]
exclude: ^tests/

- repo: https://github.com/tox-dev/pyproject-fmt
rev: 2.2.4
hooks:
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ urls.Source = "https://github.com/jazzband/prettytable"
[tool.hatch]
version.source = "vcs"

[tool.hatch.build.hooks.vcs]
version-file = "src/prettytable/_version.py"

[tool.hatch.version.raw-options]
local_scheme = "no-local-version"

Expand Down
14 changes: 2 additions & 12 deletions src/prettytable/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

from typing import Any

from ._version import __version__
from .prettytable import (
ALL,
DEFAULT,
Expand Down Expand Up @@ -44,14 +43,5 @@
"from_html",
"from_html_one",
"from_json",
"__version__",
]


def __getattr__(name: str) -> Any:
if name == "__version__":
import importlib.metadata

return importlib.metadata.version(__name__)

msg = f"module '{__name__}' has no attribute '{name}'"
raise AttributeError(msg)
2 changes: 1 addition & 1 deletion src/prettytable/prettytable.py
Original file line number Diff line number Diff line change
Expand Up @@ -2545,7 +2545,7 @@ def _get_formatted_latex_string(self, options) -> str:


def _str_block_width(val: str) -> int:
import wcwidth # type: ignore[import-not-found]
import wcwidth # type: ignore[import-untyped]

return wcwidth.wcswidth(_re.sub("", val))

Expand Down
8 changes: 8 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ requires =
tox>=4.2
env_list =
lint
mypy
py{py3, 313, 312, 311, 310, 39}

[testenv]
Expand All @@ -27,3 +28,10 @@ pass_env =
PRE_COMMIT_COLOR
commands =
pre-commit run --all-files --show-diff-on-failure

[testenv:mypy]
deps =
mypy==1.11.2
types-colorama
commands =
mypy . {posargs} --exclude tests

0 comments on commit f1980c0

Please sign in to comment.