diff --git a/mise.toml b/mise.toml index 3780608..dbe718b 100644 --- a/mise.toml +++ b/mise.toml @@ -55,12 +55,12 @@ run = "uv lock --upgrade" [tasks.lint] description = "Lint and format code" alias = "l" -run = "ruff check --fix . && ruff format ." +run = "uv run sh -c 'ruff check --fix . && ruff format .'" [tasks.test] description = "Run tests with coverage" alias = "t" -run = "coverage run -m pytest . && coverage report -m && coverage xml" +run = "uv run sh -c 'coverage run -m pytest . && coverage report -m && coverage xml'" [tasks.all] description = "Full setup from scratch" diff --git a/project/app.py b/project/app.py index c8e543c..7a50b36 100644 --- a/project/app.py +++ b/project/app.py @@ -1,6 +1,33 @@ from click import command, option, secho, version_option +class _LazyVersion: + """ + Retrieve the project version from pyproject.toml lazily. + + This is a performance optimization to avoid the overhead of importlib.metadata + and to ensure the version is available even if the package is not installed. + """ + + def __str__(self) -> str: + import re + from pathlib import Path + + try: + content = (Path(__file__).parent.parent / "pyproject.toml").read_text() + if match := re.search(r'^version\s*=\s*"(.*?)"', content, re.MULTILINE): + return match.group(1) + except Exception: # noqa: S110 # pragma: no cover + pass + + try: + import importlib.metadata + + return importlib.metadata.version("project") + except Exception: # pragma: no cover + return "0.0.0" + + @command( context_settings={"help_option_names": ["-h", "--help"]}, help="Say hello to a user.", @@ -14,7 +41,7 @@ show_default=True, metavar="", ) -@version_option() +@version_option(version=_LazyVersion()) def main(name: str = "World"): """ Say hello to the given name.