Return the annotations for an object, checking that it is a dictionary. Does not return a fresh dictionary.
(obj)
| 1129 | |
| 1130 | |
| 1131 | def _get_dunder_annotations(obj): |
| 1132 | """Return the annotations for an object, checking that it is a dictionary. |
| 1133 | |
| 1134 | Does not return a fresh dictionary. |
| 1135 | """ |
| 1136 | # This special case is needed to support types defined under |
| 1137 | # from __future__ import annotations, where accessing the __annotations__ |
| 1138 | # attribute directly might return annotations for the wrong class. |
| 1139 | if isinstance(obj, type): |
| 1140 | try: |
| 1141 | ann = _BASE_GET_ANNOTATIONS(obj) |
| 1142 | except AttributeError: |
| 1143 | # For static types, the descriptor raises AttributeError. |
| 1144 | return None |
| 1145 | else: |
| 1146 | ann = getattr(obj, "__annotations__", None) |
| 1147 | if ann is None: |
| 1148 | return None |
| 1149 | |
| 1150 | if not isinstance(ann, dict): |
| 1151 | raise ValueError(f"{obj!r}.__annotations__ is neither a dict nor None") |
| 1152 | return ann |
no test coverage detected