Fix CLI crashing on every invocation when package metadata is missing - #174
Merged
Merged
Conversation
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
Contributor
Author
|
cc @ltorres6 @MohamedNasser8 for review |
ltorres6
requested changes
Aug 30, 2026
ltorres6
left a comment
Collaborator
There was a problem hiding this comment.
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
self-requested a review
August 30, 2026 20:09
ltorres6
approved these changes
Aug 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #173
create_parser()built--versionasversion=f"osipy {_get_version()}", an f-string, so_get_version()(which callsimportlib.metadata.version("osipy")) ran immediately while the parser was being constructed, not lazily when--versionwas actually passed.main()callscreate_parser()unconditionally as its first step, so any environment where osipy isn't installed with discoverable package metadata, a plain source checkout withoutpip install -e ., PYTHONPATH-based execution on HPC, a container that only copies theosipy/source dir, crashed the entire CLI with an unhandledPackageNotFoundError. Not just--version, every invocation.This wasn't hypothetical either, it's exactly why
tests/unit/cli/test_config.pyandtests/unit/cli/test_wizard.pywere failing in a plain checkout before this fix, and none of those failing tests even touch--version.Fix
_get_version()now catchesPackageNotFoundErrorand returns a fallback string instead of raising. Minimal change, single function.Testing
TestVersionLookupRegressionintests/unit/cli/test_config.py: confirms_get_version()falls back cleanly,create_parser()no longer raises, and--versionstill exits 0 with a fallback string when metadata lookup fails.pytest tests/unit/cli/→ 175 passed (previously 8 of these failed withPackageNotFoundErrorin this environment; all pass now).ruff check/ruff format --checkclean on both changed files.mypy osipy/cli/main.pyclean, no errors on the changed function.