Repository navigation
Commit 4cf1ee5
authored
fix(utils): look up package versions without scanning all installed packages (#8215)
### 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: #8190
#### Reminders
- Tests added and linted with `uv run ruff` as requested.
- PR title uses conventional-commit style.
Co-authored-by: Adityaaa <144815351+addydude@users.noreply.github.com>1 parent 05c32d3 commit 4cf1ee5
2 files changed
Lines changed: 31 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
31 | 36 | | |
32 | 37 | | |
33 | 38 | | |
| |||
1841 | 1846 | | |
1842 | 1847 | | |
1843 | 1848 | | |
| 1849 | + | |
| 1850 | + | |
| 1851 | + | |
| 1852 | + | |
| 1853 | + | |
| 1854 | + | |
| 1855 | + | |
| 1856 | + | |
1844 | 1857 | | |
1845 | 1858 | | |
1846 | 1859 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1412 | 1412 | | |
1413 | 1413 | | |
1414 | 1414 | | |
| 1415 | + | |
| 1416 | + | |
| 1417 | + | |
| 1418 | + | |
| 1419 | + | |
| 1420 | + | |
| 1421 | + | |
| 1422 | + | |
| 1423 | + | |
| 1424 | + | |
| 1425 | + | |
| 1426 | + | |
| 1427 | + | |
| 1428 | + | |
| 1429 | + | |
| 1430 | + | |
| 1431 | + | |
| 1432 | + | |
1415 | 1433 | | |
1416 | 1434 | | |
1417 | 1435 | | |
| |||
0 commit comments