Skip to content

Commit 81040b5

Browse files
authored
Update Link.is_hash_allowed() to accept hashes=None (#6774)
2 parents ec5a01d + 0a1571b commit 81040b5

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

news/6772.bugfix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a ``NoneType`` ``AttributeError`` when evaluating hashes and no hashes
2+
are provided.

src/pip/_internal/models/link.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,11 @@ def has_hash(self):
200200
return self.hash_name is not None
201201

202202
def is_hash_allowed(self, hashes):
203-
# type: (Hashes) -> bool
203+
# type: (Optional[Hashes]) -> bool
204204
"""
205205
Return True if the link has a hash and it is allowed.
206206
"""
207-
if not self.has_hash:
207+
if hashes is None or not self.has_hash:
208208
return False
209209
# Assert non-None so mypy knows self.hash_name and self.hash are str.
210210
assert self.hash_name is not None

tests/unit/test_link.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,13 @@ def test_is_hash_allowed__no_hash(self):
117117
}
118118
hashes = Hashes(hashes_data)
119119
assert not link.is_hash_allowed(hashes)
120+
121+
@pytest.mark.parametrize('hashes, expected', [
122+
(None, False),
123+
# Also test a success case to show the test is correct.
124+
(Hashes({'sha512': [128 * 'a']}), True),
125+
])
126+
def test_is_hash_allowed__none_hashes(self, hashes, expected):
127+
url = 'https://example.com/wheel.whl#sha512={}'.format(128 * 'a')
128+
link = Link(url)
129+
assert link.is_hash_allowed(hashes) == expected

0 commit comments

Comments
 (0)