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
It should be possible to attach documentation to a type alias by adding a docstring on the next line. In practice, this doesn't always work. I am unable to attach documentation to the following types of type aliases:
Direct aliases without further use of further type hint constructs, such as str or a user-defined class, provided those types have a docstring of their own.
When using a PEP 593 Annotation, where the annotated type is something in the first category, the docstring is ignored and the docstring of the wrapped (primitive) type is used instead.
Environment data
Language Server version: v2022.11.10
OS and version: macOS Monterey (12.5.1)
Python version (& distribution if applicable, e.g. Anaconda): 3.10.8
Code Snippet
fromtypingimportAnnotated, TypeAlias# setup assignmentsarbitrary_annotation=42classUserDefinedClassWithDocstring:
"""An arbitrary class"""classUserDefinedClassWithoutDocstring:
pass# cases where docstrings are not honouredStrAlias: TypeAlias=str"""A regular type alias for str"""ClassAlias: TypeAlias=UserDefinedClassWithDocstring"""A class type alias"""AnnotatedStr: TypeAlias=Annotated[str, arbitrary_annotation]
"""An annotated type alias referencing str"""# cases where docstrings are honouredUnionAlias: TypeAlias=str|int"""A union type alias for str or int"""AnnotatedAlias: TypeAlias=Annotated[UnionAlias, arbitrary_annotation]
"""An annotated type alias referencing a union alias"""UndocumentedClassAlias: TypeAlias=UserDefinedClassWithoutDocstring"""A class type alias where the original has no docstring"""AnnotatedUndocumentedClassAlias: TypeAlias=Annotated[
UserDefinedClassWithoutDocstring, arbitrary_annotation
]
"""An annotated type alias referencing a class with no docstring"""
Repro Steps
Create a Python file in Visual Studio Code with the above contents
Trigger the hover on each TypeAlias assignment (mousing over, or using ⌘K ⌘I / Ctrl-K Ctrl-I (command Kilo India)) to inspect the docstring
Expected behavior
The docstring to be attached to more cases, if not all cases shown. I can understand there must be trade-offs between showing the aliased type docstring vs. the docstring on the alias, but especially for annotated types, there are many use-cases for annotations that constrain the type further, making it very important that documentation is specific.
then I really appreciate it when the hover documentation tells me this, instead of the str docstring.
If this isn't a bug and the choice to ignore a docstring for a type alias is explicit, please document when docstrings do work and when not.
Actual behavior
For all type aliases in the cases where docstrings are not honoured group, the docstring shown is that of the aliased type, here either str or UserDefinedClassWithDocstring.
The type aliases in the second group, cases where docstrings are honoured, the attached docstrings are shown.
The text was updated successfully, but these errors were encountered:
It should be possible to attach documentation to a type alias by adding a docstring on the next line. In practice, this doesn't always work. I am unable to attach documentation to the following types of type aliases:
str
or a user-defined class, provided those types have a docstring of their own.Environment data
Code Snippet
Repro Steps
TypeAlias
assignment (mousing over, or using⌘K ⌘I
/Ctrl-K Ctrl-I
(command Kilo India)) to inspect the docstringExpected behavior
The docstring to be attached to more cases, if not all cases shown. I can understand there must be trade-offs between showing the aliased type docstring vs. the docstring on the alias, but especially for annotated types, there are many use-cases for annotations that constrain the type further, making it very important that documentation is specific.
E.g. take annotating a string as being hexadecimal, using Predicate()
type from the
annotated-types` project:then I really appreciate it when the hover documentation tells me this, instead of the
str
docstring.If this isn't a bug and the choice to ignore a docstring for a type alias is explicit, please document when docstrings do work and when not.
Actual behavior
For all type aliases in the cases where docstrings are not honoured group, the docstring shown is that of the aliased type, here either
str
orUserDefinedClassWithDocstring
.The type aliases in the second group, cases where docstrings are honoured, the attached docstrings are shown.
The text was updated successfully, but these errors were encountered: