You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
When I annotate a method with, for example, @capture_span, the method loses its type hints and becomes just a Callable.
For example, Pylance gives the following information tooltips (as commented to the right of the reveal_type calls):
importelasticapmasapmfromtyping_extensionsimportreveal_typedeftest (a: int, b: str, c: float) ->bytes:
returnb"abc"@apm.capture_span()deftest_with_apm (a: int, b: str, c: float) ->bytes:
returnb"abc"reveal_type(test) # Type of "test" is "(a: int, b: str, c: float) -> bytes"reveal_type(test_with_apm) # Type of "test_with_apm" is "(...) -> Any"
Describe the solution you'd like
Just use TypeVar on the __call__ method signature of annotation methods, example:
After this small change, the reveal_type reports the expected type:
importelasticapmasapmfromtyping_extensionsimportreveal_type@apm.capture_span()deftest_with_apm (a: int, b: str, c: float) ->bytes:
returnb"abc"reveal_type(test_with_apm) # Type of "test_with_apm" is "(a: int, b: str, c: float) -> bytes"
Describe alternatives you've considered
I've considered creating a pull request myself to solve this, but the CONTRIBUTING.md file indicates that an issue should be opened first.
Additional context
Screenshot of VSCode's pylance type hints without the change:
Screenshot of VSCode's pylance type hints with the change:
The text was updated successfully, but these errors were encountered:
That's a great improvement! Thanks for proposing this, a PR would be great! Do you think it would also make sense to annotate the actual decorator (the decorated function) with ParamSpec? That is, if we can find a way to do it in a backwards-compatible way, as ParamSpec is only available in Python 3.10...
Thanks for the positive feedback! I think there is an implementation of the ParamSpec on the typing-extensions package, but it is for python >= 3.7, so compatibility would still be broken for 3.5 and 3.6. Also, there is the issue of adding another requirement.
Anyways, I think there is no need to annotate the internal method, since functools.wrap already solve the issue for runtime, and the TypeVar ensures the typing of the return.
I will start working on the PR as soon as possible.
marcoffee
added a commit
to marcoffee/apm-agent-python
that referenced
this issue
Sep 30, 2022
Is your feature request related to a problem? Please describe.
When I annotate a method with, for example,
@capture_span
, the method loses its type hints and becomes just aCallable
.For example,
Pylance
gives the following information tooltips (as commented to the right of thereveal_type
calls):Describe the solution you'd like
Just use
TypeVar
on the__call__
method signature of annotation methods, example:After this small change, the
reveal_type
reports the expected type:Describe alternatives you've considered
I've considered creating a pull request myself to solve this, but the
CONTRIBUTING.md
file indicates that an issue should be opened first.Additional context
Screenshot of VSCode's pylance type hints without the change:
Screenshot of VSCode's pylance type hints with the change:
The text was updated successfully, but these errors were encountered: