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

Support Decorators Maintaining Signatures of Generic Functions #27404

Closed
WillAyd opened this issue Jul 15, 2019 · 7 comments · Fixed by #28128
Closed

Support Decorators Maintaining Signatures of Generic Functions #27404

WillAyd opened this issue Jul 15, 2019 · 7 comments · Fixed by #28128
Labels
Typing type annotations, mypy/pyright type checking

Comments

@WillAyd
Copy link
Member

WillAyd commented Jul 15, 2019

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]
@WillAyd WillAyd added the Typing type annotations, mypy/pyright type checking label Jul 15, 2019
@WillAyd WillAyd added this to the Contributions Welcome milestone Jul 15, 2019
@topper-123
Copy link
Contributor

Does reveal_type return the proper types if this change is made?

@topper-123
Copy link
Contributor

Possibly related to #13875.

@WillAyd
Copy link
Member Author

WillAyd commented Aug 27, 2019

@simonjayhawkins you recently had a solution for this right? Interested in extending it too pandas.util._decorators?

@simonjayhawkins
Copy link
Member

@WillAyd I'm reopening this as #28128 did not close this issue. solved for Appender and Substitution only

dd95741 will fix deprecate_kwarg and probably similar for rewrite_axis_style_signature

@WillAyd
Copy link
Member Author

WillAyd commented Sep 6, 2019

Sounds good - thanks for taking a look

@jbrockmendel
Copy link
Member

Does being py37+ make this any more solvable?

@WillAyd
Copy link
Member Author

WillAyd commented Sep 21, 2020

This should already be resolved here:

F = TypeVar("F", bound=FuncType)

@WillAyd WillAyd closed this as completed Sep 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants