Skip to content

bpo-43823: Improve syntax errors for invalid dictionary literals#25378

Merged
pablogsal merged 2 commits into
python:masterfrom
pablogsal:invalid_kvpairs
Apr 15, 2021
Merged

bpo-43823: Improve syntax errors for invalid dictionary literals#25378
pablogsal merged 2 commits into
python:masterfrom
pablogsal:invalid_kvpairs

Conversation

@pablogsal

@pablogsal pablogsal commented Apr 13, 2021

Copy link
Copy Markdown
Member

@gvanrossum

Copy link
Copy Markdown
Member

I hope @lysnikolaou can review this...

@lysnikolaou

Copy link
Copy Markdown
Member

I hope @lysnikolaou can review this...

I've already had a look at this, but I'll try to do a better review tomorrow.

@lysnikolaou lysnikolaou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Left one comment. It looks good other than that.

Comment thread Grammar/python.gram Outdated
| 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 ':'") }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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 😆

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I did the exact same thing, while I was testing if my recommendation works! 🚀

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ok, done in ce1a129

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That was exactly what I meant. Nice. Sorry if that wasn't clear enough.

@lysnikolaou lysnikolaou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@pablogsal
pablogsal merged commit da74350 into python:master Apr 15, 2021
@pablogsal
pablogsal deleted the invalid_kvpairs branch May 19, 2021 18:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants