[Python] Fix exception variable captured in deferred closures (NameError)#4382
Merged
[Python] Fix exception variable captured in deferred closures (NameError)#4382
Conversation
…ror) Python's `except E as name:` implicitly deletes `name` at the end of the except block (per PEP 3110). This caused a NameError at runtime when a closure capturing the exception variable was stored and called after the except block exited. Fix: in `makeHandler`, copy the exception variable to `name_` (a regular local that Python won't delete) immediately inside the except block, then rename all body references from `name` to `name_` via `FableTransforms.replaceValues`. The original name is kept in the `except ... as name:` clause for readability. Affects both direct deferred closures (`fun () -> ex.Message`) and lambda-with-argument patterns (`fun obv _ -> obv.OnErrorAsync ex`) as seen in AsyncRx's `defer` function. Adds two regression tests to TestMisc.fs covering both patterns. 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
|
…dler body Use `isIdentUsed` to skip the save-to-safe-copy step when the exception variable is not referenced in the handler body. When it is used, use `getUniqueNameInDeclarationScope` to give each handler its own unique safe-copy name, preventing Pylance `reportRedeclaration` warnings when multiple typed `except` handlers exist in the same function. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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
except E as name:implicitly deletesnameat end of the except block (PEP 3110). Closures that capture the variable and are called after the block exit get aNameError: free variable 'ex' referenced before assignment in enclosing scope.makeHandler(transformTryCatch): copyex→ex_immediately inside the block via an annotated assignment, then rename all body references usingFableTransforms.replaceValues. The original name stays inexcept ... as ex:for readability.fun () -> ex.Message) and lambda-with-argument patterns (fun obv _ -> obv.OnErrorAsync ex) — the latter mirrors the AsyncRxdeferfunction.Before:
After:
Test plan
TestMisc.fscovering both closure patterns./build.sh test python --skip-fable-library)deferfunction output🤖 Generated with Claude Code