Skip to content

Commit a6fc449

Browse files
author
Roman Inflianskas
committed
Fix distribution name normalization (PEP-0503)
Current logic in `test_installed_modules` does not properly handle distributions with underscores. On my machine I get the following error while running tests: ``` tests/integrations/modules/test_modules.py:60: in test_installed_modules assert installed_modules == pkg_resources_modules E AssertionError: assert {'aiven-clien...'22.2.0', ...} == {'aiven-clien...'22.2.0', ...} E Omitting 93 identical items, use -vv to show E Left contains 1 more item: E {'tomli_w': '1.0.0'} E Right contains 1 more item: E {'tomli-w': '1.0.0'} E Use -v to get more diff ``` This change fixes distribution name normalization by applying the code from PEP-0503 (https://peps.python.org/pep-0503/#normalized-names).
1 parent 72f1e92 commit a6fc449

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

tests/integrations/modules/test_modules.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
import sentry_sdk
23

34
from sentry_sdk.integrations.modules import (
@@ -7,6 +8,17 @@
78
)
89

910

11+
def _normalize_distribution_name(name):
12+
# type: (str) -> str
13+
"""Normalize distribution name according to PEP-0503.
14+
15+
See:
16+
https://peps.python.org/pep-0503/#normalized-names
17+
for more details.
18+
"""
19+
return re.sub(r"[-_.]+", "-", name).lower()
20+
21+
1022
def test_basic(sentry_init, capture_events):
1123
sentry_init(integrations=[ModulesIntegration()])
1224
events = capture_events()
@@ -33,28 +45,23 @@ def test_installed_modules():
3345
except ImportError:
3446
pkg_resources_available = False
3547

36-
installed_modules = _get_installed_modules()
37-
38-
# This one package is reported differently by importlib
39-
# and pkg_resources, but we don't really care, so let's
40-
# just ignore it
41-
installed_modules.pop("typing-extensions", None)
42-
installed_modules.pop("typing_extensions", None)
48+
installed_distributions = {
49+
_normalize_distribution_name(dist): version
50+
for dist, version in _get_installed_modules().items()
51+
}
4352

4453
if importlib_available:
45-
importlib_modules = {
46-
_normalize_module_name(dist.metadata["Name"]): version(
54+
importlib_distributions = {
55+
_normalize_distribution_name(dist.metadata["Name"]): version(
4756
dist.metadata["Name"]
4857
)
4958
for dist in distributions()
5059
}
51-
importlib_modules.pop("typing-extensions", None)
52-
assert installed_modules == importlib_modules
60+
assert installed_distributions == importlib_distributions
5361

5462
if pkg_resources_available:
55-
pkg_resources_modules = {
56-
_normalize_module_name(dist.key): dist.version
63+
pkg_resources_distributions = {
64+
_normalize_distribution_name(dist.key): dist.version
5765
for dist in pkg_resources.working_set
5866
}
59-
pkg_resources_modules.pop("typing-extensions", None)
60-
assert installed_modules == pkg_resources_modules
67+
assert installed_distributions == pkg_resources_distributions

0 commit comments

Comments
 (0)