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

map(os.path.basename, ...): Argument 1 to "map" has incompatible type overloaded function; expected "Callable[[str], AnyStr]" #9864

Open
jdufresne opened this issue Dec 30, 2020 · 4 comments
Labels
bug mypy got something wrong topic-type-variables

Comments

@jdufresne
Copy link

jdufresne commented Dec 30, 2020

$ python --version
Python 3.9.1
$ mypy --version
mypy 0.790

Minimal code example:

import os

filenames = map(os.path.basename, ["hello/world.txt"])

Actual result:

$ mypy ~/test.py
/home/jon/test.py:3: error: Argument 1 to "map" has incompatible type overloaded function; expected "Callable[[str], AnyStr]"
Found 1 error in 1 file (checked 1 source file)

Expected: No error. This seems like an acceptable use of os.path.basename.


I'm happy to lend a hand to resolve this issue, but it is not clear to me how to fix it. Is this an issue with os.path.basename or map or something else?

@srittau
Copy link
Contributor

srittau commented Dec 30, 2020

This is a reduced version of the example above that shows the problem without involving typeshed types (or overloads):

from typing import Callable, List, TypeVar

_B = TypeVar("_B")
_T = TypeVar("_T")
_S = TypeVar("_S")

def bar(x: _B) -> _B:
    return x

def foo(func: Callable[[_T], _S], y: _T) -> _S:
    return func(y)

foo(bar, "")  # <-- error

This results in:

foo.py:13: error: Argument 1 to "foo" has incompatible type "Callable[[_B], _B]"; expected "Callable[[str], _B]"
Found 1 error in 1 file (checked 1 source file)

I believe this is a mypy problem. Could a mypy maintainer move this issue?

In typeshed we could probably work around the problem by adding additional overloads for str and bytes to basename(), but I would obviously prefer this to be solved in mypy.

@srittau
Copy link
Contributor

srittau commented Dec 30, 2020

Possibly related: #3644.

@JelleZijlstra JelleZijlstra transferred this issue from python/typeshed Dec 30, 2020
@hauntsaninja
Copy link
Collaborator

Also possibly related: #5738 (comment)

@AlexWaygood
Copy link
Member

Similar to #2389, but I'm not sure that it's a dupe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-type-variables
Projects
None yet
Development

No branches or pull requests

4 participants