Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mise.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
29 changes: 28 additions & 1 deletion project/app.py
Original file line number Diff line number Diff line change
@@ -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.",
Expand All @@ -14,7 +41,7 @@
show_default=True,
metavar="<name>",
)
@version_option()
@version_option(version=_LazyVersion())
def main(name: str = "World"):
"""
Say hello to the given name.
Expand Down