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

Calling next over generic iterator inside list comprehension inside function call gives not callable error #409

Closed
juanpablos opened this issue Sep 24, 2020 · 3 comments
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@juanpablos
Copy link

Environment data

  • Language Server version: 2020.9.6
  • OS and version: Windows 10
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.8.5

Expected behaviour

No error when calling next.

Actual behaviour

I get an error that next is not callable.

Code Snippet / Additional information

Pylance 2020.9.5 and older does not have this issue, just encountered it in 2020.9.6.

Maybe this is too specific of an issue, but just encountered this (the builtin next function gives error that it is not callable):

T = TypeVar("T")

class SomeProtocol(Protocol[T]): ...

def some_function(arg: SomeProtocol[T]): ...

class SomeClass(Generic[T]):
	def __init__(self, it: Iterator[T]):
		# for some reason, here I get an error:
		# "next" has type "Overload[(__i: Iterator[_T]) -> _T, (__i: Iterator[_T], default: _VT) -> (_T | _VT)]" and is not callable
		some_function([next(it) for _ in range(10)])

	def my_function(self, it: Iterator[T]):
		# error:
		# "next" has type "Overload[(__i: Iterator[_T]) -> _T, (__i: Iterator[_T], default: _VT) -> (_T | _VT)]" and is not callable
		some_function([next(it) for _ in range(10)])

On a simpler example (but not realistic, just to show the error):

# I know T has no meaning here but just for the example
# by the way Pylance also does not raise error for the no meaning of T in this context
def iterator() -> Iterator[T]: ...

it = iterator()
# type T
value = next(it) # no problem here
# type List[T]
ten_values = [next(it) for _ in range(10)] # no problem here
# no error if pass the generated list
some_function(ten_values) # no problem

# error:
# "next" has type "Overload[(__i: Iterator[_T]) -> _T, (__i: Iterator[_T], default: _VT) -> (_T | _VT)]" and is not callable
some_function([next(it) for _ in range(10)])

# no error if I just unpack the iterator
some_function([*it])

The error is the same (that next is not callable) if I change SomeProtocol from a Protocol to just some Generic class.
If I fix the iterator type as e.g. Iterator[int] the problem disappears. If I remove the type of arg the error also goes away.

Here is what I just said with Iterator[int]:

T = TypeVar("T")
class SomeProtocol(Protocol[T]): ...
def some_function(arg: SomeProtocol[T]): ...
# change here Iterator[T] to Iterator[int]
def iterator() -> Iterator[int]: ...

it = iterator()
some_function([next(it) for _ in range(10)]) # no errors

It also disappears if T is resolved to some type:

def iterator(var: T) -> Iterator[T]: ...

it = iterator(1)
some_function([next(it) for _ in range(10)]) # no errors
@erictraut erictraut added the bug Something isn't working label Sep 24, 2020
@erictraut
Copy link
Contributor

Thanks for the detailed bug report. I'll look into it.

@github-actions github-actions bot removed the triage label Sep 24, 2020
@erictraut
Copy link
Contributor

This will be fixed in the next release.

@erictraut erictraut added the fixed in next version (main) A fix has been implemented and will appear in an upcoming version label Sep 25, 2020
@jakebailey
Copy link
Member

This issue has been fixed in version 2020.9.7, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/master/CHANGELOG.md#202097-30-september-2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

3 participants