Skip to content

Commit 6431a18

Browse files
committed
Fix union type check for python 3.10
Signed-off-by: Jort Bergfeld <[email protected]>
1 parent 95766fa commit 6431a18

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/datumaro/experimental/dataset.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,20 @@ def _validate_attribute_type(self, expected_type: Any, value: Any) -> bool:
7878
# Union and Callable types have to be handled separately,
7979
# because isinstance() does not work with Callable types.
8080
origin = get_origin(expected_type)
81-
if origin is Union:
81+
if origin in {Union, types.UnionType}:
8282
# Check each type in the Union
83-
return any(self._validate_attribute_type(typ, value) for typ in get_args(expected_type))
84-
if origin in {typing.Callable, collections.abc.Callable} or expected_type in {
83+
result = any(self._validate_attribute_type(typ, value) for typ in get_args(expected_type))
84+
elif origin in {typing.Callable, collections.abc.Callable} or expected_type in {
8585
typing.Callable,
8686
collections.abc.Callable,
8787
}:
88-
return callable(value)
89-
return isinstance(value, origin or expected_type)
88+
result = callable(value)
89+
else:
90+
try:
91+
result = isinstance(value, expected_type)
92+
except TypeError:
93+
result = isinstance(value, origin)
94+
return result
9095

9196
@classmethod
9297
@cache

0 commit comments

Comments
 (0)