From ac53cea3590708ea733df95076fa1aea0977d055 Mon Sep 17 00:00:00 2001 From: jgart Date: Tue, 25 Jul 2023 18:27:59 -0500 Subject: [PATCH 1/4] feat: Add `--version` and `-V` cli flag Issue #: https://github.com/mkdocstrings/griffe/issues/186 PR #174: https://github.com/mkdocstrings/griffe/pull/187 --- src/griffe/cli.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/griffe/cli.py b/src/griffe/cli.py index bb195408..3dce3b0e 100644 --- a/src/griffe/cli.py +++ b/src/griffe/cli.py @@ -19,6 +19,7 @@ import os import sys from datetime import datetime, timezone +from importlib.metadata import version as get_version from pathlib import Path from typing import IO, TYPE_CHECKING, Any, Callable, Sequence @@ -34,6 +35,8 @@ from griffe.logger import get_logger from griffe.stats import _format_stats +version = get_version("griffe") + if TYPE_CHECKING: from griffe.extensions import Extensions, ExtensionType @@ -122,6 +125,7 @@ def get_parser() -> argparse.ArgumentParser: global_options = parser.add_argument_group(title="Global options") global_options.add_argument("-h", "--help", action="help", help=main_help) + global_options.add_argument("-V", "--version", action="version", version="%(prog)s " + version) def add_common_options(subparser: argparse.ArgumentParser) -> None: common_options = subparser.add_argument_group(title="Common options") From 4022d8eb6ebdc161a930ff3079f6517b5fa1a122 Mon Sep 17 00:00:00 2001 From: jgart Date: Wed, 26 Jul 2023 08:39:28 -0500 Subject: [PATCH 2/4] docs: Fix typo --- src/griffe/c3linear.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/griffe/c3linear.py b/src/griffe/c3linear.py index 31282a77..96b1e2e4 100644 --- a/src/griffe/c3linear.py +++ b/src/griffe/c3linear.py @@ -25,7 +25,7 @@ def head(self) -> T | None: @property def tail(self) -> islice: - """Tail od the dependency. + """Tail of the dependency. The `islice` object is sufficient for iteration or testing membership (`in`). """ From 51b8512dbfd2b3c1ef741a2bcd8e21251ae5cba8 Mon Sep 17 00:00:00 2001 From: jgart Date: Wed, 26 Jul 2023 08:54:57 -0500 Subject: [PATCH 3/4] docs: Improve docstring --- src/griffe/mixins.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/griffe/mixins.py b/src/griffe/mixins.py index a56e1d2b..42eb30ae 100644 --- a/src/griffe/mixins.py +++ b/src/griffe/mixins.py @@ -247,7 +247,7 @@ def as_json(self, *, full: bool = False, **kwargs: Any) -> str: **kwargs: Additional serialization options passed to encoder. Returns: - A string. + A JSON string. """ from griffe.encoders import JSONEncoder # avoid circular import From 0493f05a2f1b47e0362a6a689eae3c77586faa32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Wed, 2 Aug 2023 16:43:59 +0200 Subject: [PATCH 4/4] fixup! feat: Add `--version` and `-V` cli flag --- src/griffe/cli.py | 14 ++++++++++---- tests/test_inspector.py | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/griffe/cli.py b/src/griffe/cli.py index 3dce3b0e..b2a4614a 100644 --- a/src/griffe/cli.py +++ b/src/griffe/cli.py @@ -19,7 +19,8 @@ import os import sys from datetime import datetime, timezone -from importlib.metadata import version as get_version +from importlib.metadata import PackageNotFoundError +from importlib.metadata import version as get_dist_version from pathlib import Path from typing import IO, TYPE_CHECKING, Any, Callable, Sequence @@ -35,8 +36,6 @@ from griffe.logger import get_logger from griffe.stats import _format_stats -version = get_version("griffe") - if TYPE_CHECKING: from griffe.extensions import Extensions, ExtensionType @@ -45,6 +44,13 @@ logger = get_logger(__name__) +def _get_griffe_version() -> str: + try: + return get_dist_version("griffe") + except PackageNotFoundError: + return "0.0.0" + + def _print_data(data: str, output_file: str | IO | None) -> None: if isinstance(output_file, str): with open(output_file, "w") as fd: @@ -125,7 +131,7 @@ def get_parser() -> argparse.ArgumentParser: global_options = parser.add_argument_group(title="Global options") global_options.add_argument("-h", "--help", action="help", help=main_help) - global_options.add_argument("-V", "--version", action="version", version="%(prog)s " + version) + global_options.add_argument("-V", "--version", action="version", version="%(prog)s " + _get_griffe_version()) def add_common_options(subparser: argparse.ArgumentParser) -> None: common_options = subparser.add_argument_group(title="Common options") diff --git a/tests/test_inspector.py b/tests/test_inspector.py index f10aba4c..cd603fae 100644 --- a/tests/test_inspector.py +++ b/tests/test_inspector.py @@ -49,7 +49,7 @@ def test_missing_dependency() -> None: with temporary_pypackage("package", ["module.py"]) as tmp_package: filepath = Path(tmp_package.path, "module.py") filepath.write_text("import missing") - with pytest.raises(ImportError): # noqa: PT012,SIM117 + with pytest.raises(ImportError): # noqa: SIM117 with suppress(ModuleNotFoundError): inspect("package.module", filepath=filepath, import_paths=[tmp_package.tmpdir])