Skip to content

'"ClassName" expects no type arguments, but 1 given' while inheriting from Iterable #2425

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
sametmax opened this issue Nov 9, 2016 · 5 comments
Labels

Comments

@sametmax
Copy link

sametmax commented Nov 9, 2016

I have a class looking like this:

from collection import Iterable, Iterator
class IterableWrapper(Iterator, Iterable, BaseWrapper):
    ...

And a function type definition later going:

# type: (...) -> IterableWrapper[tuple[T, T2]]

Mypy complains:

src/ww/wrappers/iterables.py:600: error: "IterableWrapper" expects no type arguments, but 1 given

While:

# type: (...) -> IterableWrapper and # type: (...) -> Iterable[tuple[T, T2]] don't get any warning.

I was expecting that, inheriting from Iterable, I could do IterableWrapper[tuple[T, T2]] like I would do Iterable[tuple[T, T2]] and benefit from code checking on yielded elements.

@sametmax sametmax changed the title ""Class" expects no type arguments, but 1 given" while inheriting from Iterable '"Class" expects no type arguments, but 1 given' while inheriting from Iterable Nov 9, 2016
@sametmax sametmax changed the title '"Class" expects no type arguments, but 1 given' while inheriting from Iterable '"ClassName" expects no type arguments, but 1 given' while inheriting from Iterable Nov 9, 2016
@gvanrossum
Copy link
Member

It looks like you want IterableWrapper to be generic. The way to do that is

T = TypeVar('T')
class IterableWrapper(Iterator[T], Iterable[T], BaseWrapper, Generic[T]):
    ...

Also you shouldn't need Iterable[T] because Iterator already inherits from Iterable.

I am presuming your BaseWrapper is not generic.

@JukkaL
Copy link
Collaborator

JukkaL commented Nov 9, 2016

Note that Iterable means Iterator[Any] instead of Iterator[T].

@sametmax
Copy link
Author

sametmax commented Nov 9, 2016

Righ. Thanks for this explanation. I think I'm going to write a big tutorial on mypy in the coming months.

@sametmax sametmax closed this as completed Nov 9, 2016
@sametmax sametmax reopened this Nov 9, 2016
@sametmax
Copy link
Author

sametmax commented Nov 9, 2016

Ooops. Forgot to ask: why do I have to inherit from Iterator[T] AND Generic[T]? Iterator[T] is not a generic already ?

@gvanrossum
Copy link
Member

gvanrossum commented Nov 10, 2016 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants