@@ -509,7 +509,7 @@ def add_fns_to_class(self, cls):
509509 for name , fn in zip (self .names , fns ):
510510 fn .__qualname__ = f"{ cls .__qualname__ } .{ fn .__name__ } "
511511 if annotations := self .method_annotations .get (name ):
512- fn .__annotate__ = _make_annotate_function (annotations )
512+ fn .__annotate__ = _make_annotate_function (cls , annotations )
513513
514514 if self .unconditional_adds .get (name , False ):
515515 setattr (cls , name , fn )
@@ -526,10 +526,13 @@ def add_fns_to_class(self, cls):
526526 raise TypeError (error_msg )
527527
528528
529- def _make_annotate_function (annotations ):
529+ def _make_annotate_function (cls , annotations ):
530530 # Create an __annotate__ function for a dataclass
531531 # Try to return annotations in the same format as they would be
532532 # from a regular __init__ function
533+
534+ # annotations should be in FORWARDREF format at this stage
535+
533536 def __annotate__ (format ):
534537 match format :
535538 case annotationlib .Format .VALUE | annotationlib .Format .FORWARDREF :
@@ -538,19 +541,30 @@ def __annotate__(format):
538541 if isinstance (v , annotationlib .ForwardRef ) else v
539542 for k , v in annotations .items ()
540543 }
544+
541545 case annotationlib .Format .STRING :
546+ cls_annotations = {}
547+ for base in reversed (cls .__mro__ ):
548+ cls_annotations .update (
549+ annotationlib .get_annotations (base , format = format )
550+ )
551+
542552 string_annos = {}
543553 for k , v in annotations .items ():
544- if isinstance (v , str ):
545- string_annos [k ] = v
546- elif isinstance (v , annotationlib .ForwardRef ):
547- string_annos [k ] = v .evaluate (format = annotationlib .Format .STRING )
548- else :
554+ try :
555+ string_annos [k ] = cls_annotations [k ]
556+ except KeyError :
557+ # This should be the return value
549558 string_annos [k ] = annotationlib .type_repr (v )
550559 return string_annos
560+
551561 case _:
552562 raise NotImplementedError (format )
553563
564+ # This is a flag for _add_slots to know it needs to regenerate this method
565+ # In order to remove references to the original class when it is replaced
566+ __annotate__ .__generated_by_dataclasses = True
567+
554568 return __annotate__
555569
556570
0 commit comments