Correct dict['x', 'y'] into dict[ForwardRef('x'), ForwardRef('y')] and similar for list, set
(
type_: _AnnotationScanType,
)
| 186 | |
| 187 | |
| 188 | def fixup_container_fwd_refs( |
| 189 | type_: _AnnotationScanType, |
| 190 | ) -> _AnnotationScanType: |
| 191 | """Correct dict['x', 'y'] into dict[ForwardRef('x'), ForwardRef('y')] |
| 192 | and similar for list, set |
| 193 | |
| 194 | """ |
| 195 | |
| 196 | if ( |
| 197 | is_generic(type_) |
| 198 | and get_origin(type_) |
| 199 | in ( |
| 200 | dict, |
| 201 | set, |
| 202 | list, |
| 203 | collections_abc.MutableSet, |
| 204 | collections_abc.MutableMapping, |
| 205 | collections_abc.MutableSequence, |
| 206 | collections_abc.Mapping, |
| 207 | collections_abc.Sequence, |
| 208 | ) |
| 209 | # fight, kick and scream to struggle to tell the difference between |
| 210 | # dict[] and typing.Dict[] which DO NOT compare the same and DO NOT |
| 211 | # behave the same yet there is NO WAY to distinguish between which type |
| 212 | # it is using public attributes |
| 213 | and not re.match( |
| 214 | "typing.(?:Dict|List|Set|.*Mapping|.*Sequence|.*Set)", repr(type_) |
| 215 | ) |
| 216 | ): |
| 217 | # compat with py3.10 and earlier |
| 218 | return get_origin(type_).__class_getitem__( # type: ignore |
| 219 | tuple( |
| 220 | [ |
| 221 | ForwardRef(elem) if isinstance(elem, str) else elem |
| 222 | for elem in get_args(type_) |
| 223 | ] |
| 224 | ) |
| 225 | ) |
| 226 | return type_ |
| 227 | |
| 228 | |
| 229 | def _copy_generic_annotation_with( |
no test coverage detected