Fix a NoneType AttributeError when evaluating hashes.

This commit is contained in:
Chris Jerdonek 2019-07-23 05:18:59 -04:00
parent 369ec7c0a8
commit 0a1571ba03
3 changed files with 14 additions and 2 deletions

2
news/6772.bugfix Normal file
View File

@ -0,0 +1,2 @@
Fix a ``NoneType`` ``AttributeError`` when evaluating hashes and no hashes
are provided.

View File

@ -200,11 +200,11 @@ class Link(KeyBasedCompareMixin):
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

View File

@ -117,3 +117,13 @@ class TestLink:
}
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