Skip to content

Conversation

@jackulau
Copy link
Contributor

Named expressions (walrus operator :=) are not allowed inside type aliases per PEP 695. Python raises a SyntaxError for this case.

Previously, pyrefly would panic with "key lacking binding" when processing code like:

type T = (a := 1)                                                                                                                                                      
type U = (a := 1)                                                                                                                                                      

The root cause was that the definitions phase incorrectly collected walrus operator names from type alias values at module level, but during binding these were
processed in the type alias scope, causing a key/binding mismatch.

This fix:

  • Adds in_type_alias() method to check if we're in a type alias scope
  • Reports "named expression cannot be used within a type alias" error (matching Python's error message)
  • Removes incorrect walrus name collection from type alias values

Fixes #1883

Test Plan

  • Added test_named_expression_in_type_alias test case that verifies the error is reported for walrus operators in type aliases
  • All 3160 existing tests pass

…1883)

Named expressions (walrus operator `:=`) are not allowed inside type
aliases per PEP 695. Python raises a SyntaxError for this case.

Previously, pyrefly would panic with "key lacking binding" when
processing code like:

    type T = (a := 1)
    type U = (a := 1)

The root cause was that the definitions phase incorrectly collected
walrus operator names from type alias values at module level, but
during binding these were processed in the type alias scope, causing
a key/binding mismatch.

This fix:
- Adds `in_type_alias()` method to check if we're in a type alias scope
- Reports "named expression cannot be used within a type alias" error
- Removes incorrect walrus name collection from type alias values
@meta-cla meta-cla bot added the cla signed label Dec 18, 2025
@kinto0 kinto0 self-assigned this Dec 18, 2025
@meta-codesync
Copy link

meta-codesync bot commented Dec 18, 2025

@kinto0 has imported this pull request. If you are a Meta employee, you can view this in D89476251.

Copy link
Contributor

@yangdanny97 yangdanny97 left a comment

Choose a reason for hiding this comment

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

Review automatically exported from Phabricator review in Meta.

@meta-codesync
Copy link

meta-codesync bot commented Dec 30, 2025

@kinto0 merged this pull request in 2276094.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

panic: 'key lacking binding' with duplicate named expressions in type aliases

4 participants