Skip to content

Commit 856f12e

Browse files
committed
Don't type check unions recursivly
1 parent 10e639d commit 856f12e

2 files changed

Lines changed: 22 additions & 21 deletions

File tree

src/specklepy/objects/base.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
Tuple,
1414
Type,
1515
Union,
16+
get_origin,
1617
get_type_hints,
1718
)
1819
from warnings import warn
1920

2021
from pydantic.alias_generators import to_pascal
22+
from typing_extensions import get_args
2123

2224
from specklepy.logging.exceptions import SpeckleException
2325
from specklepy.transports.memory import MemoryTransport
@@ -224,28 +226,27 @@ def _validate_type(t: Optional[type], value: Any) -> Tuple[bool, Any]:
224226
if isinstance(t, ForwardRef):
225227
return True, value
226228

227-
origin = t.__origin__
229+
origin = get_origin(t)
230+
args = get_args(t)
231+
228232
# below is what in nicer for >= py38
229233
# origin = get_origin(t)
230234

231235
# recursive validation for Unions on both types preferring the fist type
232-
if origin is Union:
233-
# below is what in nicer for >= py38
234-
# t_1, t_2 = get_args(t)
235-
args = t.__args__ # type: ignore
236-
for arg_t in args:
237-
t_success, t_value = _validate_type(arg_t, value)
238-
if t_success:
239-
return True, t_value
240-
return False, value
236+
# if origin is Union or isinstance(t, UnionType):
237+
# for arg_t in args:
238+
# ok, v = _validate_type(arg_t, value)
239+
# if ok:
240+
# return True, v
241+
# return False, value
241242
if origin is dict:
242243
if not isinstance(value, dict):
243244
return False, value
244-
if value == {}:
245+
if not value:
245246
return True, value
246-
if not getattr(t, "__args__", None):
247+
if not args:
247248
return True, value
248-
t_key, t_value = t.__args__ # type: ignore
249+
t_key, t_value = args
249250

250251
if (
251252
getattr(t_key, "__name__", None),
@@ -265,11 +266,11 @@ def _validate_type(t: Optional[type], value: Any) -> Tuple[bool, Any]:
265266
if origin is list:
266267
if not isinstance(value, list):
267268
return False, value
268-
if value == []:
269+
if not value:
269270
return True, value
270-
if not hasattr(t, "__args__"):
271+
if not args:
271272
return True, value
272-
t_items = t.__args__[0] # type: ignore
273+
t_items = args[0]
273274
if getattr(t_items, "__name__", None) == "T":
274275
return True, value
275276
first_item_valid, _ = _validate_type(t_items, value[0])
@@ -280,10 +281,10 @@ def _validate_type(t: Optional[type], value: Any) -> Tuple[bool, Any]:
280281
if origin is tuple:
281282
if not isinstance(value, tuple):
282283
return False, value
283-
if not hasattr(t, "__args__"):
284+
if not args:
284285
return True, value
285286
args = t.__args__ # type: ignore
286-
if args == tuple():
287+
if not args:
287288
return True, value
288289
# we're not checking for empty tuple, cause tuple lengths must match
289290
if len(args) != len(value):
@@ -299,7 +300,7 @@ def _validate_type(t: Optional[type], value: Any) -> Tuple[bool, Any]:
299300
if origin is set:
300301
if not isinstance(value, set):
301302
return False, value
302-
if not hasattr(t, "__args__"):
303+
if not args:
303304
return True, value
304305
t_items = t.__args__[0] # type: ignore
305306
first_item_valid, _ = _validate_type(t_items, next(iter(value)))

tests/objects/test_text.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def sample_text_all_properties(sample_point: Point, sample_plane: Plane) -> Text
3535
alignmentH=AlignmentHorizontal.Center,
3636
alignmentV=AlignmentVertical.Center,
3737
plane=sample_plane,
38-
maxWidth=20,
38+
maxWidth=20.0,
3939
units=Units.m,
4040
)
4141

@@ -56,7 +56,7 @@ def test_text_creation_minimal(sample_point: Point):
5656

5757
def test_text_creation_extended(sample_point: Point, sample_plane: Plane):
5858
text_value = "text"
59-
max_width = 20
59+
max_width = 20.0
6060

6161
text_obj = Text(
6262
value=text_value,

0 commit comments

Comments
 (0)