77import keyword
88import re
99import xml .etree .ElementTree as etree # noqa
10- from typing import get_type_hints
10+ from typing import get_args , get_origin , get_type_hints
1111
1212import pytest
1313
@@ -199,7 +199,7 @@ def test_operations(spec, node):
199199 return
200200
201201 params_cls_name = compat_type_name (params_cls )
202- if params_cls_name == "Optional" :
202+ if params_cls_name in ( "Optional" , "Union" ) :
203203 # for GetAccountDetails, GetAccountFunds
204204 params_cls = py_type_unpack (params_cls )
205205 params_cls_name = compat_type_name (params_cls )
@@ -259,7 +259,7 @@ def is_param_optional(xml_param, api_cls) -> bool:
259259
260260
261261def py_type_unpack (type_def ):
262- return type_def . __args__ [0 ]
262+ return get_args ( type_def ) [0 ]
263263
264264
265265def py_type_unpack_annotated (type_def ):
@@ -274,7 +274,7 @@ def py_type_format(type_def) -> str:
274274 return py_type_replace (type_def .__metadata__ [- 1 ].title )
275275 if not hasattr (type_def , "__args__" ):
276276 return py_type_replace (type_def_name )
277- args = type_def . __args__ if type_def_name != "Optional" else type_def . __args__ [:- 1 ]
277+ args = get_args ( type_def ) if type_def_name != "Optional" else get_args ( type_def ) [:- 1 ]
278278 args_fmt = "," .join (py_type_format (arg ) for arg in args )
279279 return f"{ py_type_replace (type_def_name )} [{ args_fmt } ]"
280280
@@ -374,13 +374,13 @@ def compat_type_name(type_def) -> str:
374374 if type_def .__class__ .__name__ .startswith ("_Annotated" ):
375375 return "Annotated"
376376 name = type_def .__class__ .__name__ .lstrip ("_" ).replace ("Alias" , "" ).replace ("Generic" , "" ).replace ("Type" , "" )
377- if name == "Union" and type_def . __args__ [- 1 ] is type (None ): # noqa
377+ if name == "Union" and get_args ( type_def ) [- 1 ] is type (None ): # noqa
378378 # Optional looks just like Union, so we need to distinguish
379379 return "Optional"
380380 if not name :
381381 # Probably an enum
382382 try :
383- return type_def . __origin__ .__name__
383+ return get_origin ( type_def ) .__name__
384384 except AttributeError :
385385 pass
386386 return name
0 commit comments