Skip to content

fix: handle PackageNotFoundError when commit-check is not installed - #483

Merged
shenxianpeng merged 2 commits into
mainfrom
bugfix/fix-version-crash-uninstalled
Jul 29, 2026
Merged

fix: handle PackageNotFoundError when commit-check is not installed#483
shenxianpeng merged 2 commits into
mainfrom
bugfix/fix-version-crash-uninstalled

Conversation

@shenxianpeng

@shenxianpeng shenxianpeng commented Jul 29, 2026

Copy link
Copy Markdown
Member

Summary

When importing commit_check in a development environment where the package hasn't been pip install-ed (e.g., running directly with uv or python -m), importlib.metadata.version("commit-check") raises PackageNotFoundError, causing the entire import to crash.

Changes

  • Import PackageNotFoundError alongside version from importlib.metadata
  • Wrap __version__ assignment in a try/except PackageNotFoundError
  • Fall back to "0.0.0.dev" when the package is not installed

Tests

Added tests/version_test.py with two test cases:

  1. test_version_is_string_when_installed — verifies __version__ is a non-empty string under normal conditions
  2. test_version_fallback_when_not_installed — mocks importlib.metadata.version to raise PackageNotFoundError, then reloads the module and asserts __version__ == "0.0.0.dev"

Verification

  • import commit_check no longer crashes in an uninstalled (development) environment
  • __version__ resolves correctly when the package is installed
  • All existing tests and the two new tests pass

Wrap __version__ in try/except so that importing commit-check
does not crash in development environments where the package
has not been pip installed (e.g. running directly via uv or
python -m). Falls back to '0.0.0.dev'.
@shenxianpeng
shenxianpeng requested a review from a team as a code owner July 29, 2026 20:15
@netlify

netlify Bot commented Jul 29, 2026

Copy link
Copy Markdown

Deploy Preview for commit-check ready!

Name Link
🔨 Latest commit 5b94a42
🔍 Latest deploy log https://app.netlify.com/projects/commit-check/deploys/6a6a60c1e9bd2e0008f0be45
😎 Deploy Preview https://deploy-preview-483--commit-check.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions github-actions Bot added the bug Something isn't working label Jul 29, 2026
@coderabbitai

coderabbitai Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@shenxianpeng, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 54 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 71fa37b5-5bf8-41f7-b866-90151769b53f

📥 Commits

Reviewing files that changed from the base of the PR and between e12ced7 and 5b94a42.

📒 Files selected for processing (1)
  • tests/version_test.py
📝 Walkthrough

Walkthrough

The package version lookup now catches missing distribution metadata and falls back to "0.0.0.dev" during module import.

Changes

Package version initialization

Layer / File(s) Summary
Guarded version initialization
commit_check/__init__.py
Imports PackageNotFoundError and assigns __version__ from package metadata, falling back to "0.0.0.dev" when metadata is unavailable.

Estimated code review effort: 2 (Simple) | ~5 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: handling PackageNotFoundError during commit_check version lookup when the package isn't installed.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bugfix/fix-version-crash-uninstalled

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Jul 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.29%. Comparing base (5a3dd93) to head (5b94a42).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #483      +/-   ##
==========================================
- Coverage   97.45%   97.29%   -0.17%     
==========================================
  Files          12       12              
  Lines        1179     1182       +3     
==========================================
+ Hits         1149     1150       +1     
- Misses         30       32       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions github-actions Bot added the tests Add test related changes label Jul 29, 2026
@sonarqubecloud

Copy link
Copy Markdown

@shenxianpeng shenxianpeng removed the tests Add test related changes label Jul 29, 2026
@shenxianpeng
shenxianpeng merged commit ad606e1 into main Jul 29, 2026
31 of 32 checks passed
@shenxianpeng
shenxianpeng deleted the bugfix/fix-version-crash-uninstalled branch July 29, 2026 20:28
@codspeed-hq

codspeed-hq Bot commented Jul 29, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 380 untouched benchmarks
🆕 2 new benchmarks
⏩ 114 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
🆕 test_version_fallback_when_not_installed N/A 3.9 ms N/A
🆕 test_version_is_string_when_installed N/A 100.2 µs N/A

Comparing bugfix/fix-version-crash-uninstalled (5b94a42) with main (5a3dd93)

Open in CodSpeed

Footnotes

  1. 114 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant