Skip to content

Type is incompatible with Callable when using Concatenate #14321

Closed
@ringohoffman

Description

@ringohoffman

Bug Report

As expected, a class A can be typed as Type[A] or Callable[P, A], where P is a ParamSpec.

Using Concatenate then, you should also be able to bind A to Callable[Concatenate[<type>, P], A], where <type> is the type of A's first constructor parameter, but mypy currently errors when you attempt this.

To Reproduce

gist: https://gist.github.com/mypy-play/1cab71a52f3d7f3a4e9045e6e68eabe0
mypy-playground: https://mypy-play.net/?mypy=latest&python=3.10&gist=1cab71a52f3d7f3a4e9045e6e68eabe0

from __future__ import annotations

from typing import Callable, Concatenate, ParamSpec

P = ParamSpec("P")


class A:
    def __init__(self, a_param_1: str) -> None:
        ...

    @classmethod
    def add_params(cls: Callable[P, A]) -> Callable[Concatenate[float, P], A]:
        def new_constructor(i: float, *args: P.args, **kwargs: P.kwargs) -> A:
            return cls(*args, **kwargs)
        return new_constructor
    
    @classmethod
    def remove_params(cls: Callable[Concatenate[str, P], A]) -> Callable[P, A]:
        def new_constructor(*args: P.args, **kwargs: P.kwargs) -> A:
            return cls("my_special_str", *args, **kwargs)
        return new_constructor

A.add_params()  # OK
A.remove_params()  # [misc] mypy(error): Invalid self argument "Type[A]" to attribute function "remove_params" with type "Callable[[Callable[[str, **P], A]], Callable[P, A]]"

Expected Behavior

No error should be raised.

Actual Behavior

error: Invalid self argument "Type[A]" to attribute function "remove_params" with type "Callable[[Callable[[str, **P], A]], Callable[P, A]]"  [misc]

Your Environment

  • Mypy version used: mypy 0.991 (compiled: yes)
  • Python version used: 3.10.6

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