Skip to content

Commit 4adc49c

Browse files
jaracoclaude
andcommitted
Report invalid distributions in diagnose instead of crashing.
An empty metadata directory is still discovered as a distribution, but it cannot name itself, so the diagnostic died on the very condition it exists to surface. Describe such distributions by the location of their metadata instead. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent fe3dbcd commit 4adc49c

3 files changed

Lines changed: 47 additions & 2 deletions

File tree

‎importlib_metadata/diagnose.py‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
import sys
22

3-
from . import Distribution
3+
from . import Distribution, MetadataNotFound
4+
5+
6+
def _locate(dist: Distribution) -> str:
7+
"""
8+
Indicate where the distribution's metadata was found.
9+
"""
10+
return str(getattr(dist, '_path', dist))
11+
12+
13+
def describe(dist: Distribution) -> str:
14+
"""
15+
Describe the distribution by name, or by location when the
16+
distribution is too invalid to name itself.
17+
18+
>>> describe(Distribution.at('missing-1.0.dist-info'))
19+
'missing-1.0.dist-info (no metadata)'
20+
"""
21+
try:
22+
return dist.name
23+
except MetadataNotFound:
24+
return f'{_locate(dist)} (no metadata)'
25+
except KeyError:
26+
return f'{_locate(dist)} (metadata without a name)'
427

528

629
def inspect(path):
@@ -9,7 +32,7 @@ def inspect(path):
932
if not dists:
1033
return
1134
print("Found", len(dists), "packages:", end=' ')
12-
print(', '.join(dist.name for dist in dists))
35+
print(', '.join(map(describe, dists)))
1336

1437

1538
def run():

‎newsfragments/548.bugfix.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``importlib_metadata.diagnose`` no longer crashes on a metadata directory
2+
that has no metadata file or whose metadata declares no name. Such
3+
distributions are now reported by their location.

‎tests/test_main.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import contextlib
12
import importlib
3+
import io
24
import pickle
35
import re
46
import unittest
@@ -13,6 +15,7 @@
1315
MetadataNotFound,
1416
PackageNotFoundError,
1517
_unique,
18+
diagnose,
1619
distributions,
1720
entry_points,
1821
metadata,
@@ -169,6 +172,22 @@ def test_missing_metadata(self):
169172
with self.assertRaises(MetadataNotFound):
170173
metadata('foo')
171174

175+
def test_diagnose_reports_invalid(self):
176+
"""
177+
The diagnostic should report invalid dists by location
178+
rather than crashing on them.
179+
180+
Ref python/importlib_metadata#548.
181+
"""
182+
fixtures.build_files(self.make_pkg('nameless-1.0'), self.site_dir)
183+
fixtures.build_files(self.make_pkg('empty-1.0', files={}), self.site_dir)
184+
out = io.StringIO()
185+
with contextlib.redirect_stdout(out):
186+
diagnose.inspect(str(self.site_dir))
187+
report = out.getvalue()
188+
assert 'empty-1.0.dist-info (no metadata)' in report
189+
assert 'nameless-1.0.dist-info (metadata without a name)' in report
190+
172191

173192
class NonASCIITests(fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
174193
@staticmethod

0 commit comments

Comments
 (0)