Runtime representation of an annotated type. At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't' with extra annotations. The alias behaves like a normal typing alias, instantiating is the same as instantiating the underlying type, binding it to t
| 892 | # 3.7-3.8 |
| 893 | else: |
| 894 | class _AnnotatedAlias(typing._GenericAlias, _root=True): |
| 895 | """Runtime representation of an annotated type. |
| 896 | |
| 897 | At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't' |
| 898 | with extra annotations. The alias behaves like a normal typing alias, |
| 899 | instantiating is the same as instantiating the underlying type, binding |
| 900 | it to types is also the same. |
| 901 | """ |
| 902 | def __init__(self, origin, metadata): |
| 903 | if isinstance(origin, _AnnotatedAlias): |
| 904 | metadata = origin.__metadata__ + metadata |
| 905 | origin = origin.__origin__ |
| 906 | super().__init__(origin, origin) |
| 907 | self.__metadata__ = metadata |
| 908 | |
| 909 | def copy_with(self, params): |
| 910 | assert len(params) == 1 |
| 911 | new_type = params[0] |
| 912 | return _AnnotatedAlias(new_type, self.__metadata__) |
| 913 | |
| 914 | def __repr__(self): |
| 915 | return (f"typing_extensions.Annotated[{typing._type_repr(self.__origin__)}, " |
| 916 | f"{', '.join(repr(a) for a in self.__metadata__)}]") |
| 917 | |
| 918 | def __reduce__(self): |
| 919 | return operator.getitem, ( |
| 920 | Annotated, (self.__origin__,) + self.__metadata__ |
| 921 | ) |
| 922 | |
| 923 | def __eq__(self, other): |
| 924 | if not isinstance(other, _AnnotatedAlias): |
| 925 | return NotImplemented |
| 926 | if self.__origin__ != other.__origin__: |
| 927 | return False |
| 928 | return self.__metadata__ == other.__metadata__ |
| 929 | |
| 930 | def __hash__(self): |
| 931 | return hash((self.__origin__, self.__metadata__)) |
| 932 | |
| 933 | class Annotated: |
| 934 | """Add context specific metadata to a type. |
no outgoing calls
no test coverage detected