Skip to content

Commit 09f97cf

Browse files
committed
Add more helpful error message when metadata is malformed, xref #558 #626
1 parent ac57b94 commit 09f97cf

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
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-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
DEFAULT_BACKEND = {
2323
'build-backend': 'setuptools.build_meta:__legacy__',
24-
'requires': ['setuptools >= 40.8.0'],
24+
'requires': ['setuptools >= 40.8.0', 'wheel'],
2525
}
2626

2727

@@ -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,12 @@ 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+
next(build.check_dependency("malformed_dep==1.0.0"), None)
179+
assert "Package malformed_dep has malformed metadata and no version information could be found" in str(excinfo)
180+
168181

169182
def test_bad_project(package_test_no_project):
170183
# Passing a nonexistent project directory

0 commit comments

Comments
 (0)