[Python] Fix nonlocal declarations inside match/case blocks (SyntaxError regression)#4379
Merged
[Python] Fix nonlocal declarations inside match/case blocks (SyntaxError regression)#4379
Conversation
…ror regression) Python requires `nonlocal`/`global` declarations to appear before any use of the variable in the enclosing function. Because `match/case` does not create a new scope, nonlocal statements generated inside case bodies caused `SyntaxError: name 'x' is used prior to nonlocal declaration` at runtime. Fix: lift NonLocal/Global statements out of all match case bodies before building the `Statement.match'` node. The lifted declarations are prepended to the result list, where `transformBody`'s existing `getNonLocals` pass then hoists them to the top of the enclosing function. Add `getNonLocals` and `getNonLocalsFromMatchCases` helpers to `Fable2Python.Util` and apply them at all 6 match statement creation sites in `Fable2Python.Transforms`. Also add regression tests covering the `take` operator pattern and nested union match with mutable closure captures. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Python Type Checking Results (Pyright)
Excluded files with errors (4 files)These files have known type errors and are excluded from CI. Remove from
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
nonlocal/globaldeclarations were being generated insidecasebodies inmatch/casestatements, causingSyntaxError: name 'x' is used prior to nonlocal declarationat runtime (regression reported infsharp_control_async_rx/filter.py)Statement.match'node; prepend them before the match sotransformBodycan hoist them to the top of the enclosing functiongetNonLocalsandgetNonLocalsFromMatchCaseshelpers toFable2Python.Util(single source of truth) and apply them at all 6 match statement creation sites inFable2Python.TransformstakeFn(take operator with mutable closure) andnestedUnionMatch(nested union match with mutable capture)Test plan
./build.sh test python --skip-fable-librarypasses all tests including the two new regression testsfsharp_control_async_rx/filter.py(timeflies example) no longer throwsSyntaxErrornonlocal/globaldeclarations appear before any use of the variable in generated Python output🤖 Generated with Claude Code