Repository navigation
fix: narrow bare except Exception in version fallback - #3835
Quratulain-bilal wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
Narrows exception handling in the Spec Kit version fallback.
Changes:
- Handles missing package metadata explicitly.
- Limits project-file errors to expected exception types.
Show a summary per file
| File | Description |
|---|---|
src/specify_cli/_assets.py |
Narrows version lookup fallback exceptions. |
Review details
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 1/1 changed files
- Comments generated: 1
- Review effort level: Medium
| try: | ||
| return importlib.metadata.version("specify-cli") | ||
| except Exception: | ||
| except importlib.metadata.PackageNotFoundError: |
Replace overly broad except Exception with specific exception types: - importlib.metadata.PackageNotFoundError for missing package - (OSError, KeyError, ValueError) for pyproject.toml read/parse errors This prevents silently swallowing unexpected errors like AttributeError or RecursionError from broken tomllib or malformed pyproject.toml.
fdb5353 to
56ea7d4
Compare
|
This pull request has been automatically marked as stale because it has had no activity for 60 days. It will be closed in 30 days unless there is a substantive update. If you intend to continue, please describe the current status, address or acknowledge outstanding feedback, and confirm whether the branch can be updated and the change remains ready for review. A comment that only states that the pull request is "still relevant" does not provide enough context for maintainers. |
…allback importlib.metadata.version() can raise InvalidMetadataError for a malformed installed distribution, which is not a PackageNotFoundError, so the narrowed handler let it escape get_speckit_version() instead of using the pyproject/unknown fallback. Build the guarded exception tuple before the lookup, mirroring _version._get_installed_version(). The narrowed pyproject branch also called .get() on whatever the project key held, so a non-mapping value turned the fallback into an AttributeError; read it through isinstance checks instead. Regression tests cover both paths (corrupt metadata and a non-mapping project table) plus the pyproject fallback itself. Both new tests fail on the previous implementation: one with the escaping InvalidMetadataError, the other with AttributeError: 'int' object has no attribute 'get'. Assisted-by: opencode (model: mimo-v2.6-flash-free, autonomous)
|
Please address Copilot feedback |
Adds tests that FAIL against the previous bare except Exception implementation and PASS after the narrowing: - test_unrelated_exception_from_version_lookup_propagates: TypeError from importlib.metadata.version() now propagates - test_unrelated_exception_from_pyproject_propagates: RuntimeError from pyproject parsing now propagates - test_pyproject_io_error_returns_unknown: OSError still caught (intended path preserved) Assisted-by: GitHub Copilot (autonomous)
|
Hi @mnriem — I've addressed Copilot's feedback: added before/after regression evidence that fails against the previous bare except Exception implementation:
All 9 tests pass. Ready for re-review when you get a chance. |


Summary
Replace overly broad
except Exceptionwith specific exception types inget_speckit_version()fallback.Changes
_assets.py: Narrow outer exception toPackageNotFoundError_assets.py: Narrow inner exception to(OSError, KeyError, ValueError)