Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Regression in 1.2.0: callback protocol with Callable and Concatenate #15065

Closed
@Rogdham

Description

@Rogdham

Bug Report

The code below passed with mypy 1.1.1 but not in 1.2.0

To Reproduce

from typing import Concatenate, Callable, ParamSpec, Protocol

P = ParamSpec("P")


class Decorator(Protocol):
    def __call__(self, fct: Callable[Concatenate[str, P], int]) -> Callable[P, int]:
        ...


def foo(value: int) -> Decorator:
    def bar(fct: Callable[Concatenate[str, P], int]) -> Callable[P, int]:
        raise NotImplementedError

    return bar

I haven't been able to strip down any further, it seems that it has something to do with both Callable and Concatenate.

Expected Behavior

No error (as in mypy 1.1.1).

Actual Behavior

The following error is displayed:

issue.py:15: error: Incompatible return value type (got "Callable[[Callable[[str, **P], int]], Callable[P, int]]", expected "Decorator")  [return-value]
issue.py:15: note: "Decorator.__call__" has type "Callable[[Arg(Callable[[str, **P], int], 'fct')], Callable[P, int]]"
Found 1 error in 1 file (checked 1 source file)

Note that I have tried to change the Decorator definition to make fct parameter positional-only:

class Decorator(Protocol):
    def __call__(self, fct: Callable[Concatenate[str, P], int], /) -> Callable[P, int]:
        ...

Which resulted in the error message being even more confusing:

issue.py:15: error: Incompatible return value type (got "Callable[[Callable[[str, **P], int]], Callable[P, int]]", expected "Decorator")  [return-value]
issue.py:15: note: "Decorator.__call__" has type "Callable[[Callable[[str, **P], int]], Callable[P, int]]"
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.2.0
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.11.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-paramspecPEP 612, ParamSpec, Concatenate

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions