Skip to content

ParamSpec does not consider substitution of @overloads #13540

Closed as not planned
Closed as not planned
@sbrudenell

Description

@sbrudenell

Bug Report

It looks like mypy only considers the first @overload of a function, when considering substitutions for a ParamSpec.

In particular this affects use of asyncio.to_thread(f, arg), if f has @overloads.

To Reproduce

import asyncio
from collections.abc import Callable
from typing import overload
from typing import TypeVar

from typing_extensions import ParamSpec


@overload
def f(x: int) -> None:
    ...


@overload
def f(x: str) -> None:
    ...


def f(x) -> None:
    pass


_P = ParamSpec("_P")
_R = TypeVar("_R")


# type signature modeled after asyncio.to_thread()
def generic(func: Callable[_P, _R], *args: _P.args, **kwargs: _P.kwargs) -> _R:
    ...


# using first overload: passes
generic(f, 1)
# using second overload: fails
generic(f, "test")

Expected Behavior

$ mypy t.py
Success: no issues found in 1 source file

Actual Behavior

$ mypy t.py
t.py:35: error: Argument 2 to "generic" has incompatible type "str"; expected "int"
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 0.971 (compiled: yes)
  • Mypy command-line flags: mypy t.py
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10.4
  • Operating system and version: Ubuntu 22.04.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions