44import sys
55import typing as T
66import uuid
7+ from typing import Optional
78
89import graphene
910import graphene .types
@@ -70,10 +71,26 @@ def test_builtin_scalars(input, expected):
7071 assert field .default_value == input [1 ]
7172
7273
73- def test_union ():
74+ def test_union_optional ():
7475 field = _convert_field_from_spec ("attr" , (T .Union [int , float , str ], 5.0 ))
75- assert issubclass (field .type . of_type , graphene .Union )
76+ assert issubclass (field .type , graphene .Union )
7677 assert field .default_value == 5.0
78+ assert field .type .__name__ .startswith ("UnionOf" )
79+
80+
81+ @pytest .mark .parametrize (
82+ "input" ,
83+ [
84+ (T .Union [int , float , str ], ...),
85+ (T .Union [int , float , str , None ], ...),
86+ (Optional [T .Union [int , float , str ]], ...),
87+ (Optional [T .Union [int , float , None ]], ...),
88+ ],
89+ )
90+ def test_union (input ):
91+ field = _convert_field_from_spec ("attr" , input )
92+ assert isinstance (field .type , graphene .NonNull )
93+ assert field .default_value == None
7794 assert field .type .of_type .__name__ .startswith ("UnionOf" )
7895
7996
@@ -95,6 +112,27 @@ def test_literal_singleton():
95112 assert field .default_value == "literal1"
96113 assert field .type .of_type == graphene .String
97114
115+ def test_union_pipe_optional ():
116+ field = _convert_field_from_spec ("attr" , (int | float | str , 5.0 ))
117+ assert issubclass (field .type , graphene .Union )
118+ assert field .default_value == 5.0
119+ assert field .type .__name__ .startswith ("UnionOf" )
120+
121+ @pytest .mark .parametrize (
122+ "input" ,
123+ [
124+ (int | float | str , ...),
125+ (int | float | str | None , ...),
126+ (Optional [int | float | str ], ...),
127+ (Optional [int | float | None ], ...),
128+ ],
129+ )
130+ def test_union_pipe (input ):
131+ field = _convert_field_from_spec ("attr" , input )
132+ assert isinstance (field .type , graphene .NonNull )
133+ assert field .default_value == None
134+ assert field .type .of_type .__name__ .startswith ("UnionOf" )
135+
98136
99137def test_mapping ():
100138 with pytest .raises (ConversionError ) as exc :
0 commit comments