Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Link.is_hash_allowed() to accept hashes=None #6774

Merged
Merged
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
2 changes: 2 additions & 0 deletions news/6772.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a ``NoneType`` ``AttributeError`` when evaluating hashes and no hashes
are provided.
4 changes: 2 additions & 2 deletions src/pip/_internal/models/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,11 @@ def has_hash(self):
return self.hash_name is not None

def is_hash_allowed(self, hashes):
# type: (Hashes) -> bool
# type: (Optional[Hashes]) -> bool
"""
Return True if the link has a hash and it is allowed.
"""
if not self.has_hash:
if hashes is None or not self.has_hash:
return False
# Assert non-None so mypy knows self.hash_name and self.hash are str.
assert self.hash_name is not None
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/test_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,13 @@ def test_is_hash_allowed__no_hash(self):
}
hashes = Hashes(hashes_data)
assert not link.is_hash_allowed(hashes)

@pytest.mark.parametrize('hashes, expected', [
(None, False),
# Also test a success case to show the test is correct.
(Hashes({'sha512': [128 * 'a']}), True),
])
def test_is_hash_allowed__none_hashes(self, hashes, expected):
url = 'https://example.com/wheel.whl#sha512={}'.format(128 * 'a')
link = Link(url)
assert link.is_hash_allowed(hashes) == expected