1
+ import sys
1
2
import random
2
3
from abc import abstractmethod
3
4
from typing import (
29
30
from docarray .proto import DocListProto , NodeProto
30
31
from docarray .typing .tensor .abstract_tensor import AbstractTensor
31
32
33
+ if sys .version_info >= (3 , 12 ):
34
+ from types import GenericAlias
35
+
32
36
T = TypeVar ('T' , bound = 'AnyDocArray' )
33
37
T_doc = TypeVar ('T_doc' , bound = BaseDocWithoutId )
34
38
IndexIterType = Union [slice , Iterable [int ], Iterable [bool ], None ]
@@ -51,8 +55,12 @@ def __repr__(self):
51
55
@classmethod
52
56
def __class_getitem__ (cls , item : Union [Type [BaseDocWithoutId ], TypeVar , str ]):
53
57
if not isinstance (item , type ):
54
- return Generic .__class_getitem__ .__func__ (cls , item ) # type: ignore
55
- # this do nothing that checking that item is valid type var or str
58
+ if sys .version_info < (3 , 12 ):
59
+ return Generic .__class_getitem__ .__func__ (cls , item ) # type: ignore
60
+ # this do nothing that checking that item is valid type var or str
61
+ # Keep the approach in #1147 to be compatible with lower versions of Python.
62
+ else :
63
+ return GenericAlias (cls , item ) # type: ignore
56
64
if not safe_issubclass (item , BaseDocWithoutId ):
57
65
raise ValueError (
58
66
f'{ cls .__name__ } [item] item should be a Document not a { item } '
0 commit comments