bpo-43823: Improve syntax errors for invalid dictionary literals#25378
Conversation
dcbf88f to
196347b
Compare
|
I hope @lysnikolaou can review this... |
I've already had a look at this, but I'll try to do a better review tomorrow. |
196347b to
e90dd4f
Compare
lysnikolaou
left a comment
There was a problem hiding this comment.
Left one comment. It looks good other than that.
| | expression a=':' &('}'|',') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") } | ||
| invalid_kvpair: | ||
| | a=expression !(':') { RAISE_SYNTAX_ERROR("':' expected after dictionary key") } | ||
| | expression a=':' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") } |
There was a problem hiding this comment.
What about adding a third alternative here to handle the error message for a starred expr after the colon? Something along the lines of:
| expression ':' b='*' bitwise_or { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use a starred expression in a dictionary value") }
The above rule might need some changing as well to accommodate the case where this happens in the first kvpair.
There was a problem hiding this comment.
The problem is that we cannot easily do it in the first pair because that can happen in a set. Only when we know it must be a dictionary (because we have seen the first valid pair) then we know that these things are incorrect.
There was a problem hiding this comment.
In this case though, because we're parsing the ':' token, can't we be optimistic and assume that they're trying to create a dictionary? We might have some false positives for this alternative, but in most cases the error report would be correct, right?
There was a problem hiding this comment.
In this case though, because we're parsing the ':' token, can't we be optimistic and assume that they're trying to create a dictionary?
Yep yep, I am referring to this part of your message:
to accommodate the case where this happens in the first kvpair.
There was a problem hiding this comment.
Ah, yes, the joy of seeing you own error:
self.check_intersection(alt, other_alt)
File "/Users/pgalindo3/github/python/master/Tools/peg_generator/pegen/validator.py", line 44, in check_intersection
raise ValidationError(
pegen.validator.ValidationError: In invalid_kvpair there is an alternative that will never be visited:
I just ordered them incorrectly 😆
There was a problem hiding this comment.
I did the exact same thing, while I was testing if my recommendation works! 🚀
There was a problem hiding this comment.
We might have some false positives for this alternative, but in most cases the error report would be correct, right?
I think I know what you mean here, let me try to get it to work also on the first alternative
There was a problem hiding this comment.
That was exactly what I meant. Nice. Sorry if that wasn't clear enough.
https://bugs.python.org/issue43823