Repository navigation
Don't evaluate a function's local variable annotations, as in Python (#796) - #951
Open
craigbarratt wants to merge 1 commit into
Open
craigbarratt wants to merge 1 commit into
craigbarratt wants to merge 1 commit into
Conversation
…796) Python never evaluates or stores the annotation of a local variable in a function, so `y: Any[int] = None` inside a function runs without error. Pyscript evaluated it and failed with TypeError. Module and class annotations are unchanged. Co-Authored-By: Claude Opus 5.5 <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.
Fixes #796.
Python never evaluates or stores the annotation of a local variable in a function. Pyscript evaluated it each time the assignment ran, so
y: Any[int] = Noneinside a function raisedTypeError: type 'Any' is not subscriptable, and an undefined name in the annotation was kept as a string in a local__annotations__.ast_annassignnow skips the annotation in a function's local scope and only assigns the value. Module and class annotations are unchanged, including a class body inside a function. The branch that kept a function's own__annotations__variable is no longer needed.Tests: new cases in
FUNCTION_LOCALS_TESTS, compared with CPython and also run inside an inner function:x: intwith no value still makesxan unbound local;__annotations__isn't updated by a local annotation.The new tests fail on master. All 307 tests pass, and ruff, pylint and mypy are clean.
🤖 Generated with Claude Code