Skip to content

Commit 78414bf

Browse files
committed
implement typing_extensions as optional dependency
works better with astropy#10689, but even without. Signed-off-by: nstarman <[email protected]>
1 parent 6fa06b6 commit 78414bf

3 files changed

Lines changed: 112 additions & 95 deletions

File tree

‎astropy/units/_typing.py‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""Typing compatibility for :mod:`~astropy.units`.
4+
5+
Static types are imported from
6+
1. ``typing`` (if present)
7+
2. ``typing_extensions`` (if present)
8+
3. Here if not present in the above.
9+
These are altered from https://github.com/python/typing
10+
11+
"""
12+
13+
__all__ = [
14+
"Annotated",
15+
]
16+
17+
import operator
18+
19+
try: # py 3.9+
20+
from typing import Annotated
21+
except ImportError: # optional dependency
22+
try:
23+
from typing_extensions import Annotated
24+
except ImportError:
25+
_HAS_ANNOTATED = False # need to do ourselves
26+
else:
27+
_HAS_ANNOTATED = True
28+
else:
29+
_HAS_ANNOTATED = True
30+
31+
32+
if not _HAS_ANNOTATED:
33+
34+
class _AnnotatedAlias:
35+
"""Runtime representation of an annotated type.
36+
At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't'
37+
with extra annotations. The alias behaves like a normal typing alias,
38+
instantiating is the same as instantiating the underlying type, binding
39+
it to types is also the same.
40+
"""
41+
42+
def __init__(self, origin, metadata):
43+
if isinstance(origin, _AnnotatedAlias):
44+
metadata = origin.__metadata__ + metadata
45+
origin = origin.__origin__
46+
self.__origin__ = origin
47+
self.__metadata__ = (
48+
metadata if isinstance(metadata, tuple) else (metadata,)
49+
)
50+
51+
# def copy_with(self, params):
52+
# assert len(params) == 1
53+
# new_type = params[0]
54+
# return _AnnotatedAlias(new_type, self.__metadata__)
55+
56+
def __repr__(self):
57+
return "Annotated[{}, {}]".format(
58+
self.__origin__, ", ".join(repr(a) for a in self.__metadata__)
59+
)
60+
61+
def __reduce__(self):
62+
return (
63+
operator.getitem,
64+
(Annotated, (self.__origin__,) + self.__metadata__),
65+
)
66+
67+
def __eq__(self, other):
68+
if not isinstance(other, _AnnotatedAlias):
69+
return NotImplemented
70+
if self.__origin__ != other.__origin__:
71+
return False
72+
return self.__metadata__ == other.__metadata__
73+
74+
def __hash__(self):
75+
return hash((self.__origin__, self.__metadata__))
76+
77+
class _Py36CompatMeta(type):
78+
def __getitem__(cls, params):
79+
if not isinstance(params, tuple) or len(params) < 2:
80+
raise TypeError(
81+
"Annotated[...] should be used "
82+
"with at least two arguments (a type and an "
83+
"annotation)."
84+
)
85+
origin = params[0]
86+
metadata = tuple(params[1:])
87+
return _AnnotatedAlias(origin, metadata)
88+
89+
class Annotated(metaclass=_Py36CompatMeta):
90+
"""Add context specific metadata to a type."""
91+
92+
__slots__ = ()
93+
94+
def __new__(cls, *args, **kwargs):
95+
raise TypeError("Type Annotated cannot be instantiated.")
96+
97+
def __class_getitem__(cls, params):
98+
if not isinstance(params, tuple) or len(params) < 2:
99+
raise TypeError(
100+
"Annotated[...] should be used "
101+
"with at least two arguments (a type and an "
102+
"annotation)."
103+
)
104+
origin = params[0]
105+
metadata = tuple(params[1:])
106+
return _AnnotatedAlias(origin, metadata)
107+
108+
def __init_subclass__(cls, *args, **kwargs):
109+
raise TypeError(
110+
"Cannot subclass {}.Annotated".format(cls.__module__)
111+
)

‎astropy/units/quantity.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ def __class_getitem__(cls, unit_annotation):
340340
"""
341341
if isinstance(unit_annotation, tuple): # unit and annotations
342342
unit = unit_annotation[0]
343-
if len(unit_annotation) > 1:
343+
if len(unit_annotation) > 1: # multiple metadata
344344
raise NotImplementedError("Multiple metadata not yet supported.")
345345
else: # just unit
346346
annotations = ()

‎astropy/units/typing.py‎

Lines changed: 0 additions & 94 deletions
This file was deleted.

0 commit comments

Comments
 (0)