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

Unclear error message: incompatible type "type[str]"; expected "Callable[[_T], _T]" #17026

Open
Jacobfaib opened this issue Mar 14, 2024 · 5 comments

Comments

@Jacobfaib
Copy link

Jacobfaib commented Mar 14, 2024

To Reproduce

from collections.abc import Sequence
from typing import TypeVar

_T = TypeVar("_T")

def foo(seq: Sequence[_T]) -> None:
    seq = tuple(map(str, seq))
$ mypy ./mypy_bug.py
mypy_bug.py: note: In function "foo":
mypy_bug.py:7:19: error: Argument 1 to "map" has incompatible type "type[str]"; expected "Callable[[_T], _T]"
[arg-type]
      seq = tuple(map(str, seq))
                      ^~~
Found 1 error in 1 file (checked 1 source file)

Interestingly enough, this error does not fire if you assign to something other than seq:

def foo(seq: Sequence[_T]) -> None:
    not_seq = tuple(map(str, seq))  # no problem, no mypy error
  • Mypy version used: mypy 1.9.0 (compiled: yes)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: Python 3.12.2
@Jacobfaib Jacobfaib added the bug mypy got something wrong label Mar 14, 2024
@Jacobfaib Jacobfaib changed the title Argument 1 to "map" has incompatible type "type[str]"; expected "Callable[[_T], _T]" [1.9.0] Argument 1 to "map" has incompatible type "type[str]"; expected "Callable[[_T], _T]" Mar 14, 2024
@erictraut
Copy link

You're attempting to assign a value of type Sequence[str] to a parameter that has a declared type of Sequence[_T]. This is a type violation, so mypy is correct to generate an error here.

Mypy's error message could arguably be made more clear in this case. For example, here is now pyright reports the error for the same code.

@Jacobfaib
Copy link
Author

You're attempting to assign a value of type Sequence[str] to a parameter that has a declared type of Sequence[_T]. This is a type violation, so mypy is correct to generate an error here.

Sure, and mypy is indeed silenced if you run with --allow-redefinition, but the point here is:

  1. It is not clear from the diagnostic that the original sin is type redefinition.
  2. mypy's diagnostic message is arguably wrong. str (or more specifically, type[T] which accepts a single argument) is a sub-type of Callable[[T], T] no?

@JelleZijlstra
Copy link
Member

str is not a subtype of Callable[[T], T], because such a callable should return its input type unchanged.

@AnkitSharma-29
Copy link

The issue you're encountering is with the map() function call. The error message indicates that there's an incompatible type being passed as the first argument to map(). In this case, map(str, seq), you're trying to convert each element of seq to a string using the str function.

To fix this issue and make it type-check cleanly with mypy, you can explicitly specify the function type for map() by using a lambda function. Here's how you can do it:
`from collections.abc import Sequence
from typing import TypeVar, Callable

_T = TypeVar("_T")

def foo(seq: Sequence[_T]) -> None:
seq = tuple(map(lambda x: str(x), seq)) # Use lambda to specify the function type
# Now mypy should not raise any errors
`

By using a lambda function, you're explicitly specifying the type of the function being passed to map(), which resolves the incompatible type error.

@JukkaL
Copy link
Collaborator

JukkaL commented Mar 15, 2024

The issue here is that the error message is hard to figure out. We can leave this issue open, but it will be about improving the error message (I'll update the title).

@JukkaL JukkaL added topic-usability and removed bug mypy got something wrong labels Mar 15, 2024
@JukkaL JukkaL changed the title [1.9.0] Argument 1 to "map" has incompatible type "type[str]"; expected "Callable[[_T], _T]" Unclear error message: incompatible type "type[str]"; expected "Callable[[_T], _T]" Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants