According to PEP 557 which defines the meaning of data classes,
The dataclass
decorator examines the class to find fields. A field is defined as any variable identified in __annotations__
. That is, a variable that has a type annotation.
Which is to say that the premise of this question (e.g. "How can I use dataclass
with a field that has no type annotation) must be rejected. The term 'field' in the context of dataclass
necessitates that the attribute has a type annotation by definition.
Note that using a generic type annotation like typing.Any
is not the same as having an unannotated attribute, since the attribute will appear in __annotations__
.
Finally, the helper function make_dataclass
will automatically use typing.Any
for the type annotation in cases when only an attribute name is supplied, and this is also mentioned in the PEP with an example.