Skip to content

Callables in dicts lose their typing #14779

@kristjanvalur

Description

@kristjanvalur

Bug Report

typing of callables falls away in dicts, without warnings.

To Reproduce

from typing import Callable, Any, Mapping

def api(handlers: Mapping[Any, Callable[[str], int]]) -> None:
    pass

def handler1(arg):
    return 1

def handler2(arg:int) -> str:
    return "foo"

handlers = {
    1: handler1,
    2: handler2
}

api(handlers)  # no error
api(handlers={1:handler1, 2:handler2})  # Mypy correctly identifes handler2 as not matching the spec.

Expected Behavior

Running MyPy --strict on the above file should somehow warn us that the type of handler2 is being recast as a
callable taking Any

Actual Behavior
MyPy approves this file, even though an incorrect type is being passed in as a handler via the dict.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions