87992

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?

Answer1:

As mentioned in comments, a typeof slice can also be passed.

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]

Recommend

  • Mypy error - incompatible types in assignment
  • mypy Error TypeVar with Value Restriction and Union of Unions / Optional Cannot Pass generic contain
  • How to add a custom flag to IPython's magic commands? (.ipy files)
  • In Dart, what's the difference between List.from and .of, and between Map.from and .of?
  • Cordova 2.7.0 PushPlugin “cordova cannot be resolved” and “ctx cannot be resolved”
  • Subclassing Sequence with proper type hints in Python
  • How to replace numbers with order in (python) list
  • How to rename file with a sequence that restarts if certain matches exist
  • In Scala, why can I use `Unit` as return type here?
  • How to call firefox addon function with onclick in html
  • uninitialized AudioTrack exception when I try to generate tone on 22nd time
  • Product and product template in Odoo 10
  • Outlook Com not registered
  • Android RecyclerView Blank Space
  • MATLAB quick find of a vector in matrix
  • Observable.forkJoin() TS2322 error after updating to TypeScript 2.4.1 and Rxjs 5.4.2
  • Create ranking for vector of double
  • With one EDMX file use multiple connection strings that relate to multiple databases
  • How can I include multiple models in one view for in a Joomla 3.x component built with Component Cre
  • Using Paypal Pro in Omnipay
  • pygame.init() shows as undefined variable after installing Pygame
  • How to eliminate warning for passing multidimensional array as const multidimensional array?
  • How to use animated gif in Firemonkey?
  • @tailrec why does this method not compile with 'contains a recursive call not in tail position&
  • Javascript Array, Object, Date not defined
  • Android app gives error “BatteryStatsImpl: reading network stats”
  • C: Incompatible pointer type initializing
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Django query for large number of relationships
  • Sorting a 2D array using the second column C++
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?