Skip to content

Fix CLI crashing on every invocation when package metadata is missing - #174

Merged
ltorres6 merged 2 commits into
OSIPI:mainfrom
bhuvan-somisetty:fix-cli-version-crash-173
Aug 30, 2026
Merged

Fix CLI crashing on every invocation when package metadata is missing#174
ltorres6 merged 2 commits into
OSIPI:mainfrom
bhuvan-somisetty:fix-cli-version-crash-173

Conversation

@bhuvan-somisetty

Copy link
Copy Markdown
Contributor

Fixes #173

create_parser() built --version as version=f"osipy {_get_version()}", an f-string, so _get_version() (which calls importlib.metadata.version("osipy")) ran immediately while the parser was being constructed, not lazily when --version was actually passed. main() calls create_parser() unconditionally as its first step, so any environment where osipy isn't installed with discoverable package metadata, a plain source checkout without pip install -e ., PYTHONPATH-based execution on HPC, a container that only copies the osipy/ source dir, crashed the entire CLI with an unhandled PackageNotFoundError. Not just --version, every invocation.

This wasn't hypothetical either, it's exactly why tests/unit/cli/test_config.py and tests/unit/cli/test_wizard.py were failing in a plain checkout before this fix, and none of those failing tests even touch --version.

Fix

_get_version() now catches PackageNotFoundError and returns a fallback string instead of raising. Minimal change, single function.

Testing

  • Added TestVersionLookupRegression in tests/unit/cli/test_config.py: confirms _get_version() falls back cleanly, create_parser() no longer raises, and --version still exits 0 with a fallback string when metadata lookup fails.
  • pytest tests/unit/cli/ → 175 passed (previously 8 of these failed with PackageNotFoundError in this environment; all pass now).
  • Full suite: 786 passed, 40 skipped (GPU/data-dependent), 6 xfailed, 0 failed.
  • ruff check / ruff format --check clean on both changed files.
  • mypy osipy/cli/main.py clean, no errors on the changed function.

create_parser() built --version as version=f"osipy {_get_version()}",
an f-string, so _get_version() ran immediately while the parser was
being constructed rather than lazily when --version was actually
passed. Since main() calls create_parser() unconditionally, any
environment where osipy isn't installed with discoverable package
metadata (source checkout without pip install -e ., PYTHONPATH-based
execution, a container that only copies the osipy/ source dir) made
the entire CLI crash with an unhandled PackageNotFoundError, not just
--version.

_get_version() now catches PackageNotFoundError and falls back to a
placeholder string instead of raising.

Fixes OSIPI#173
@bhuvan-somisetty

Copy link
Copy Markdown
Contributor Author

cc @ltorres6 @MohamedNasser8 for review

@ltorres6 ltorres6 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution and for identifying this bug.

The ground truth for the version of the package is stored in osipy/_version.py, so the proper fix here is to switch the _get_version() function to use from osipy._version import __version__.

This also eliminates the need for any monkey patching and should simplify your unit-tests.

If you make those changes I am happy to re-review.

@ltorres6
ltorres6 self-requested a review August 30, 2026 20:09
@ltorres6
ltorres6 merged commit 0251e9b into OSIPI:main Aug 30, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] osipy CLI crashes on every invocation when package metadata isn't discoverable, not just --version

2 participants