I’m using a NamedTuple
with comments as suggested for Python 3.6+:
class Config:
buy_fee: float
"""My field description..."""
# ...
Now I’d like to programmatically get the docstrings of the fields to generate a nice help message.
I tried to use inspect
as follows:
[(f, inspect.getdoc(getattr(cls, f))) for f in cls._fields]
But I only get some default comment values: [('buy_fee', 'Alias for field number 0'), ...]
.
I tried to convert the class to a @dataclass
and extract the docs with
[(f.name, inspect.getdoc(f)) for f in dataclasses.fields(cls)]
But this approach also failed, I got just [('buy_fee', None), ...]
.
Is there some way to achieve this?