diff --git a/dargs/cli.py b/dargs/cli.py index 3273463..1f1429a 100644 --- a/dargs/cli.py +++ b/dargs/cli.py @@ -3,9 +3,29 @@ import argparse import json import sys +from importlib import import_module from typing import IO, Any -from dargs._version import __version__ +try: + from dargs._version import __version__ +except ModuleNotFoundError as exc: + if exc.name != "dargs._version": + raise + try: + get_version = getattr(import_module("setuptools_scm"), "get_version") + except ModuleNotFoundError as scm_exc: + if scm_exc.name != "setuptools_scm": + raise + # Source archives may have neither the generated module nor build tools. + # The CLI should remain usable even when an exact version is unavailable. + __version__ = "0+unknown" + else: + __version__ = get_version( + root="..", + relative_to=__file__, + fallback_version="0+unknown", + ) + from dargs.check import check diff --git a/tests/test_cli.py b/tests/test_cli.py index 25ec787..1217d16 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -9,6 +9,16 @@ class TestCli(unittest.TestCase): + def test_version_with_python_module(self) -> None: + """The source checkout exposes a version without generated build files.""" + result = subprocess.run( + [sys.executable, "-m", "dargs", "--version"], + capture_output=True, + text=True, + check=True, + ) + self.assertTrue(result.stdout.strip()) + def test_check(self) -> None: subprocess.check_call( [