Skip to content

Commit 12b5047

Browse files
committed
added ast_annassign; see #537
1 parent ba15d6c commit 12b5047

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

custom_components/pyscript/eval.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,6 +1446,12 @@ async def ast_augassign(self, arg):
14461446
arg.target.ctx = ast.Store()
14471447
await self.recurse_assign(arg.target, new_val)
14481448

1449+
async def ast_annassign(self, arg):
1450+
"""Execute type hint assignment statement (just ignore the type hint)."""
1451+
if arg.value is not None:
1452+
rhs = await self.aeval(arg.value)
1453+
await self.recurse_assign(arg.target, rhs)
1454+
14491455
async def ast_namedexpr(self, arg):
14501456
"""Execute named expression."""
14511457
val = await self.aeval(arg.value)

tests/test_unit_eval.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@
140140
["func = lambda m=2: 2 * m; [func(), func(3), func(m=4)]", [4, 6, 8]],
141141
["thres = 1; list(filter(lambda x: x < thres, range(-5, 5)))", [-5, -4, -3, -2, -1, 0]],
142142
["y = 5; y = y + (x := 2 * y); [x, y]", [10, 15]],
143+
["x: int = 10; x", 10],
144+
["x: int = [10, 20]; x", [10, 20]],
143145
["Foo = type('Foo', (), {'x': 100}); Foo.x = 10; Foo.x", 10],
144146
["Foo = type('Foo', (), {'x': 100}); Foo.x += 10; Foo.x", 110],
145147
["Foo = [type('Foo', (), {'x': 100})]; Foo[0].x = 10; Foo[0].x", 10],
@@ -1429,6 +1431,7 @@ async def test_eval(hass):
14291431
"Exception in test line 1 column 21: too few values to unpack (expected 3)",
14301432
],
14311433
["(x, y) = 1", "Exception in test line 1 column 9: cannot unpack non-iterable object"],
1434+
["x: int; x", "Exception in test line 1 column 8: name 'x' is not defined"],
14321435
[
14331436
"a, *y, w, z = range(2)",
14341437
"Exception in test line 1 column 20: too few values to unpack (expected at least 3)",

0 commit comments

Comments
 (0)