Errors with type annotation handling in 1.7.0
Hi,
Ver 1.7.0 seems to have introduced issues with the runtime handling of certain type annotations. The cases I have found so far are demonstrated by the following contrived minimal examples:
def foo[K](arg: K) -> K:
ret: K = arg
return ret
foo(1)
causes exception NameError: name 'K' is not defined
def bar(x) -> None:
y: Any[int] = None
bar(1)
gives exception TypeError: type 'Any' is not subscriptable
Both work properly in ver 1.6.4 and are OK with Pylance.
Note that these problems arise with the declaration of the annotated variables above. Other uses of similar constructs seem to work OK, e.g:
def baz(x: Any[int] = None) -> None:
pass
baz()
works fine.
Any thoughts?
pyscript 1.6.4 / 1.7.0 HA OS core 2025.12.1 on RPi4 & RPi3
The first example uses PEP 695, which isn’t supported by Pyscript yet. Working code:
from typing import TypeVar
K = TypeVar("K")
def foo[K](arg: K) -> K:
ret: K = arg
return ret
The second example is not valid in native Python. Any is not parameterizable.
$ python
Python 3.13.11 (main, Dec 5 2025, 16:06:33) [Clang 17.0.0 (clang-1700.4.4.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from typing import Any
>>> y: Any[int] = None
Traceback (most recent call last):
File "<python-input-1>", line 1, in <module>
y: Any[int] = None
~~~^^^^^
TypeError: type 'Any' is not subscriptable
>>>
Thanks for the clear examples, @RussEaton. I checked both on current master.
The first one, def foo[K](arg: K) -> K: ret: K = arg; return ret, now works and foo(1) returns 1. The NameError went away with https://github.com/custom-components/pyscript/pull/915, which changed how an annotation is evaluated when it names something that isn't defined yet.
The second one, y: Any[int] = None inside a function, still raises TypeError: type 'Any' is not subscriptable. Regular Python never evaluates annotations on local variables inside a function, so the same code runs without error there. Pyscript evaluates the annotation when it runs the assignment, so the error shows up. That is a difference from Python that is still open. Until it is fixed, you can remove the annotation or use Any without a subscript. I think this issue should stay open for the second case.