-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Attempt to create a subclass of a generic class results in TypeError: '_AnyCall' object isn't subscriptable #755
Comments
Can you add some more detail on the type checker you are using, the python version on your host and the stubs package and version you are using? |
If I test your sample in Pyright's playground it also shows an error ( but a different one ) Code sample in pyright playground from typing import TypeVar, Generic, List, Tuple
T = TypeVar('T')
class Foo(Generic[T]):
...
# Something does not exist
class Baz(Foo[Something]):
...
# Something needs to exist
Something = List[Tuple]
class Bar(Foo[Something]):
... |
And a quick test does seem not show an obvious error.
from typing import Dict, Generic, TypeVar
from typing_extensions import reveal_type
T = TypeVar("T")
from machine import Pin, Signal
class Registry(Generic[T]):
def __init__(self) -> None:
self._store: Dict[str, T] = {}
def set_item(self, k: str, v: T) -> None:
self._store[k] = v
def get_item(self, k: str) -> T:
return self._store[k]
inputs = Registry[Pin]()
inputs.set_item("button", Pin(4, Pin.IN))
inputs.set_item("X", Pin(18, Pin.IN))
inputs.set_item("Y", Pin(19, Pin.IN))
outputs = Registry[Signal]()
outputs.set_item("Player-A", Signal(Pin(5, Pin.OUT)))
outputs.set_item("Player-B", Signal(Pin(6, Pin.OUT)))
outputs.set_item("Game over", Signal(Pin(8, Pin.OUT), invert=True))
b = inputs.get_item("button")
reveal_type(b) # Expected `Pin`, got `Pin`
x = inputs.get_item("X")
reveal_type(x) # Expected `Pin`, got `Pin`
game_over = outputs.get_item("Game over")
reveal_type(game_over) # Expected `Signal`, got `Signal` |
Couple of things, sorry for not being sufficiently clear:
|
Then it may be that you need to add the round brackets , that I tend to forget as well Or in your sample |
Note that I do not have any |
E.g.
from typing import TypeVar, Generic, Any
T = TypeVar('T')
class Foo(Generic[T]):
pass
class Bar(Foo[Any]):
pass % mypy test.py typing.mpy 100%[=================================================>] 1.63K --.-KB/s in 0s 2024-05-28 21:12:06 (15.9 MB/s) - ‘typing.mpy’ saved [1669/1669]
|
Thanks , now I know what to look for . |
Ah, Generic is subscriptable, but doing so returns AnyCall which isn't. Maybe subscriptable should inherit from AnyCall, but return a new subscriptable so it can be used recursively like this. |
@ANogin , I think I can solve your problem based on Andrew`s suggestion. typings.py class _AnyCall:
def __init__(*args, **kwargs):
pass
def __call__(*args, **kwargs):
pass
def __getitem__(self, arg): # <--
return _anyCall
_anyCall = _AnyCall()
class _SubscriptableType:
def __getitem__(self, arg):
return _anyCall |
#755 typing*: Update .mpy files test: add more typing runtime tests fix: shouldnot be checked in to test ignore checking in typing files
I have merged the updated typings.py/.mpy |
I have
And micropython complains:
for the
Foo[Something]
...The text was updated successfully, but these errors were encountered: