Skip to content

Commit 3610d14

Browse files
committed
Merge branch 'feature/124-nicer-not-found'
2 parents f0c5679 + 583731b commit 3610d14

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

importlib_metadata/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@
5555
class PackageNotFoundError(ModuleNotFoundError):
5656
"""The package was not found."""
5757

58+
def __str__(self):
59+
tmpl = "No package metadata was found for {self.name}"
60+
return tmpl.format(**locals())
61+
62+
@property
63+
def name(self):
64+
name, = self.args
65+
return name
66+
5867

5968
class EntryPoint(
6069
PyPy_repr,

importlib_metadata/docs/changelog.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
importlib_metadata NEWS
33
=========================
44

5+
v1.7.0
6+
======
7+
8+
* ``PathNotFoundError`` now has a custom ``__str__``
9+
mentioning "package metadata" being missing to help
10+
guide users to the cause when the package is installed
11+
but no metadata is present. Closes #124.
12+
513
v1.6.1
614
======
715

importlib_metadata/tests/test_main.py

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

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

0 commit comments

Comments
 (0)