python / python/mypy

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

Open
#17,026 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic-usability
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the minimal example from mypy_bug.py with mypy and compare the assignment to seq with the equivalent assignment to not_seq. Trace how the type checker infers map's callable argument in these two contexts. Done means the inconsistent diagnostic is resolved and the reproduced case has regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.