Skip to content

Commit 5a388a7

Browse files
committed
Add more helpful error message when metadata is malformed, xref #558 #626
1 parent 26f2c5c commit 5a388a7

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/build/_util.py

+6
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ def check_dependency(
5353
# dependency is not installed in the environment.
5454
yield (*ancestral_req_strings, normalised_req_string)
5555
else:
56+
if dist.version is None:
57+
# Malformed dist-info or some other form of metadata corruption
58+
# req.specifier.contains will raise TypeError, so let's do the same
59+
# with a more helpful error message
60+
msg = f'Package {req.name} has malformed metadata and no version information could be found'
61+
raise TypeError(msg)
5662
if req.specifier and not req.specifier.contains(dist.version, prereleases=True):
5763
# the installed version is incompatible.
5864
yield (*ancestral_req_strings, normalised_req_string)

tests/test_projectbuilder.py

+14
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ def from_name(cls, name):
4343
return CircularMockDistribution()
4444
elif name == 'nested_circular_dep':
4545
return NestedCircularMockDistribution()
46+
elif name == 'malformed_dep':
47+
return MalformedMockDistribution()
4648
raise _importlib.metadata.PackageNotFoundError
4749

4850

@@ -127,6 +129,11 @@ def read_text(self, filename):
127129
"""
128130
).strip()
129131

132+
class MalformedMockDistribution(MockDistribution):
133+
def read_text(self, filename):
134+
if filename == 'METADATA':
135+
return ""
136+
130137

131138
@pytest.mark.parametrize(
132139
('requirement_string', 'expected'),
@@ -165,6 +172,13 @@ def test_check_dependency(monkeypatch, requirement_string, expected):
165172
monkeypatch.setattr(_importlib.metadata, 'Distribution', MockDistribution)
166173
assert next(build.check_dependency(requirement_string), None) == expected
167174

175+
def test_check_dependency_bad_metadata(monkeypatch):
176+
monkeypatch.setattr(_importlib.metadata, 'Distribution', MockDistribution)
177+
with pytest.raises(TypeError) as excinfo:
178+
with pytest.warns(DeprecationWarning):
179+
next(build.check_dependency("malformed_dep==1.0.0"), None)
180+
assert "Package malformed_dep has malformed metadata and no version information could be found" in str(excinfo)
181+
168182

169183
def test_bad_project(package_test_no_project):
170184
# Passing a nonexistent project directory

0 commit comments

Comments
 (0)