Skip to content

Commit a48a3bb

Browse files
author
Roman Inflianskas
authored
Fix distribution name normalization (PEP-0503) (#2144)
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 a48a3bb

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

tests/integrations/modules/test_modules.py

+22-16
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1+
import re
12
import sentry_sdk
23

34
from sentry_sdk.integrations.modules import (
45
ModulesIntegration,
56
_get_installed_modules,
6-
_normalize_module_name,
77
)
88

99

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

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)
47+
installed_distributions = {
48+
_normalize_distribution_name(dist): version
49+
for dist, version in _get_installed_modules().items()
50+
}
4351

4452
if importlib_available:
45-
importlib_modules = {
46-
_normalize_module_name(dist.metadata["Name"]): version(
53+
importlib_distributions = {
54+
_normalize_distribution_name(dist.metadata["Name"]): version(
4755
dist.metadata["Name"]
4856
)
4957
for dist in distributions()
5058
}
51-
importlib_modules.pop("typing-extensions", None)
52-
assert installed_modules == importlib_modules
59+
assert installed_distributions == importlib_distributions
5360

5461
if pkg_resources_available:
55-
pkg_resources_modules = {
56-
_normalize_module_name(dist.key): dist.version
62+
pkg_resources_distributions = {
63+
_normalize_distribution_name(dist.key): dist.version
5764
for dist in pkg_resources.working_set
5865
}
59-
pkg_resources_modules.pop("typing-extensions", None)
60-
assert installed_modules == pkg_resources_modules
66+
assert installed_distributions == pkg_resources_distributions

0 commit comments

Comments
 (0)