diff --git a/news/5147.bugfix b/news/5147.bugfix new file mode 100644 index 00000000000..1ed999b3af4 --- /dev/null +++ b/news/5147.bugfix @@ -0,0 +1,2 @@ +Don't output stack trace when `pip install` is called with an invalid +requirement. diff --git a/src/pip/_internal/req/req_install.py b/src/pip/_internal/req/req_install.py index f3515e40ce2..1662cea2719 100644 --- a/src/pip/_internal/req/req_install.py +++ b/src/pip/_internal/req/req_install.py @@ -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, ', '') + add_msg = msg raise InstallationError( "Invalid requirement: '%s'\n%s" % (req, add_msg)) return cls( diff --git a/tests/unit/test_req.py b/tests/unit/test_req.py index f9e73afc323..4d5cfa7b990 100644 --- a/tests/unit/test_req.py +++ b/tests/unit/test_req.py @@ -537,12 +537,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')