Skip to content

Commit

Permalink
Generate __version__ at build to avoid slow importlib.metadata im…
Browse files Browse the repository at this point in the history
…port (#446)
  • Loading branch information
hugovk authored Aug 6, 2024
2 parents b8ff5d3 + 920596d commit 0c6649e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ on:
permissions:
contents: read

env:
FORCE_COLOR: 1

jobs:
# Always build & lint package.
build-package:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,6 @@ venv.bak/
.mypy_cache/
.dmypy.json
dmypy.json

# hatch-vcs
src/pypistats/_version.py
12 changes: 11 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ scripts.pypistats = "pypistats.cli:main"
[tool.hatch]
version.source = "vcs"

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

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

Expand All @@ -81,20 +84,27 @@ lint.select = [
"EM", # flake8-errmsg
"F", # pyflakes errors
"I", # isort
"ICN", # flake8-import-conventions
"ISC", # flake8-implicit-str-concat
"LOG", # flake8-logging
"PGH", # pygrep-hooks
"PYI", # flake8-pyi
"RUF022", # unsorted-dunder-all
"RUF100", # unused noqa (yesqa)
"UP", # pyupgrade
"W", # pycodestyle warnings
"YTT", # flake8-2020
]
lint.extend-ignore = [
lint.ignore = [
"E203", # Whitespace before ':'
"E221", # Multiple spaces before operator
"E226", # Missing whitespace around arithmetic operator
"E241", # Multiple spaces after ','
]
lint.flake8-import-conventions.aliases.datetime = "dt"
lint.flake8-import-conventions.banned-from = [
"datetime",
]
lint.isort.known-first-party = [
"pypistats",
]
Expand Down
5 changes: 3 additions & 2 deletions src/pypistats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import atexit
import datetime as dt
import importlib.metadata
import json
import sys
import warnings
Expand All @@ -17,7 +16,9 @@
from slugify import slugify
from termcolor import colored

__version__ = importlib.metadata.version(__name__)
from . import _version

__version__ = _version.__version__

BASE_URL = "https://pypistats.org/api/"
CACHE_DIR = Path(user_cache_dir("pypistats"))
Expand Down

0 comments on commit 0c6649e

Please sign in to comment.