I too have used plain strings in Annotated[...] to document fields in
dataclasses.

On Thu, 2021-11-18 at 13:51 -0500, Ricky Teachey wrote:
> On Thu, Nov 18, 2021 at 1:39 PM Thomas Grainger <tagr...@gmail.com>
> wrote:
> > Ricky Teachey wrote:
> > > 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__
> > > Out[9]:
> > > {'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.
> > > Rick.
> > > ---
> > > Ricky.
> > > "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 tmkehrenb...@gmail.com 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
> > > > that.
> > > > Thomas
> > > > _______________________________________________
> > > > Python-ideas mailing list -- python-ideas@python.org
> > > > To unsubscribe send an email to python-ideas-le...@python.org
> > > > https://mail.python.org/mailman3/lists/python-ideas.python.org/
> > > > Message archived at
> > >
> >
> > 
> https://mail.python.org/archives/list/python-ideas@python.org/message/F2QJ3Q.
> > ..
> > > > Code of Conduct: http://python.org/psf/codeofconduct/
> > > >
> > 
> > I think you need to use another type rather than a plain string
> > here eg:
> > 
> > 
> > ```
> > @dataclasses.dataclass(frozen=True)
> > class Doc:
> >     v: str
> > 
> > @dataclasses.dataclass
> > class A:
> >     """docstring for class A."""
> >     x: typing.Annotated[int, Doc("docstring for x")]
> > ```
> > 
> 
> 
> I don't know why you would? It seems to work just fine with a plain
> string?
> 
> One thing that is bad about it: the class member "docstrings"
> (actually the annotations) are repeated all over the place (look like
> 4 times total?) in the help() output-- example is below:
> 
> >>> help(A)
> Help on class A in module __main__:
> 
> class A(builtins.object)
>  |  A(x: typing.Annotated[int, 'docstring for x']) -> None
>  |
>  |  docstring for class A.
>  |
>  |  Methods defined here:
>  |
>  |  __eq__(self, other)
>  |
>  |  __init__(self, x: typing.Annotated[int, 'docstring for x']) ->
> None
>  |
>  |  __repr__(self)
>  |
>  |  -----------------------------------------------------------------
> -----
>  |  Data descriptors defined here:
>  |
>  |  __dict__
>  |      dictionary for instance variables (if defined)
>  |
>  |  __weakref__
>  |      list of weak references to the object (if defined)
>  |
>  |  -----------------------------------------------------------------
> -----
>  |  Data and other attributes defined here:
>  |
>  |  __annotations__ = {'x': typing.Annotated[int, 'docstring for x']}
>  |
>  |  __dataclass_fields__ = {'x':
> Field(name='x',type=typing.Annotated[int,...
>  |
>  |  __dataclass_params__ =
> _DataclassParams(init=True,repr=True,eq=True,or...
>  |
>  |  __hash__ = None
> 
> ---
> Ricky.
> 
> "I've never met a Kentucky man who wasn't either thinking about going
> home or actually going home." - Happy Chandler
> 
> 
> 
> _______________________________________________
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/BXCEE4G6SDYWI4LQMTPZ3AWAJN2I3EMD/
> Code of Conduct: http://python.org/psf/codeofconduct/

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/KG67DQKZYMDZNZTLSZIHSYG5IGBH2REG/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to