Skip to content

Allow general expressions in PEP 798 dict comprehension unpacking (#30406) - #30418

Open
viliamgonda wants to merge 1 commit into
astral-sh:mainfrom
viliamgonda:fix/pep798-dictcomp-boolean-expr
Open

viliamgonda wants to merge 1 commit into
astral-sh:mainfrom
viliamgonda:fix/pep798-dictcomp-boolean-expr

Conversation

@viliamgonda

Copy link
Copy Markdown

Summary

Closes #30406.

In PEP 798, dictionary comprehension unpacking uses the general expression grammar rule ('**' expression for_if_clauses), whereas dictionary displays restrict unpacking operands to bitwise OR precedence ('**' bitwise_or).

parse_set_or_dict_like_expression previously called parse_expression_with_bitwise_or_precedence() immediately upon encountering **. This prematurely rejected valid unparenthesized boolean expressions (e.g. {**x or {} for x in []}, {**not x for x in []}) with Boolean expression cannot be used here before determining whether the construct was a dictionary comprehension.

Refactor bitwise OR precedence validation into validate_bitwise_or_precedence and defer it until after checking whether the expression is followed by comprehension clauses (for / async).

…king (astral-sh#30406)

In PEP 798, dictionary comprehension unpacking uses the general expression
rule ('**' expression for_if_clauses), unlike dictionary displays which
restrict unpacking operands to bitwise OR precedence ('**' bitwise_or).

Defer bitwise OR precedence validation in parse_set_or_dict_like_expression
until after checking whether the expression is followed by comprehension
clauses ('for' / 'async').

Closes astral-sh#30406.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

parser Related to the parser

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Valid PEP 798 dictionary comprehension with a boolean expression is rejected

2 participants