GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(utils): look up package versions without scanning all installed packages by Shellghostt · Pull Request #8215 · getsentry/sentry-python

Conversation

@Shellghostt

@Shellghostt Shellghostt commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Description

package_version() in sentry_sdk/utils.py rebuilt the full installed-package mapping on every call: _get_installed_modules() iterates importlib.metadata.distributions() (parsing every installed distribution's metadata), and package_version additionally re-normalized that whole mapping per call. Integrations call package_version during sentry_sdk.init() to check minimum versions, so in environments with a few hundred installed packages this scan measurably slowed SDK startup.

This change makes package_version look up the single requested distribution with importlib.metadata.version() instead:

  • Top-level guarded from importlib import metadata as importlib_metadata (no inline imports, per the repo guidelines); PackageNotFoundError maps to None, matching the previous behaviour for missing packages.
  • The existing _get_installed_modules() scan remains as the fallback for environments without importlib.metadata (Python < 3.8), so supported old interpreters keep working.
  • Name handling is unchanged: the requested name is PEP 503-normalized first, which is also how importlib.metadata.version() resolves distribution names.

Tests in tests/test_utils.py:

  • test_package_version_installed_package asserts an installed package (pytest) resolves to the same parsed version as importlib.metadata.
  • test_package_version_does_not_scan_all_installed_packages monkeypatches _get_installed_modules to fail if invoked (the only way to assert the scan no longer happens) and asserts both hit and miss lookups still work.

Validation:

  • TESTPATH=tests/test_utils.py uv run tox -e py3.12-common -- -k package_version — 3 passed.
  • uv run ruff check / ruff format --check on the changed files — clean.
  • uv run --group typing mypy sentry_sdk — no issues in 203 files.

Issues

  • resolves: package_version() parses every installed package's metadata, slowing sentry_sdk.init() #8190

Reminders

  • Tests added and linted with uv run ruff as requested.
  • PR title uses conventional-commit style.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Shellghostt
fix(utils): look up package versions without scanning all installed p…
43d6462
…ackages
@Shellghostt
Shellghostt requested a review from a team as a code owner November 8, 2026 02:35
sentrivana
sentrivana approved these changes Nov 9, 2026
View reviewed changes

@sentrivana sentrivana left a comment

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks @Shellghostt!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@sentrivana
sentrivana enabled auto-merge (squash) November 9, 2026 12:52
@sentrivana
sentrivana merged commit 4cf1ee5 into getsentry:master Nov 9, 2026
317 of 319 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@sentrivana sentrivana mentioned this pull request Nov 9, 2026
perf: Avoid scanning installed packages in package_version #8191
Closed
This was referenced Nov 9, 2026
publish: getsentry/[email protected] getsentry/publish#10305
Closed
publish: getsentry/[email protected] getsentry/publish#10388
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@sentrivana sentrivana sentrivana approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

package_version() parses every installed package's metadata, slowing sentry_sdk.init()

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@Shellghostt @sentrivana
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.