1616 _SubParsersAction ,
1717)
1818from collections import defaultdict
19- from collections .abc import Mapping , Sequence
19+ from collections .abc import Callable , Mapping , Sequence
2020from enum import Enum
2121from functools import cached_property
2222from textwrap import dedent
2525 TYPE_CHECKING ,
2626 Annotated ,
2727 Any ,
28- Callable ,
2928 Generic ,
3029 Literal ,
3130 NoReturn ,
32- Optional ,
3331 TypeVar ,
34- Union ,
3532 cast ,
33+ get_args ,
34+ get_origin ,
3635 overload ,
3736)
3837
4342from pydantic .dataclasses import is_pydantic_dataclass
4443from pydantic .fields import FieldInfo
4544from pydantic_core import PydanticUndefined
46- from typing_extensions import get_args , get_origin
4745from typing_inspection import typing_objects
4846from typing_inspection .introspection import is_union_origin
4947
@@ -95,21 +93,21 @@ class _CliArg(BaseModel):
9593 arg_prefix : str
9694 case_sensitive : bool
9795 hide_none_type : bool
98- kebab_case : Optional [ Union [ bool , Literal ['all' , 'no_enums' ]]]
99- enable_decoding : Optional [ bool ]
96+ kebab_case : bool | Literal ['all' , 'no_enums' ] | None
97+ enable_decoding : bool | None
10098 env_prefix_len : int
10199 args : list [str ] = []
102100 kwargs : dict [str , Any ] = {}
103101
104102 _alias_names : tuple [str , ...] = PrivateAttr (())
105- _alias_paths : dict [str , Optional [ int ] ] = PrivateAttr ({})
103+ _alias_paths : dict [str , int | None ] = PrivateAttr ({})
106104 _is_alias_path_only : bool = PrivateAttr (False )
107105 _field_info : FieldInfo = PrivateAttr ()
108106
109107 def __init__ (
110108 self ,
111109 field_info : FieldInfo ,
112- parser_map : defaultdict [str | FieldInfo , dict [Optional [ int ] | str , _CliArg ]],
110+ parser_map : defaultdict [str | FieldInfo , dict [int | None | str , _CliArg ]],
113111 ** values : Any ,
114112 ) -> None :
115113 super ().__init__ (** values )
@@ -132,12 +130,12 @@ def __init__(
132130 parser_map [self .field_info ][index ] = parser_map [alias_path_dest ][index ]
133131
134132 @classmethod
135- def get_kebab_case (cls , name : str , kebab_case : Optional [ Union [ bool , Literal ['all' , 'no_enums' ]]] ) -> str :
133+ def get_kebab_case (cls , name : str , kebab_case : bool | Literal ['all' , 'no_enums' ] | None ) -> str :
136134 return name .replace ('_' , '-' ) if kebab_case not in (None , False ) else name
137135
138136 @classmethod
139137 def get_enum_names (
140- cls , annotation : type [Any ], kebab_case : Optional [ Union [ bool , Literal ['all' , 'no_enums' ]]]
138+ cls , annotation : type [Any ], kebab_case : bool | Literal ['all' , 'no_enums' ] | None
141139 ) -> tuple [str , ...]:
142140 enum_names : tuple [str , ...] = ()
143141 annotation = _strip_annotated (annotation )
@@ -157,7 +155,7 @@ def field_info(self) -> FieldInfo:
157155 return self ._field_info
158156
159157 @cached_property
160- def subcommand_dest (self ) -> Optional [ str ] :
158+ def subcommand_dest (self ) -> str | None :
161159 return f'{ self .arg_prefix } :subcommand' if _CliSubCommand in self .field_info .metadata else None
162160
163161 @cached_property
@@ -206,7 +204,7 @@ def alias_names(self) -> tuple[str, ...]:
206204 return self ._alias_names
207205
208206 @cached_property
209- def alias_paths (self ) -> dict [str , Optional [ int ] ]:
207+ def alias_paths (self ) -> dict [str , int | None ]:
210208 return self ._alias_paths
211209
212210 @cached_property
@@ -236,7 +234,7 @@ def is_no_decode(self) -> bool:
236234
237235
238236T = TypeVar ('T' )
239- CliSubCommand = Annotated [Union [ T , None ] , _CliSubCommand ]
237+ CliSubCommand = Annotated [T | None , _CliSubCommand ]
240238CliPositionalArg = Annotated [T , _CliPositionalArg ]
241239_CliBoolFlag = TypeVar ('_CliBoolFlag' , bound = bool )
242240CliImplicitFlag = Annotated [_CliBoolFlag , _CliImplicitFlag ]
@@ -585,9 +583,7 @@ def _is_nested_alias_path_only_workaround(
585583 return True
586584 return False
587585
588- def _get_merge_parsed_list_types (
589- self , parsed_list : list [str ], field_name : str
590- ) -> tuple [Optional [type ], Optional [type ]]:
586+ def _get_merge_parsed_list_types (self , parsed_list : list [str ], field_name : str ) -> tuple [type | None , type | None ]:
591587 merge_type = self ._cli_dict_args .get (field_name , list )
592588 if (
593589 merge_type is list
@@ -606,7 +602,7 @@ def _get_merge_parsed_list_types(
606602
607603 def _merged_list_to_str (self , merged_list : list [str ], field_name : str ) -> str :
608604 decode_list : list [str ] = []
609- is_use_decode : Optional [ bool ] = None
605+ is_use_decode : bool | None = None
610606 cli_arg_map = self ._parser_map .get (field_name , {})
611607 for index , item in enumerate (merged_list ):
612608 cli_arg = cli_arg_map .get (index )
@@ -867,7 +863,7 @@ def _parse_known_args(*args: Any, **kwargs: Any) -> Namespace:
867863 self ._add_subparsers = self ._connect_parser_method (add_subparsers_method , 'add_subparsers_method' )
868864 self ._formatter_class = formatter_class
869865 self ._cli_dict_args : dict [str , type [Any ] | None ] = {}
870- self ._parser_map : defaultdict [str | FieldInfo , dict [Optional [ int ] | str , _CliArg ]] = defaultdict (dict )
866+ self ._parser_map : defaultdict [str | FieldInfo , dict [int | None | str , _CliArg ]] = defaultdict (dict )
871867 self ._add_parser_args (
872868 parser = self .root_parser ,
873869 model = self .settings_cls ,
@@ -892,7 +888,7 @@ def _add_parser_args(
892888 is_model_suppressed : bool = False ,
893889 ) -> ArgumentParser :
894890 subparsers : Any = None
895- alias_path_args : dict [str , Optional [ int ] ] = {}
891+ alias_path_args : dict [str , int | None ] = {}
896892 # Ignore model default if the default is a model and not a subclass of the current model.
897893 model_default = (
898894 None
@@ -1159,7 +1155,7 @@ def _add_parser_submodels(
11591155 def _add_parser_alias_paths (
11601156 self ,
11611157 parser : Any ,
1162- alias_path_args : dict [str , Optional [ int ] ],
1158+ alias_path_args : dict [str , int | None ],
11631159 added_args : list [str ],
11641160 arg_prefix : str ,
11651161 subcommand_prefix : str ,
0 commit comments