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

Fix/5147 #5170

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 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/5147.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Don't output stack trace when `pip install` is called with an invalid
requirement.
6 changes: 4 additions & 2 deletions src/pip/_internal/req/req_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,16 @@ def from_line(
if req is not None:
try:
req = Requirement(req)
except InvalidRequirement:
except InvalidRequirement as e:
if os.path.sep in req:
add_msg = "It looks like a path."
add_msg += deduce_helpful_msg(req)
elif '=' in req and not any(op in req for op in operators):
add_msg = "= is not a valid operator. Did you mean == ?"
else:
add_msg = traceback.format_exc()
msg = str(e)
msg = msg.replace('Invalid requirement, ', '')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This replace() seems brittle and hacky to me, but I'll let others weigh in. The packaging project probably shouldn't be including this string to begin with since it's redundant with the exception class being InvalidRequirement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed I've created a PR against packaging fixing that: pypa/packaging#129

add_msg = msg
raise InstallationError(
"Invalid requirement: '%s'\n%s" % (req, add_msg))
return cls(
Expand Down
11 changes: 9 additions & 2 deletions tests/unit/test_req.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,12 +536,19 @@ def test_single_equal_sign(self):
assert "Invalid requirement" in err_msg
assert "= is not a valid operator. Did you mean == ?" in err_msg

def test_traceback(self):
def test_no_traceback(self):
with pytest.raises(InstallationError) as e:
InstallRequirement.from_line('toto 42')
err_msg = e.value.args[0]
assert "Invalid requirement" in err_msg
assert "\nTraceback " in err_msg
assert "Traceback" not in err_msg

def test_error_hint(self):
with pytest.raises(InstallationError) as e:
InstallRequirement.from_line('toto 42')
err_msg = e.value.args[0]
assert "Invalid requirement" in err_msg
assert "parse error at" in err_msg

def test_requirement_file(self):
req_file_path = os.path.join(self.tempdir, 'test.txt')
Expand Down