Skip to content

Commit

Permalink
Add more helpful error message when metadata is malformed, xref pypa#558
Browse files Browse the repository at this point in the history
  • Loading branch information
timkpaine committed Feb 1, 2024
1 parent 26f2c5c commit 02f3a81
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/build/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ def check_dependency(
# dependency is not installed in the environment.
yield (*ancestral_req_strings, normalised_req_string)
else:
if dist.version is None:
# Malformed dist-info or some other form of metadata corruption
# req.specifier.contains will raise TypeError, so let's do the same
# with a more helpful error message
raise TypeError(f"Package {req.name} has malformed metadata and no version information could be found")
if req.specifier and not req.specifier.contains(dist.version, prereleases=True):
# the installed version is incompatible.
yield (*ancestral_req_strings, normalised_req_string)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_projectbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def from_name(cls, name):
return CircularMockDistribution()
elif name == 'nested_circular_dep':
return NestedCircularMockDistribution()
elif name == 'malformed_dep':
return MalformedMockDistribution()
raise _importlib.metadata.PackageNotFoundError


Expand Down Expand Up @@ -127,6 +129,11 @@ def read_text(self, filename):
"""
).strip()

class MalformedMockDistribution(MockDistribution):
def read_text(self, filename):
if filename == 'METADATA':
return ""


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

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


def test_bad_project(package_test_no_project):
# Passing a nonexistent project directory
Expand Down

0 comments on commit 02f3a81

Please sign in to comment.