Description
dataclass
needs to examine the annotations of class objects it decorates. Due to __annotations__
being badly designed, it currently pulls the __annotations__
out of the class dict (cls.__dict__.get('__annotations__')
). While this works today, this wouldn't work properly for classes defiend in modules using from __future__ import co_annotations
if PEP 649 is accepted. It's also not recommended best practices for working with annotations. (Which, admittedly, I wrote.)
Best practices call for using inspect.get_annotations
to get the annotations on any object. This does exactly what dataclass
wants; it doesn't inherit base class annotations if no child class defines annotations, and it always returns a dict. (Empty, if appropriate.)
dataclass
has in the past resisted using inspect.get_annotations
because it didn't want to incur the runtime cost of importing inspect
. However, as of this time in CPython trunk, inspect
is always imported during startup, and dataclass
is already importing it anyway. It's time dataclass
changed to best practices here.