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

list doesn't seem to be a valid callable #9253

Open
Akuli opened this issue Aug 3, 2020 · 7 comments
Open

list doesn't seem to be a valid callable #9253

Akuli opened this issue Aug 3, 2020 · 7 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code topic-type-variables

Comments

@Akuli
Copy link
Contributor

Akuli commented Aug 3, 2020

don't know if this is bug or feature request

def foo() -> None:
    iterator = map(list, ['hello', 'world'])  # error: Argument 1 to "map" has incompatible type "Type[List[Any]]"; expected "Callable[[str], List[_T]]"
    reveal_type(iterator)

this works, but feels like a workaround:

iterator = map(lambda foo: list(foo), ["hello", "world"])

python 3.7, mypy from latest master (20b4628), no flags

@gvanrossum
Copy link
Member

I’m sure this is well known and probably a duplicate. You can help by searching the tracker for an issue that describes this.

@Akuli
Copy link
Contributor Author

Akuli commented Aug 3, 2020

couldn't find another issue quite like this, but i tried other callable builtin classes

map(dict, ['12', '34'])       # error
map(enumerate, ['12', '34'])  # error
map(float, ['12', '34'])      # ok
map(int, ['12', '34'])        # ok
map(list, ['12', '34'])       # error
map(str, ['12', '34'])        # ok
map(tuple, ['12', '34'])      # error

seems to be problem with generics? no generic classes work and all non-generics work

@gvanrossum
Copy link
Member

Hm, that's interesting. Seems only the generic classes have this error. Maybe that's a clue for someone.

@Akuli
Copy link
Contributor Author

Akuli commented Aug 22, 2020

It could be the "known very old bug that generic functions when passed as arguments to another generic functions don't work". It seems that nobody can find that from the issue tracker, but some people know about it. #9265 (comment)

@JukkaL
Copy link
Collaborator

JukkaL commented Sep 11, 2020

#1507 is a similar issue, though maybe we can keep this open as well, since the fix for type objects might be different and the error message looks different.

@JukkaL JukkaL added bug mypy got something wrong false-positive mypy gave an error on correct code topic-type-variables labels Sep 11, 2020
@tiagocabo
Copy link

I have a similar issue when I try
map(str, [1, 2])

having this error
Incompatible types in assignment (expression has type "map[str]", variable has type "Optional[List[int]]")

@Akuli
Copy link
Contributor Author

Akuli commented Nov 17, 2022

@tiagocabo That is not a bug: map doesn't return a list, it returns an iterator, and you are trying to assign it to a list variable. You can annotate the variable you're assining map(str, [1, 2]) to as

variable_name: Optional[Iterable[int]]

or (with Python 3.10+ or from __future__ import annotations)

variable_name: Iterable[int] | None

Alternatively, you can convert the iterator to a list with list(map(str, [1, 2])).

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 topic-type-variables
Projects
None yet
Development

No branches or pull requests

4 participants