Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions custom_components/pyscript/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1762,13 +1762,9 @@ async def ast_augassign(self, arg):

async def ast_annassign(self, arg):
"""Execute type hint assignment statement and track __annotations__."""
if isinstance(arg.target, ast.Name):
# as in Python, the annotation of a function's local variable is never evaluated or stored
if isinstance(arg.target, ast.Name) and not self.in_local_scope():
annotations = self.sym_table.setdefault("__annotations__", {})
if isinstance(annotations, EvalLocalVar):
# a function's own __annotations__ variable
if not annotations.is_defined():
annotations.set({})
annotations = annotations.get()
if arg.annotation:
try:
annotations[arg.target.id] = await self.aeval(arg.annotation)
Expand Down
37 changes: 37 additions & 0 deletions tests/test_unit_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -6854,6 +6854,43 @@ def f():
x: int = 1
return x
result = f()
""",
"""
def f():
__annotations__ = {}
x: int = 1
return x, __annotations__
result = f()
""",
# a local variable's annotation is never evaluated (#796)
"""
from typing import Any
def f():
y: Any[int] = None
z: undefined_name = 2
return y, z
result = f()
""",
"""
calls = []
def note(name):
calls.append(name)
return int
def f():
x: note("local") = 1
class C:
y: note("class") = 2
return x, C.y, C.__annotations__
result = f(), calls
""",
"""
def f():
x: int
try:
return x
except UnboundLocalError as exc:
return type(exc).__name__
result = f()
""",
"""
def f():
Expand Down
Loading