Skip to content

Definition of "__iter__" in "ItemsView" is incompatible with definition "Sequence" #5973

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

Closed
althonos opened this issue Nov 29, 2018 · 2 comments · Fixed by #6720
Closed

Definition of "__iter__" in "ItemsView" is incompatible with definition "Sequence" #5973

althonos opened this issue Nov 29, 2018 · 2 comments · Fixed by #6720
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-0-high

Comments

@althonos
Copy link

Hi there !

Trying to annotate a class inheriting from both a sequence and an items view will raise an error, although I believe both definitions are compatible in the following case:

from typing import *

K = TypeVar('K')
V = TypeVar('V')

class OrderedItemsView(Generic[K, V], ItemsView[K, V], Sequence[Tuple[K, V]]):
  def __iter__(self) -> Iterator[Tuple[K, V]]:
    return iter([])

This example raises:

test.py:7: error: Definition of "__iter__" in base class "ItemsView" is incompatible with definition in base class "Sequence"

although, for the two base classes, I believe __iter__ to be defined as Iterator[Tuple[K, V]].

@JelleZijlstra
Copy link
Member

JelleZijlstra commented Nov 29, 2018

Confirmed on master. Here's a more self-contained repro:

from typing import TypeVar, Generic, Tuple

K = TypeVar('K')
V = TypeVar('V')
T = TypeVar('T')

class ItemsView(Generic[K, V]):
    def __iter__(self) -> Tuple[K, V]: ...

class Sequence(Generic[T]):
    def __iter__(self) -> T: ...

class OrderedItemsView(ItemsView[K, V], Sequence[Tuple[K, V]]):
    def __iter__(self) -> Tuple[K, V]: ...

Gives me .../iv2.py:13: error: Definition of "__iter__" in base class "ItemsView" is incompatible with definition in base class "Sequence".

Interestingly, the method that produces this error (check_compatibility in checker.py) has a "# TODO: What if some classes are generic?".

@ilevkivskyi ilevkivskyi added bug mypy got something wrong priority-0-high false-positive mypy gave an error on correct code labels Nov 29, 2018
@ilevkivskyi
Copy link
Member

I think this is important to fix, otherwise multiple inheritance of generic classes is problematic.

ilevkivskyi added a commit that referenced this issue Apr 25, 2019
Fixes #5973

The fix is straightforward, I just copy a bit of logic (bind self and map type) from normal override check
to the multiple inheritance one (I also factored it out in a small method to avoid duplication).

Using this opportunity I also added/expanded some docstrings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-0-high
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants