Skip to content

Wrong Python.asdl type for keys in Dict literals #131421

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

Closed
Demonstrandum opened this issue Mar 18, 2025 · 7 comments
Closed

Wrong Python.asdl type for keys in Dict literals #131421

Demonstrandum opened this issue Mar 18, 2025 · 7 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-parser type-bug An unexpected behavior, bug, or error

Comments

@Demonstrandum
Copy link
Contributor

Demonstrandum commented Mar 18, 2025

In the Python.asdl syntax description, the file asserts that Dict literal keys have type expr*. This is false. keys can be optional.

In the ast documentation for Python:

When doing dictionary unpacking using dictionary literals the expression to be expanded goes in the values list, with a None at the corresponding position in keys.

Hence, keys is really a expr?* and not a expr*.

Linked PRs

@picnixz

This comment has been minimized.

@picnixz picnixz added the pending The issue will be closed if no feedback is provided label Mar 18, 2025
@Demonstrandum
Copy link
Contributor Author

In the ADSL grammar, I don't think ?* is allowed. The ? stands for "optional", namely something that can be omitted, while * stands for "0 or more" arguments, given as a list, so IIRC, it represents any kind of lists. Here, the ASDL entry for Dict only means that keys is a list, with zero or more expressions.

cc @JelleZijlstra

Right, but there aren't 0 or more expressions, but rather 0 or more optional expressions, ie an expression can be None instead of an actual expr

@picnixz

This comment has been minimized.

@picnixz
Copy link
Member

picnixz commented Mar 18, 2025

Ah I understand your point now. You mean that the None is not a true None but a sentinel None.

@picnixz picnixz removed the pending The issue will be closed if no feedback is provided label Mar 18, 2025
@picnixz picnixz added type-bug An unexpected behavior, bug, or error interpreter-core (Objects, Python, Grammar, and Parser dirs) and removed docs Documentation in the Doc dir labels Mar 22, 2025
@picnixz
Copy link
Member

picnixz commented Mar 22, 2025

(Adding the relevant labels as the PR being proposed suggests a way to improve the existing grammar parser)

@StanFromIreland
Copy link
Contributor

This is still in the docs project.

@picnixz picnixz removed this from docs issues Apr 20, 2025
JelleZijlstra added a commit that referenced this issue May 4, 2025
…#131419)

In the `ast` documentation for Python:

* https://docs.python.org/3/library/ast.html#ast.Dict
it is made clear that:

> When doing dictionary unpacking using dictionary literals the expression to be expanded goes in the values list, with a `None` at the corresponding position in `keys`.

Hence, `keys` is really a `expr?*` and *not* a `expr*`.

Co-authored-by: Bénédikt Tran <[email protected]>
Co-authored-by: Jelle Zijlstra <[email protected]>
JelleZijlstra added a commit to JelleZijlstra/cpython that referenced this issue May 4, 2025
JelleZijlstra added a commit to JelleZijlstra/cpython that referenced this issue May 4, 2025
PR python#131419 broke this, but we failed to run tests on the PR due to a bug
in our script.
ambv pushed a commit that referenced this issue May 4, 2025
PR #131419 broke this, but we failed to run tests on the PR due to a bug
in our script.
@Demonstrandum
Copy link
Contributor Author

Merged #131419

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-parser type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants