Skip to content

Commit 96ddc58

Browse files
authored
bpo-42089: Sync with current cpython branch of importlib_metadata (GH-22775)
~~The only differences are in the test files.~~ Automerge-Triggered-By: @jaraco
1 parent 95ad890 commit 96ddc58

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

Lib/importlib/metadata.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@
3737
class PackageNotFoundError(ModuleNotFoundError):
3838
"""The package was not found."""
3939

40+
def __str__(self):
41+
tmpl = "No package metadata was found for {self.name}"
42+
return tmpl.format(**locals())
43+
44+
@property
45+
def name(self):
46+
name, = self.args
47+
return name
48+
4049

4150
class EntryPoint(
4251
collections.namedtuple('EntryPointBase', 'name value group')):

Lib/test/test_importlib/fixtures.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import textwrap
77
import contextlib
88

9+
from test.support.os_helper import FS_NONASCII
10+
911

1012
@contextlib.contextmanager
1113
def tempdir():
@@ -212,12 +214,7 @@ def build_files(file_defs, prefix=pathlib.Path()):
212214

213215
class FileBuilder:
214216
def unicode_filename(self):
215-
try:
216-
from test.support import os_helper
217-
except ImportError:
218-
# outside CPython, hard-code a unicode snowman
219-
return '☃'
220-
return os_helper.FS_NONASCII or \
217+
return FS_NONASCII or \
221218
self.skip("File system does not support non-ascii.")
222219

223220

Lib/test/test_importlib/test_main.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ def test_for_name_does_not_exist(self):
3232
with self.assertRaises(PackageNotFoundError):
3333
Distribution.from_name('does-not-exist')
3434

35+
def test_package_not_found_mentions_metadata(self):
36+
"""
37+
When a package is not found, that could indicate that the
38+
packgae is not installed or that it is installed without
39+
metadata. Ensure the exception mentions metadata to help
40+
guide users toward the cause. See #124.
41+
"""
42+
with self.assertRaises(PackageNotFoundError) as ctx:
43+
Distribution.from_name('does-not-exist')
44+
45+
assert "metadata" in str(ctx.exception)
46+
3547
def test_new_style_classes(self):
3648
self.assertIsInstance(Distribution, type)
3749

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
In ``importlib.metadata.PackageNotFoundError``, make reference to the
2+
package metadata being missing to improve the user experience.

0 commit comments

Comments
 (0)