Could this be a use case for typing.Annotated?

In [6]: from dataclasses import dataclass

In [7]: from typing import Annotated

In [8]: class A:
   ...:     """Docstring for class A."""
   ...:     x: Annotated[int, "Docstring for x"]
   ...:     y: Annotated[bool, "Docstring for y"] = True

In [9]: A.__annotations__
{'x': typing.Annotated[int, 'Docstring for x'],
 'y': typing.Annotated[bool, 'Docstring for y']}

The syntax is a bit arduous; I'd be in favor of thinking through ways to make it easier to write. But the basic functionality already exists; there's no reason to duplicate it with another language feature.



"I've never met a Kentucky man who wasn't either thinking about going home or actually going home." - Happy Chandler

On Thu, Nov 18, 2021 at 5:50 AM <> wrote:
Stephen J. Turnbull wrote:
> @standard_class_docstring_parser
>     class A:
>         """
>         Class docstring.
>         x:  Docstring for x
>         y:  Docstring for y
>         """
>         x: int
>         y: bool = True

Oh, this is actually a nice idea. You could have a
decorator that parses reST and then according
to some conventions extracts the attribute
docstrings. I think I will try to write something like

Python-ideas mailing list --
To unsubscribe send an email to
Message archived at
Code of Conduct: