Skip to content

Commit

Permalink
Fix a NoneType AttributeError when evaluating hashes.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjerdonek committed Jul 23, 2019
1 parent 369ec7c commit 0a1571b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
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

0 comments on commit 0a1571b

Please sign in to comment.