Skip to content

Support Decorators Maintaining Signatures of Generic Functions #27404

Closed
@WillAyd

Description

@WillAyd

As a follow up to #27393 I think I made a mistake in just typing some of the return values to be Callable. The following script highlights that issue:

from pandas.util._decorators import Appender

def func1(num: int) -> int:
    return num

@Appender("")
def func2(num: int) -> int:
    return num

reveal_type(func1)
reveal_type(func2)

If you run that from the project root you'll get something like the following:

$ mypy foo.py
foo.py:10: note: Revealed type is 'def (num: builtins.int) -> builtins.int'
foo.py:11: note: Revealed type is 'def (*Any, **Any) -> Any'

We probably want to maintain the types of the signature for func2 so I think need to use TypeVar for generic support.

More info on the mypy side in:

python/mypy#3113
python/mypy#1551

So I think need something like:

T = TypeVar('T', bound=Callable[..., Any])
# then decorators can return
-> Callable[[T], T]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Typingtype annotations, mypy/pyright type checking

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions