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

Display metadata from typing.Annotated for function parameters #2306

Closed
SSmJaE opened this issue Jan 29, 2022 · 4 comments
Closed

Display metadata from typing.Annotated for function parameters #2306

SSmJaE opened this issue Jan 29, 2022 · 4 comments
Labels
waiting for user response Requires more information from user

Comments

@SSmJaE
Copy link

SSmJaE commented Jan 29, 2022

Currently, we can use a google or other style function docstring to document parameters that we can see when using the function,
but it has these disadvantages

  1. The documentation of the parameters is not together with the parameters, sometimes it is difficult to quickly find the documentation in the docstring of the function
  2. While you can use some extensions to automatically generate docstrings, you need to sync it every time you change the parameter definition

Since after PEP 593, python now supports typing.Annotated to provide metadata, why not display the metadata by the way, but only the type of the parameter?

suppose we have this function definition

def test_func(arg: Annotated[int, "some document for this arg", ValueRange(100, 200)]):
    pass

what i want is
from
image

to
206120c7fefbfd83446378b950ddb41

I know that when I hold down the Ctrl key I can find the full definition, but if we can see the metadata (including some "documentation") just hover over the parameter, It will be better in actual use.

Related discussion

@erictraut
Copy link
Contributor

Annotated isn't typically used for documentation. It's intended for user-specific (or tool-specific) metadata, and the interpretation is specific to the use case. General-purpose type checkers and language servers should therefore not interpret its meaning. Other than validating that it's syntactically correct, pylance/pyright don't pay any attention to Annotated type arguments beyond the first one, which correspond to the annotated type. As you pointed out, you can hold down the Ctrl key to see the full definition .

@SSmJaE
Copy link
Author

SSmJaE commented Jan 29, 2022

ok, thanks for your reply

@SSmJaE
Copy link
Author

SSmJaE commented Jan 29, 2022

By the way, what is the best practice for adding docstrings to parameters in vscode?

@erictraut
Copy link
Contributor

erictraut commented Jan 29, 2022

There isn't a single standard for function/method docstrings in Python, but there are several popular conventions. Pylance supports the following:

    1. Epytext:
         @param param1: description
    2. reST:
         :param param1: description
    3. Google (variant 1):
         Args:
             param1: description
    4. Google (variant 2):
         Args:
             param1 (type): description

If you use one of these forms, you'll see the documentation text for the current parameter as you type a call signature.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for user response Requires more information from user
Projects
None yet
Development

No branches or pull requests

3 participants