We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent c7a2373 commit 2106bc6Copy full SHA for 2106bc6
1 file changed
tests/utils.py
@@ -97,7 +97,22 @@ def assert_matches_type(
97
assert_matches_type(key_type, key, path=[*path, "<dict key>"])
98
assert_matches_type(items_type, item, path=[*path, "<dict item>"])
99
elif is_union_type(type_):
100
- for i, variant in enumerate(get_args(type_)):
+ variants = get_args(type_)
101
+
102
+ try:
103
+ none_index = variants.index(type(None))
104
+ except ValueError:
105
+ pass
106
+ else:
107
+ # special case Optional[T] for better error messages
108
+ if len(variants) == 2:
109
+ if value is None:
110
+ # valid
111
+ return
112
113
+ return assert_matches_type(type_=variants[not none_index], value=value, path=path)
114
115
+ for i, variant in enumerate(variants):
116
try:
117
assert_matches_type(variant, value, path=[*path, f"variant {i}"])
118
return
0 commit comments