Skip to content
Open
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
7 changes: 6 additions & 1 deletion importlib_metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,12 @@ def _ensure_metadata_present(self, text: str | None) -> str:
@property
def name(self) -> str:
"""Return the 'Name' metadata for the distribution package."""
return self.metadata['Name']
try:
return self.metadata['Name']
except KeyError:
raise KeyError(
f'No Name metadata found for {getattr(self, "_path", self)}'
) from None

@property
def _normalized_name(self):
Expand Down
13 changes: 13 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,19 @@ def test_missing_metadata(self):
with self.assertRaises(MetadataNotFound):
metadata('foo')

def test_missing_name_metadata(self):
"""
Dists with metadata but no Name should raise a KeyError
with a useful message identifying the distribution.

Ref python/importlib_metadata#508.
"""
fixtures.build_files(self.make_pkg('foo-4.4'), self.site_dir)
dist = Distribution.at(self.site_dir / 'foo-4.4.dist-info')
with self.assertRaises(KeyError) as ctx:
dist.name
assert 'foo-4.4.dist-info' in str(ctx.exception)


class NonASCIITests(fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
@staticmethod
Expand Down