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

How can I make Pylance display the docstring of decorated function not of the decorator? #125

Closed
AlexVillarra opened this issue Jul 16, 2020 · 8 comments
Labels
fixed in next version (main) A fix has been implemented and will appear in an upcoming version needs documentation Improvements or additions to documentation

Comments

@AlexVillarra
Copy link

I cant' seem to be able to obtain the docstring and autocomplete functionality for a decorated function. In fact, as can be seen in the attached images, the docstring and autocomplete are showing the information for the decorator, not the decorated function.
I was wondering if this is the expected behavior, and if it is, how can I get the information for the actual function displayed instead?

For information, if useful, I'm using VS Code1.47.1 for Windows 10, Pylance 2020.7.2, and python 3.7.7 (conda based).
with_decorator
without_decorator

@erictraut
Copy link
Contributor

Thanks for posting the question. Please see this issue for an explanation and a solution.

We should consider documenting this, since several users have hit the problem recently.

@erictraut erictraut added the needs documentation Improvements or additions to documentation label Jul 16, 2020
@AlexVillarra
Copy link
Author

Perfect, I was quite confused as of why it was not working. I tried the solution proposed in the issue you mentioned and it works like a charm :) .

@jakebailey
Copy link
Member

jakebailey commented Jul 16, 2020

I wonder if there's anything we can do to help detect decorator signatures past documenting the fix with annotations. It seems pretty common to have untyped decorators. (Are the *args and **kwargs commonality due to reduced examples, or a common pattern to quickly wrap any function?)

@erictraut
Copy link
Contributor

If the decorator is completely unannotated and we can't infer any type information (i.e. the return type of the decorator is unknown), Pyright falls back on the assumption that the decorator is leaving the original function unmodified. So that case is handled already.

The problem comes from cases where the analyzer is able to infer some type information. The @wraps decorator is especially problematic because it provides a full type signature that returns a generic callable type _AnyCallable.

Since @wraps appears to be the source of many of these problems, maybe we should submit a change to typeshed to improve its signature so it uses a TypeVar. I don't know enough about the semantics of @wraps to say whether that's feasible. If it's mutating the original function (adding or removing parameters), then it's possible to describe that mutation using a TypeVar.

@aaronsmith1234
Copy link

Hi,
We are experiencing this same issue; old python language server seems to handle it fine, but Pylance seems to drop all the info. Can provide some sample code if necessary, but seems like examples above are probably sufficient.

@erictraut
Copy link
Contributor

I've improved the handling of unannotated decorators. The decorated function/method signature and docstring is now retained if the inferred return type of the decorator is a function that accepts only *args or **kwargs (which is the case in the examples above). This change will be in the next release of pylance.

@erictraut erictraut added the fixed in next version (main) A fix has been implemented and will appear in an upcoming version label Jan 8, 2021
@jakebailey
Copy link
Member

This issue has been fixed in version 2021.1.1, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202111-13-january-2021

@varunagrawal
Copy link

varunagrawal commented Feb 3, 2021

Note that the fix in version 2021.1.1 assumes that the decorator's returned function (aka the wrapper) only takes *args and **kwargs as arguments. I am doing something which requires injection of a custom argument, so it fails for me. It would be amazing if this worked for the general case. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed in next version (main) A fix has been implemented and will appear in an upcoming version needs documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

5 participants