From 34f8f86ef2a487198c011ebe3bef1e41632d74a3 Mon Sep 17 00:00:00 2001 From: Julian Stirling Date: Mon, 14 Jul 2025 15:46:13 +0000 Subject: [PATCH] Apply suggestions from code review of branch update-setting-manager Co-authored-by: Joe Knapper Co-authored-by: Beth Probert --- src/openflexure_microscope_server/utilities.py | 14 ++++++++------ tests/test_version_strings.py | 4 ++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/openflexure_microscope_server/utilities.py b/src/openflexure_microscope_server/utilities.py index 13d6ee23..60a2fc5b 100644 --- a/src/openflexure_microscope_server/utilities.py +++ b/src/openflexure_microscope_server/utilities.py @@ -104,7 +104,7 @@ class VersionData(BaseModel): version_source: str -def robust_version_strings() -> tuple[str, str]: +def robust_version_strings() -> VersionData: """Return a version string and information on its source. Returning a python version string is not without problems. If the package is @@ -118,7 +118,7 @@ def robust_version_strings() -> tuple[str, str]: installation from source. Return the version string from the toml file and return the literal string "TOML" as its source. 3. If a Git directory is available but no pyproject.toml then something is very - weird log an error. Then return "Undefined" as the version string, but still + weird - log an error. Then return "Undefined" as the version string, but still return the Git hash as the source. 4. Finally if neither a Git directory nor a pyproject.toml are available then the server has been installed from a distribution package (i.e. a wheel). In this @@ -152,9 +152,10 @@ def robust_version_strings() -> tuple[str, str]: ver = "Undefined" source = _get_hash_from_git_dir(git_dir_path) else: - # "Neither probably installed from wheel. Note this can - # be unreliable if the package is not installed as a distribution. - # This is why it is the last option. + # Has neither .git nor pyproject.toml. It is probably installed from wheel. + # This will be the expected method of packaging in the future. This option + # is handled last as ``importlib.metadata.version`` can be unreliable if + # the package is no installed as a distribution package. ver = "v" + version("openflexure_microscope_server") source = "Dist" return VersionData(version=ver, version_source=source) @@ -222,7 +223,8 @@ def _get_version_from_toml(toml_path: str) -> str: :param toml_path: The path to the toml file. - :returns: The version string directly as reported in the toml file. + :returns: The version string directly as reported in the toml file, or "Undefined" + if there is any error, errors are logged not raised. """ try: with open(toml_path, "rb") as toml_file: diff --git a/tests/test_version_strings.py b/tests/test_version_strings.py index 121bb065..19d3a74c 100644 --- a/tests/test_version_strings.py +++ b/tests/test_version_strings.py @@ -80,7 +80,7 @@ def git_repo(temp_dir): create_fake_file() create_fake_file() _git("add -A") - _git("commit -m 'message'") + _git("commit -m 'message2'") yield temp_dir @@ -175,7 +175,7 @@ def test_reading_hash_from_git(): create_fake_file() create_fake_file() _git("add -A") - _git("commit -m 'message'") + _git("commit -m 'message2'") git_hash2 = _git("rev-parse HEAD") assert utilities._get_hash_from_git_dir(git_dir) == git_hash2