python / python/mypy

False positive: Callable arg types more strict than required

Open
#15,328 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Struggled to name this properly but here's a repro:

import typing

# Want to accept any callable with a string arg
CallableT = typing.Callable[[str], None]

def func(callable_m: dict[str, CallableT]) -> None:
    callable_m['first']("a")
    
    
def first(a: str) -> None:
    return None
    
def second(a: str) -> None:
    return None

def third(b: str) -> None:
    return None   

# This mapping will fail because the callables will have type:
# Callable[[Arg(str, 'a')], None]    
failed_mapping = {
    'first': first,
    'second': second
}
func(failed_mapping)

# By renaming the arg in `third` from `a` to `b` everything is groovy
passing_mapping = {
    'first': first,
    'third': third
}
func(passing_mapping)

https://mypy-play.net/?mypy=latest&python=3.11&gist=ff039c923e370f79a680c463ef627810

Easy workaround with renaming the arg but is a bit of a pain

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 with the supplied Python reproduction and mypy-play link, then trace the callable compatibility check that compares parameter names. Confirm the behavior with a focused regression test; done means compatible callables such as first and second are accepted without renaming their parameters.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.