Skip to content

Commit 9391424

Browse files
committed
fix: error type hints in Python3.12 (#1147)
Signed-off-by: 954 <[email protected]>
1 parent a2421a6 commit 9391424

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

docarray/array/any_array.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
import random
23
from abc import abstractmethod
34
from typing import (
@@ -29,6 +30,9 @@
2930
from docarray.proto import DocListProto, NodeProto
3031
from docarray.typing.tensor.abstract_tensor import AbstractTensor
3132

33+
if sys.version_info < (3, 12):
34+
from types import GenericAlias
35+
3236
T = TypeVar('T', bound='AnyDocArray')
3337
T_doc = TypeVar('T_doc', bound=BaseDocWithoutId)
3438
IndexIterType = Union[slice, Iterable[int], Iterable[bool], None]
@@ -51,8 +55,12 @@ def __repr__(self):
5155
@classmethod
5256
def __class_getitem__(cls, item: Union[Type[BaseDocWithoutId], TypeVar, str]):
5357
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
5664
if not safe_issubclass(item, BaseDocWithoutId):
5765
raise ValueError(
5866
f'{cls.__name__}[item] item should be a Document not a {item} '

0 commit comments

Comments
 (0)