Skip to content

bpo-42089: Sync with current cpython branch of importlib_metadata #22775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Lib/importlib/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@
class PackageNotFoundError(ModuleNotFoundError):
"""The package was not found."""

def __str__(self):
tmpl = "No package metadata was found for {self.name}"
return tmpl.format(**locals())

@property
def name(self):
name, = self.args
return name


class EntryPoint(
collections.namedtuple('EntryPointBase', 'name value group')):
Expand Down
9 changes: 3 additions & 6 deletions Lib/test/test_importlib/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import textwrap
import contextlib

from test.support.os_helper import FS_NONASCII


@contextlib.contextmanager
def tempdir():
Expand Down Expand Up @@ -212,12 +214,7 @@ def build_files(file_defs, prefix=pathlib.Path()):

class FileBuilder:
def unicode_filename(self):
try:
from test.support import os_helper
except ImportError:
# outside CPython, hard-code a unicode snowman
return '☃'
return os_helper.FS_NONASCII or \
return FS_NONASCII or \
self.skip("File system does not support non-ascii.")


Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_importlib/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ def test_for_name_does_not_exist(self):
with self.assertRaises(PackageNotFoundError):
Distribution.from_name('does-not-exist')

def test_package_not_found_mentions_metadata(self):
"""
When a package is not found, that could indicate that the
packgae is not installed or that it is installed without
metadata. Ensure the exception mentions metadata to help
guide users toward the cause. See #124.
"""
with self.assertRaises(PackageNotFoundError) as ctx:
Distribution.from_name('does-not-exist')

assert "metadata" in str(ctx.exception)

def test_new_style_classes(self):
self.assertIsInstance(Distribution, type)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
In ``importlib.metadata.PackageNotFoundError``, make reference to the
package metadata being missing to improve the user experience.