Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incompatible return type for wrappers around ParamSpec-generics #12276

Closed
BvB93 opened this issue Mar 2, 2022 · 2 comments · Fixed by #11847
Closed

Incompatible return type for wrappers around ParamSpec-generics #12276

BvB93 opened this issue Mar 2, 2022 · 2 comments · Fixed by #11847
Labels
bug mypy got something wrong topic-paramspec PEP 612, ParamSpec, Concatenate

Comments

@BvB93
Copy link
Contributor

BvB93 commented Mar 2, 2022

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)
@BvB93 BvB93 added the bug mypy got something wrong label Mar 2, 2022
@cdce8p
Copy link
Collaborator

cdce8p commented Mar 3, 2022

/CC: @A5rocks

@A5rocks
Copy link
Contributor

A5rocks commented Mar 4, 2022

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.)

@JelleZijlstra JelleZijlstra added the topic-paramspec PEP 612, ParamSpec, Concatenate label Mar 19, 2022
JukkaL pushed a commit that referenced this issue Apr 7, 2022
This PR adds a new Parameters proper type to represent ParamSpec parameters 
(more about this in the PR), along with supporting the Concatenate operator.

Closes #11833
Closes #12276
Closes #12257
Refs #8645
External ref python/typeshed#4827

Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-paramspec PEP 612, ParamSpec, Concatenate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants