I'm trying to use a specialised subclass of List to create a TokenList that is backed by a regular list, but also contains a metadata dict with key/values. Everything works great, until I wanted to add types to this, and now I'm getting this error:
error: Signature of "__setitem__" incompatible with supertype "list"
... and the error is the first overload below:
import typing as T
class Token(dict):
...
class TokenList(T.List[Token]):
...
@T.overload
def __setitem__(self, key: int, tokens: T.Union[dict, Token]) -> None: ... # noqa, pragma: no cover
@T.overload
def __setitem__(self, key: slice, tokens: T.Union[T.Iterable[T.Union[dict, Token]], 'TokenList']) -> None: ... # noqa, pragma: no cover
def __setitem__(self, key, tokens): # noqa: F811
if isinstance(key, int):
token = tokens
token = self._dict_to_token_and_set_defaults(token)
super(TokenList, self).__setitem__(key, token)
else:
tokens = [self._dict_to_token_and_set_defaults(token) for token in tokens]
super(TokenList, self).__setitem__(key, tokens)
I've tried to find the type defintition for list to compare it to mine, but hasn't been able to find it.
Except for ignoring this error and going on with my life, is there any way around this? Any pointers appreciated.
(I'm using mypy 0.910 and Python 3.9.1 if that matters.)