225225"""
226226
227227import argparse
228+ from argparse import BooleanOptionalAction
228229from typing import (
229230 TypeVar ,
230231 Optional ,
231232 Sequence ,
232233 Type ,
233234 Tuple ,
234- List ,
235235 get_origin ,
236236 Literal ,
237237 get_args ,
238238 Union ,
239- Dict ,
240239 Any ,
241240 Generic ,
242241)
249248)
250249from importlib .metadata import version
251250
252- if hasattr (argparse , "BooleanOptionalAction" ):
253- # BooleanOptionalAction was added in Python 3.9
254- BooleanOptionalAction = argparse .BooleanOptionalAction
255- else :
256- # backport of argparse.BooleanOptionalAction.
257- class BooleanOptionalAction (argparse .Action ):
258- def __init__ (
259- self ,
260- option_strings ,
261- dest ,
262- default = None ,
263- type = None ,
264- choices = None ,
265- required = False ,
266- help = None ,
267- metavar = None ,
268- ):
269- _option_strings = []
270- for option_string in option_strings :
271- _option_strings .append (option_string )
272-
273- if option_string .startswith ("--" ):
274- option_string = "--no-" + option_string [2 :]
275- _option_strings .append (option_string )
276-
277- if help is not None and default is not None :
278- help += f" (default: { default } )"
279-
280- super ().__init__ (
281- option_strings = _option_strings ,
282- dest = dest ,
283- nargs = 0 ,
284- default = default ,
285- type = type ,
286- choices = choices ,
287- required = required ,
288- help = help ,
289- metavar = metavar ,
290- )
291-
292- def __call__ (self , parser , namespace , values , option_string = None ):
293- if option_string in self .option_strings :
294- setattr (namespace , self .dest , not option_string .startswith ("--no-" ))
295-
296- def format_usage (self ):
297- return " | " .join (self .option_strings )
298-
299-
300251# In Python 3.10, we can use types.NoneType
301252NoneType = type (None )
302253
@@ -316,7 +267,7 @@ def parse_args(options_class: Type[OptionsType], args: ArgsType = None) -> Optio
316267
317268def parse_known_args (
318269 options_class : Type [OptionsType ], args : ArgsType = None
319- ) -> Tuple [OptionsType , List [str ]]:
270+ ) -> Tuple [OptionsType , list [str ]]:
320271 """Parse known arguments and return tuple containing dataclass type
321272 and list of remaining arguments.
322273 """
@@ -424,7 +375,7 @@ def _add_dataclass_options(
424375 parser .add_argument (* args , ** kwargs )
425376
426377
427- def _get_kwargs (namespace : argparse .Namespace ) -> Dict [str , Any ]:
378+ def _get_kwargs (namespace : argparse .Namespace ) -> dict [str , Any ]:
428379 """Converts a Namespace to a dictionary containing the items that
429380 to be used as keyword arguments to the Options class.
430381 """
@@ -511,7 +462,7 @@ def parse_args(self, args: ArgsType = None, namespace=None) -> OptionsType:
511462
512463 def parse_known_args (
513464 self , args : ArgsType = None , namespace = None
514- ) -> Tuple [OptionsType , List [str ]]:
465+ ) -> Tuple [OptionsType , list [str ]]:
515466 """Parse known arguments and return tuple containing dataclass type
516467 and list of remaining arguments.
517468 """
0 commit comments