Skip to content

Commit

Permalink
Merge branch 'maint/2.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Nov 24, 2020
2 parents 2bc4989 + dd25a38 commit c3421fb
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
12 changes: 12 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
importlib_metadata NEWS
=========================

v3.1.0
======

* Merge with 2.1.0.

v2.1.0
======

* #253: When querying for package metadata, the lookup
now honors
`package normalization rules <https://packaging.python.org/specifications/recording-installed-packages/>`_.

v3.0.0
======

Expand Down
11 changes: 9 additions & 2 deletions importlib_metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ def search(self, name):
for child in self.children():
n_low = child.lower()
if (n_low in name.exact_matches
or n_low.startswith(name.prefix)
or n_low.replace('.', '_').startswith(name.prefix)
and n_low.endswith(name.suffixes)
# legacy case:
or self.is_egg(name) and n_low == 'egg-info'):
Expand All @@ -482,12 +482,19 @@ def __init__(self, name):
self.name = name
if name is None:
return
self.normalized = name.lower().replace('-', '_')
self.normalized = self.normalize(name)
self.prefix = self.normalized + '-'
self.exact_matches = [
self.normalized + suffix for suffix in self.suffixes]
self.versionless_egg_name = self.normalized + '.egg'

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


@install
class MetadataPathFinder(NullFinder, DistributionFinder):
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ testing =
importlib_resources>=1.3; python_version < "3.9"
packaging
pep517
unittest2; python_version < "3"
docs =
sphinx
rst.linker
30 changes: 30 additions & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,36 @@ def setUp(self):
build_files(DistInfoPkg.files, self.site_dir)


class DistInfoPkgWithDot(OnSysPath, SiteDir):
files = {
"pkg_dot-1.0.0.dist-info": {
"METADATA": """
Name: pkg.dot
Version: 1.0.0
""",
},
}

def setUp(self):
super(DistInfoPkgWithDot, self).setUp()
build_files(DistInfoPkgWithDot.files, self.site_dir)


class DistInfoPkgWithDotLegacy(OnSysPath, SiteDir):
files = {
"pkg.dot-1.0.0.dist-info": {
"METADATA": """
Name: pkg.dot
Version: 1.0.0
""",
},
}

def setUp(self):
super(DistInfoPkgWithDotLegacy, self).setUp()
build_files(DistInfoPkgWithDotLegacy.files, self.site_dir)


class DistInfoPkgOffPath(SiteDir):
def setUp(self):
super(DistInfoPkgOffPath, self).setUp()
Expand Down
21 changes: 20 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import re
import textwrap
import unittest

try:
import unittest2 as unittest
except ImportError:
import unittest

from . import fixtures
from importlib_metadata import (
Expand All @@ -12,6 +16,7 @@
class APITests(
fixtures.EggInfoPkg,
fixtures.DistInfoPkg,
fixtures.DistInfoPkgWithDot,
fixtures.EggInfoFile,
unittest.TestCase):

Expand All @@ -31,6 +36,12 @@ def test_for_name_does_not_exist(self):
with self.assertRaises(PackageNotFoundError):
distribution('does-not-exist')

def test_name_normalization(self):
names = 'pkg.dot', 'pkg_dot', 'pkg-dot', 'pkg..dot', 'Pkg.Dot'
for name in names:
with self.subTest(name):
assert distribution(name).metadata['Name'] == 'pkg.dot'

def test_for_top_level(self):
self.assertEqual(
distribution('egginfo-pkg').read_text('top_level.txt').strip(),
Expand Down Expand Up @@ -146,6 +157,14 @@ def test_more_complex_deps_requires_text(self):
assert deps == expected


class LegacyDots(fixtures.DistInfoPkgWithDotLegacy, unittest.TestCase):
def test_name_normalization(self):
names = 'pkg.dot', 'pkg_dot', 'pkg-dot', 'pkg..dot', 'Pkg.Dot'
for name in names:
with self.subTest(name):
assert distribution(name).metadata['Name'] == 'pkg.dot'


class OffSysPathTests(fixtures.DistInfoPkgOffPath, unittest.TestCase):
def test_find_distributions_specified_path(self):
dists = Distribution.discover(path=[str(self.site_dir)])
Expand Down

0 comments on commit c3421fb

Please sign in to comment.