Skip to content

Commit bb14ff4

Browse files
rouge8cjerdonek
authored andcommitted
Fix NameError when handling InvalidRequirement in install_req_from_req_string (#6419)
Previously, an InvalidRequirement would raise a NameError while trying to raise an InstallationError because `req` was not defined. Discovered while working on #6402.
1 parent c8e9caa commit bb14ff4

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

news/6419.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix ``NameError`` when handling an invalid requirement.

src/pip/_internal/req/constructors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def install_req_from_req_string(
319319
try:
320320
req = Requirement(req_string)
321321
except InvalidRequirement:
322-
raise InstallationError("Invalid requirement: '%s'" % req)
322+
raise InstallationError("Invalid requirement: '%s'" % req_string)
323323

324324
domains_not_allowed = [
325325
PyPI.file_storage_domain,

tests/unit/test_req_install.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55
from pip._vendor.packaging.requirements import Requirement
66

7+
from pip._internal.exceptions import InstallationError
78
from pip._internal.req.constructors import (
89
install_req_from_line, install_req_from_req_string,
910
)
@@ -49,6 +50,18 @@ def test_forward_slash_results_in_a_link(self, tmpdir):
4950

5051
class TestInstallRequirementFrom(object):
5152

53+
def test_install_req_from_string_invalid_requirement(self):
54+
"""
55+
Requirement strings that cannot be parsed by
56+
packaging.requirements.Requirement raise an InstallationError.
57+
"""
58+
with pytest.raises(InstallationError) as excinfo:
59+
install_req_from_req_string("http:/this/is/invalid")
60+
61+
assert str(excinfo.value) == (
62+
"Invalid requirement: 'http:/this/is/invalid'"
63+
)
64+
5265
def test_install_req_from_string_without_comes_from(self):
5366
"""
5467
Test to make sure that install_req_from_string succeeds

0 commit comments

Comments
 (0)