Skip to content
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

0.750 regression: Incompatible types with enumerate()/sorted() and property #8064

Closed
The-Compiler opened this issue Dec 3, 2019 · 1 comment

Comments

@The-Compiler
Copy link
Contributor

With 0.750 and this code:

from typing import TypeVar, Sequence

_T = TypeVar('_T')


class NeighborList:

    def __init__(self, items: Sequence[_T]) -> None:
        self._items = items

    def print_sorted(self) -> None:
        print(sorted(enumerate(self.items), key=lambda e: e[1]))  # <---

    @property
    def items(self) -> Sequence[_T]:
        return self._items


nl = NeighborList([1, 2, 3])
nl.print_sorted()

I get:

x.py:12: error: Argument 1 to "sorted" has incompatible type "enumerate[_T]"; expected "Iterable[Tuple[int, _T]]"
x.py:12: error: Argument 1 to "enumerate" has incompatible type "Sequence[_T]"; expected "Iterable[_T]"

I bisected this, it was introduced in 9d5d0e9 (part of #7933, fix for #7863).

When I remove the property and use self._items directly, things seem to be fine.

#7434 looks somewhat similar to me, but I don't think it's the same issue.

@ilevkivskyi
Copy link
Member

Mypy is correct, you should make the class generic using Generic[_T] base, otherwise these annotations don't make much sense (there is another issue to give a better error message in such cases, see #6520).

The-Compiler added a commit to qutebrowser/qutebrowser that referenced this issue Dec 3, 2019
The class should inherit from typing.Sequence[_T] to be generic - that is a
superclass of collections.abc.Sequence and seems to work on Python 3.5 as well.

See python/mypy#8064
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants