MCPcopy Index your code
hub / github.com/python/cpython / _make_annotate_function

Function _make_annotate_function

Lib/dataclasses.py:536–576  ·  view source on GitHub ↗
(__class__, method_name, annotation_fields, return_type)

Source from the content-addressed store, hash-verified

534
535
536def _make_annotate_function(__class__, method_name, annotation_fields, return_type):
537 # Create an __annotate__ function for a dataclass
538 # Try to return annotations in the same format as they would be
539 # from a regular __init__ function
540
541 def __annotate__(format, /):
542 Format = annotationlib.Format
543 match format:
544 case Format.VALUE | Format.FORWARDREF | Format.STRING:
545 cls_annotations = {}
546 for base in reversed(__class__.__mro__):
547 cls_annotations.update(
548 annotationlib.get_annotations(base, format=format)
549 )
550
551 new_annotations = {}
552 for k in annotation_fields:
553 # gh-142214: The annotation may be missing in unusual dynamic cases.
554 # If so, just skip it.
555 try:
556 new_annotations[k] = cls_annotations[k]
557 except KeyError:
558 pass
559
560 if return_type is not MISSING:
561 if format == Format.STRING:
562 new_annotations["return"] = annotationlib.type_repr(return_type)
563 else:
564 new_annotations["return"] = return_type
565
566 return new_annotations
567
568 case _:
569 raise NotImplementedError(format)
570
571 # This is a flag for _add_slots to know it needs to regenerate this method
572 # In order to remove references to the original class when it is replaced
573 __annotate__.__generated_by_dataclasses__ = True
574 __annotate__.__qualname__ = f"{__class__.__qualname__}.{method_name}.__annotate__"
575
576 return __annotate__
577
578
579def _field_assign(frozen, name, value, self_name):

Callers 1

add_fns_to_classMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…