-
The situation is I have a vector class from mod import Foo
from typing import TYPE_CHECKING, TypeVar, TypeAlias
T = TypeVar("T")
if TYPE_CHECKING:
FooList : TypeAlias = Foo[T] | List[T]
"""Short doc of FooList"""
else:
FooList: TypeAlias = Foo | List[T] # at runtime cannot add a type to Foo
"""Short doc of FooList"""
x : FooList # <-- has no doc tooltip In non TypeAlias cases I can hack around it with a redefinition, or using a
However doing this will result in a How can I address this, and is this something that pylance could implement to integrate docstring from such always-true guarded if-clauses? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
What I came up with so far is to hack it with
|
Beta Was this translation helpful? Give feedback.
-
There is no formally accepted specification for attribute docstrings. The closest thing is PEP 258, which was submitted for consideration but ultimately rejected. This is the spec I used when implementing support for attribute docstrings in pyright. This PEP clearly states that string literals should be considered docstrings only if they're at the top level of a module or class definition, not within a conditional block. That makes sense if you think about it. There have been several attempts over the past few years to formally specify ways to add docstrings to attributes and type aliases, but these attempts have faltered. If you'd like to propose a standard in this area, the Python Ideas forum is probably a good place to propose your idea. If you can get community consensus around an idea — and especially if you can get it standardized, then pyright / pylance will likely implement your idea. I like the suggestion by @Daraan. That's the workaround I'd use in this case. |
Beta Was this translation helpful? Give feedback.
There is no formally accepted specification for attribute docstrings. The closest thing is PEP 258, which was submitted for consideration but ultimately rejected. This is the spec I used when implementing support for attribute docstrings in pyright. This PEP clearly states that string literals should be considered docstrings only if they're at the top level of a module or class definition, not within a conditional block. That makes sense if you think about it.
There have been several attempts over the past few years to formally specify ways to add docstrings to attributes and type aliases, but these attempts have faltered.
If you'd like to propose a standard in this area, the Python Ideas …