1313
1414import click
1515
16+ from ._compat_utils import UNION_TYPES , get_args , get_origin
1617from .completion import get_completion_inspect_parameters
1718from .core import MarkupMode , TyperArgument , TyperCommand , TyperGroup , TyperOption
1819from .models import (
@@ -810,30 +811,31 @@ def get_click_param(
810811 is_tuple = False
811812 parameter_type : Any = None
812813 is_flag = None
813- origin = getattr (main_type , "__origin__" , None )
814+ origin = get_origin (main_type )
815+
814816 if origin is not None :
815- # Handle Optional[SomeType]
816- if origin is Union :
817+ # Handle SomeType | None and Optional[SomeType]
818+ if origin in UNION_TYPES :
817819 types = []
818- for type_ in main_type . __args__ :
820+ for type_ in get_args ( main_type ) :
819821 if type_ is NoneType :
820822 continue
821823 types .append (type_ )
822824 assert len (types ) == 1 , "Typer Currently doesn't support Union types"
823825 main_type = types [0 ]
824- origin = getattr (main_type , "__origin__" , None )
826+ origin = get_origin (main_type )
825827 # Handle Tuples and Lists
826828 if lenient_issubclass (origin , List ):
827- main_type = main_type . __args__ [0 ]
828- assert not getattr (
829- main_type , "__origin__" , None
829+ main_type = get_args ( main_type ) [0 ]
830+ assert not get_origin (
831+ main_type
830832 ), "List types with complex sub-types are not currently supported"
831833 is_list = True
832834 elif lenient_issubclass (origin , Tuple ): # type: ignore
833835 types = []
834- for type_ in main_type . __args__ :
835- assert not getattr (
836- type_ , "__origin__" , None
836+ for type_ in get_args ( main_type ) :
837+ assert not get_origin (
838+ type_
837839 ), "Tuple types with complex sub-types are not currently supported"
838840 types .append (
839841 get_click_type (annotation = type_ , parameter_info = parameter_info )
@@ -848,7 +850,7 @@ def get_click_param(
848850 if is_list :
849851 convertor = generate_list_convertor (convertor )
850852 if is_tuple :
851- convertor = generate_tuple_convertor (main_type . __args__ )
853+ convertor = generate_tuple_convertor (get_args ( main_type ) )
852854 if isinstance (parameter_info , OptionInfo ):
853855 if main_type is bool and not (parameter_info .is_flag is False ):
854856 is_flag = True
@@ -1002,7 +1004,7 @@ def get_param_completion(
10021004 incomplete_name = None
10031005 unassigned_params = [param for param in parameters .values ()]
10041006 for param_sig in unassigned_params [:]:
1005- origin = getattr (param_sig .annotation , "__origin__" , None )
1007+ origin = get_origin (param_sig .annotation )
10061008 if lenient_issubclass (param_sig .annotation , click .Context ):
10071009 ctx_name = param_sig .name
10081010 unassigned_params .remove (param_sig )
0 commit comments