Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mypy: Signature of "__getitem__" incompatible with supertype "Sequence"

I have a class that inherits from MutableSequence like this:

class QqTag(MutableSequence):
    def __init__(self):
        self._children = []
    def __getitem__(self, idx: int) -> 'QqTag':
        return self._children[idx]

mypy complains that Signature of "__getitem__" incompatible with supertype "Sequence".

In Sequence, this method is defined as:

@abstractmethod
def __getitem__(self, index):
    raise IndexError

So, what's the problem and why mypy isn't happy with my implementation?

like image 337
Ilya V. Schurov Avatar asked Oct 11 '17 09:10

Ilya V. Schurov


1 Answers

As mentioned in comments, a typeof slice can also be passed. Ie, change idx: int to idx: Union[int, slice].

This will make mypy happy (at least on my machine ;):

class QqTag(MutableSequence):
    def __init__(self):
        self._children = []

    def __getitem__(self, idx: Union[int, slice]) -> 'QqTag':
        return self._children[idx]
like image 126
Zachary Ryan Smith Avatar answered Sep 23 '22 05:09

Zachary Ryan Smith