Skip to content

Commit

Permalink
chore(internal): update test helper function (#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Apr 25, 2024
1 parent 8b18b52 commit f46e454
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,22 @@ def assert_matches_type(
assert_matches_type(key_type, key, path=[*path, "<dict key>"])
assert_matches_type(items_type, item, path=[*path, "<dict item>"])
elif is_union_type(type_):
for i, variant in enumerate(get_args(type_)):
variants = get_args(type_)

try:
none_index = variants.index(type(None))
except ValueError:
pass
else:
# special case Optional[T] for better error messages
if len(variants) == 2:
if value is None:
# valid
return

return assert_matches_type(type_=variants[not none_index], value=value, path=path)

for i, variant in enumerate(variants):
try:
assert_matches_type(variant, value, path=[*path, f"variant {i}"])
return
Expand Down

0 comments on commit f46e454

Please sign in to comment.