Skip to content

Commit dbdef6b

Browse files
committed
Capture the target expectation of normalized names.
1 parent d027af6 commit dbdef6b

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

importlib_metadata/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ def search(self, name):
473473
for child in self.children():
474474
n_low = child.lower()
475475
if (n_low in name.exact_matches
476-
or n_low.replace('.', '_').startswith(name.prefix)
476+
or n_low.startswith(name.prefix)
477477
and n_low.endswith(name.suffixes)
478478
# legacy case:
479479
or self.is_egg(name) and n_low == 'egg-info'):
@@ -494,12 +494,19 @@ def __init__(self, name):
494494
self.name = name
495495
if name is None:
496496
return
497-
self.normalized = name.lower().replace('-', '_')
497+
self.normalized = self.normalize(name)
498498
self.prefix = self.normalized + '-'
499499
self.exact_matches = [
500500
self.normalized + suffix for suffix in self.suffixes]
501501
self.versionless_egg_name = self.normalized + '.egg'
502502

503+
@staticmethod
504+
def normalize(name):
505+
"""
506+
PEP 503 normalization plus dashes as underscores.
507+
"""
508+
return re.sub(r"[-_.]+", "-", name).lower().replace('-', '_')
509+
503510

504511
@install
505512
class MetadataPathFinder(NullFinder, DistributionFinder):

tests/fixtures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def setUp(self):
106106

107107
class DistInfoPkgWithDot(OnSysPath, SiteDir):
108108
files = {
109-
"pkg.dot-1.0.0.dist-info": {
109+
"pkg_dot-1.0.0.dist-info": {
110110
"METADATA": """
111111
Name: pkg.dot
112112
Version: 1.0.0

tests/test_api.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ def test_for_name_does_not_exist(self):
4242
with self.assertRaises(PackageNotFoundError):
4343
distribution('does-not-exist')
4444

45-
def test_for_name_containing_dot(self):
46-
assert distribution('pkg-dot').metadata['Name'] == 'pkg.dot'
45+
def test_name_normalization(self):
46+
names = 'pkg.dot', 'pkg_dot', 'pkg-dot', 'pkg..dot', 'Pkg.Dot'
47+
for name in names:
48+
with self.subTest(name):
49+
assert distribution(name).metadata['Name'] == 'pkg.dot'
4750

4851
def test_for_top_level(self):
4952
self.assertEqual(

0 commit comments

Comments
 (0)