Repository navigation
fix(utils): look up package versions without scanning all installed packages - #8215
Open
Shellghostt wants to merge 1 commit into
Open
Shellghostt wants to merge 1 commit into
Shellghostt wants to merge 1 commit into
Conversation
This branch has not been deployed
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.
Description
package_version()insentry_sdk/utils.pyrebuilt the full installed-package mapping on every call:_get_installed_modules()iteratesimportlib.metadata.distributions()(parsing every installed distribution's metadata), andpackage_versionadditionally re-normalized that whole mapping per call. Integrations callpackage_versionduringsentry_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_versionlook up the single requested distribution withimportlib.metadata.version()instead:from importlib import metadata as importlib_metadata(no inline imports, per the repo guidelines);PackageNotFoundErrormaps toNone, matching the previous behaviour for missing packages._get_installed_modules()scan remains as the fallback for environments withoutimportlib.metadata(Python < 3.8), so supported old interpreters keep working.importlib.metadata.version()resolves distribution names.Tests in
tests/test_utils.py:test_package_version_installed_packageasserts an installed package (pytest) resolves to the same parsed version asimportlib.metadata.test_package_version_does_not_scan_all_installed_packagesmonkeypatches_get_installed_modulesto 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 --checkon the changed files — clean.uv run --group typing mypy sentry_sdk— no issues in 203 files.Issues
package_version()parses every installed package's metadata, slowingsentry_sdk.init()#8190Reminders
uv run ruffas requested.