Skip to content

Incompatible return type for wrappers around ParamSpec-generics #12276

Closed

Description

@BvB93

Bug Report

When creating a helper function around a ParamSpec-based generic I recently stumbled upon a spurious "Incompatible return value type" error.

Mypy seems to (internally) represent CustomCallable[~P]-parametrized generics as CustomCallable[Callable[P, None]] instead of just CustomCallable[P]. This internal representation can be exposed in certain situations though, resulting in false positives such as in the example below.

To Reproduce

from collections.abc import Callable
from typing import Generic, Any
from typing_extensions import ParamSpec

P = ParamSpec("P")


class CustomCallable(Generic[P]):
    def __init__(self, func: Callable[P, Any]) -> None: ...
    def __call__(self, *args: P.args, **kwargs: P.kwargs) -> None: ...


def func(func: Callable[P, Any]) -> CustomCallable[P]:
    # error: Incompatible return value type (got "CustomCallable[Callable[P, None]]", expected "CustomCallable[P]")
    return CustomCallable(func)

Your Environment

  • Mypy version used: mypy 0.931
  • Mypy command-line flags: n.a.
  • Mypy configuration options from mypy.ini (and other config files): n.a.
  • Python version used: 3.10.2
  • Operating system and version: Windows 10 (build 19044.1526)

Activity

  1. added
    bugmypy got something wrong
    on Mar 2, 2022
  2. cdce8p commented on Mar 3, 2022

    @cdce8p
    Collaborator
  3. A5rocks commented on Mar 4, 2022

    @A5rocks
    Collaborator

    Yeah I wish mypy did not represent P as (...) -> None (I added an alternate representation for concatenate and paramspec literals :-)...

    This is definitely a bug that should be solved though. I'll add it to my todo list for after my current paramspec PR is merged :^)

    (Unless you want to give this a try: Callable[P, None] should be reducable to P via CallableType.param_spec(), unless I'm misremembering the method.)

  4. added a commit that references this issue on Apr 7, 2022
    07d8878
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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