-
Notifications
You must be signed in to change notification settings - Fork 765
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
Decorated context manager tooltips don't display actual arguments #516
Comments
The problem is that you're annotating the undecorated function incorrectly. The undecorated function is not a context manager. It becomes a context manager only once it is decorated. There are a couple of workarounds:
|
You are technically correct (the best kind of correct!) that the annotation is incorrect. However neither workaround appears to work: I didn't note it in the original report, but I only even tried |
That made me chuckle. :) Great Futurama reference. The reason that the decorated function has this signature is due to the typeshed stub for contextlib.pyi. Here's how the decorator is declared: def contextmanager(func: Callable[..., Iterator[_T]]) -> Callable[..., _GeneratorContextManager[_T]]: ... Note that it returns a Callable that takes any parameters. (That's what the The _PS = ParamSpec("_PS")
def contextmanager(func: Callable[_PS, Iterator[_T]]) -> Callable[_PS, _GeneratorContextManager[_T]]: ... Pyright (which is the type checker used within Pylance) already has full support for PEP 612, but some of the other type checkers have not yet added this support. For that reason, the typeshed stubs have not yet incorporated If you would like, you can file a bug or submit a PR in the typeshed repo to encourage the maintainers to make these changes. Pylance/pyright regularly update the typeshed stubs, so we will automatically pick up any improvements. |
@erictraut thanks for the detailed explanation! I will note that PyCharm handles decorated context managers correctly (from a usability perspective), but I'm fairly certain that they use their own tooling which explains why the behavior is different. Please feel free to close this if you think it's not relevant given that the source of the problem is in another project. Thanks again for your help! |
The latest typeshed stubs for contextlib now make use of ParamSpec (PEP 612), which means that |
Awesome, thanks! |
This issue has been fixed in version 2021.6.2, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202162-16-june-2021 |
Environment data
Expected behaviour
Tooltips should show the actual arguments that a
contextmanager
-decorated function takes.Actual behaviour
Arguments are rendered as
*args: Any, **kwargs: Any
(see screenshot).Logs
Code Snippet / Additional information
The text was updated successfully, but these errors were encountered: