Skip to content

Callable protocol definition and ParamSpec issue #5844

@lmazuel

Description

@lmazuel

Describe the bug

I define a callable protocol following the official doc of Annotating callable object. Especially the part of the end that says:

Callable cannot express complex signatures such as functions that take a variadic number of arguments, overloaded functions, or functions that have keyword-only parameters. However, these signatures can be expressed by defining a Protocol class with a call() method

In particular, I define a decorator that adds a keyword only argument to the function it decorates, and I use a callable protocol to define this kwarg-only parameter. I can't get pyright to be happy while mypy is super happy with it.

Code or Screenshots

This is a mypy playground:
https://mypy-play.net/?mypy=latest&python=3.11&gist=bba538a9212466a819ffb0343c925b84

You can see that mypy has no issue to type check everything, include on purposed wrong code.

While I run the same file on Pyright, I get this:

  callable_paramspec.py:11:30 - error: Keyword parameter "something_added" cannot appear in signature after ParamSpec args parameter (reportGeneralTypeIssues)
  callable_paramspec.py:20:28 - error: Keyword parameter "something_added" cannot appear in signature after ParamSpec args parameter (reportGeneralTypeIssues)
  callable_paramspec.py:24:16 - error: Expression of type "_Wrapped[P@my_decorator, T@my_decorator, (*args: P.args, something_added: bool = False, **kwargs: P.kwargs), T@my_decorator]" cannot be assigned to return type "TracedMethod[P@my_decorator, T@my_decorator]"
    "_Wrapped[P@my_decorator, T@my_decorator, (*args: P.args, something_added: bool = False, **kwargs: P.kwargs), T@my_decorator]" is incompatible with protocol "TracedMethod[P@my_decorator, T@my_decorator]"
      "__call__" is an incompatible type
        Type "(*args: P.args, something_added: bool = False, **kwargs: P.kwargs) -> T@my_decorator" cannot be assigned to type "(*args: P.args, something_added: bool = False, **kwds: P.kwargs) -> T@TracedMethod"
          Parameter 1: type "*tuple[P.args, ...]" cannot be assigned to type "*tuple[P.args, ...]"
            "builtins.tuple" is incompatible with "builtins.tuple"
          Parameter 3: type "P.kwargs" cannot be assigned to type "P.kwargs"
            Type "P.kwargs" cannot be assigned to type "P.kwargs" (reportGeneralTypeIssues)

I'm using pyright 1.1.324, and Python 3.11.4.

Metadata

Metadata

Assignees

No one assigned

    Labels

    as designedNot a bug, working as intendedbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions