-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
"No overload variant of "next" matches argument types" for types.Iterable #3490
Comments
Try Iterator instead. |
Sometimes I wonder if mypy should warn if an incompatible return type is specified on a generator... |
Oh, but it does check the return type. Only Generator and its superclasses are allowed, i.e. Iterator, Iterable, and object. This gives the author some freedom in how much they freedom they want for future changes to that particular function (maybe they'd like to be able to turn it into something else that can be iterated over without breaking the callers). It's just that next() wants an Iterator. But most of the time people don't use next() with a generator, they just use a for-loop over it. |
The error message isn't very useful though. Mypy should really tell that the expected type is |
so this is expected behaviour? things = [1, 2, 3]
first = next(things) # No overload variant of "next" matches argument type List[int] i know this is more of a support question really, but why is this an error? seems like perfectly cromulent code to me... |
|
omg how. embarrassing. so sorry everyone. thanks Jelle. for posterity, what I wanted to do was something like # return the first thing in things, or None if things is empty
next(things, None) # works for iterators/generators
# instead you might prefer
things[0] if things else None # for a list/iterable. |
Here's a short example of what I'd think should be a correctly typed program...
Running
mypy
on this (under Python 3.4, no strict mode, no other flags at all) results in the following error:Is this a bug, or am I doing something wrong?
The text was updated successfully, but these errors were encountered: