-
Notifications
You must be signed in to change notification settings - Fork 253
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
Improved error message in ParseException #129
Improved error message in ParseException #129
Conversation
tests/test_requirements.py
Outdated
def test_parseexception_error_msg(self): | ||
with pytest.raises(InvalidRequirement) as e: | ||
Requirement("toto 42") | ||
assert "Invalid Requiremnt" not in e |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you tried running this test without the changes you've made? I'm pretty sure it will still pass, the assertion will never be reached because the line before it raises the InvalidRequirement
exception.
I also don't think you can assert whether a string is in an exception like that. It'd probably be better to assert that it's equivalent to an expected string, rather than asserting that it doesn't contain some string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the test to check for a substring being in the exception now.
packaging/requirements.py
Outdated
raise InvalidRequirement( | ||
"Invalid requirement, parse error at \"{0!r}\"".format( | ||
requirement_string[e.loc:e.loc + 8])) | ||
raise InvalidRequirement("Parse error: {0!r}".format(e)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changes the exception from:
Invalid requirement, parse error at "'42'"
to:
Parse error: Expected stringEnd (at char 5), (line:1, col:6)
It's not really clear to me how this improves the situation in pypa/pip#5170? Is the idea that pip
will now parse this new message and display something more meaningful to the user? I'm not sure that the new message is actually more user-friendly than the old one.
My 2 cents on this would be to change it to: index f87c57c..e8008a6 100644
--- a/packaging/requirements.py
+++ b/packaging/requirements.py
@@ -92,16 +92,16 @@ class Requirement(object):
try:
req = REQUIREMENT.parseString(requirement_string)
except ParseException as e:
- raise InvalidRequirement(
- "Invalid requirement, parse error at \"{0!r}\"".format(
- requirement_string[e.loc:e.loc + 8]))
+ raise InvalidRequirement("Parse error at \"{0!r}\": {1}".format(
+ requirement_string[e.loc:e.loc + 8], e.msg
+ ))
self.name = req.name
if req.url:
parsed_url = urlparse.urlparse(req.url)
if not (parsed_url.scheme and parsed_url.netloc) or (
not parsed_url.scheme and not parsed_url.netloc):
- raise InvalidRequirement("Invalid URL given")
+ raise InvalidRequirement("Invalid URL: {0}".format(req.url))
self.url = req.url
else:
self.url = None
|
Updated my PR to @pradyunsg 's suggestion. And added a test for the new string on an invalid-URL exception. |
tests/test_requirements.py
Outdated
Requirement("name @ gopher:/foo/com") | ||
assert "Invalid URL: " in e |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be dedented by one level. Otherwise, this line isn't executed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd check "Invalid URL" in str(e)
and 'gopher:/foo/com' in str(e)
tests/test_requirements.py
Outdated
def test_parseexception_error_msg(self): | ||
with pytest.raises(InvalidRequirement) as e: | ||
Requirement("toto 42") | ||
assert "Expected stringEnd" in e |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dedent this line as well.
@venthur thanks for the PR :) |
Hello,
This PR will help to fix pypa/pip#5147 and pypa/pip#5170. It changes the string being passed in the
InvalidRequirement
to something likeParse error: Expected stringEnd (at char 5), (line:1, col:6)
which can be easily used bypip
to produce a nicer error message on parsing errors.