Closed
Description
Feature or enhancement
Proposal:
A really nice __slots__
feature is the ability to provide a data dictionary in the form of docstrings. That is also available for property
objects. Consider this example:
class Rocket:
__slots__ = {
'velocity': 'Speed relative to Earth in meters per second',
'mass': 'Gross weight in kilograms',
}
@property
def kinetic_energy(self):
'Kinetic energy in Newtons'
return self.mass * self.velocity ** 2
Running help(Rocket)
shows all the field docstrings which is super helpful:
class Rocket(builtins.object)
| Readonly properties defined here:
|
| kinetic_energy
| Kinetic energy in Newtons
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| mass
| Gross weight in kilograms
|
| velocity
| Speed relative to Earth in meters per second
It would be nice is the same can be done with dataclasses that define __slots__
:
@dataclass(slots=True)
class Rocket:
velocity: float = field(doc='Speed relative to Earth in meters per second')
weight: float = field(doc='Gross weight in kilograms')
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Activity
sobolevn commentedon Jan 10, 2024
The change is quite simple, it results in:
The change:
The only concern I have is that we change how
__slots__
is defined: instead oftuple
it would be adict
. I think that it should be fine, since it is a part of the language. But, some people might expect it to be only atuple
.If others are fine with the change, I will add tests with docs and send a PR.
ericvsmith commentedon Jan 13, 2024
I think this change is okay. I don't think it would be reasonable for anyone to assume that dataclasses'
__slots__
would always be a tuple.That said, the documentation at https://docs.python.org/3/reference/datamodel.html says of
__slots__
: "This class variable can be assigned a string, iterable, or sequence of strings with variable names used by instances.". Perhaps that needs to be updated for the dict case?rhettinger commentedon Jan 13, 2024
It would be nice to have the dict case specifically called out in the data model docs. Right now, all we have is the announcement in the 3.8 whatsnew.
ericvsmith commentedon Jan 13, 2024
Created #114044 for the documentation issue.
ericvsmith commentedon Jan 13, 2024
One question I have about this feature: should it be an error to specify
doc
if not using__slots__
? It will be available in theField
object, so I guess that's of some utility. I guess if we got super excited about it, we could teachinspect
aboutField.doc
if the class is a dataclass and__slots__
isn't a dict. Or maybe even if it is a dict, then we could skip changing__slots__
to a dict for dataclasses.sobolevn commentedon Jan 13, 2024
I think it should be ok, some other tool might also use this
doc=
field.As a separate feature, I guess :)
Right now I don't understand the whole idea.
rhettinger commentedon Jan 13, 2024
That makes sense to me. In this regard, it would be a bit like the metadata field but for a dedicated purpose.
ericvsmith commentedon Jan 13, 2024
I'm not sure this answers your question, @sobolevn, but:
I mean that
inspect.getdoc()
could contain logic that says "if this is a dataclass, then show the documentation of the fields, using the Field.doc values, if they exist". It doesn't really fit into the "Data descriptors defined here" section, but we should be able to come up with something.I'll grant you that this is an unrelated issue.
pythongh-113878: Add `doc` parameter to `dataclasses.field`
doc
parameter todataclasses.field
#114051ericvsmith commentedon Jan 14, 2024
My typical questions when adding a feature to dataclasses are:
attrs
do anything similar? If so, we probably want to do it in a similar way.typing.dataclass_transform
(PEP 681)?11 remaining items