@@ -71,9 +71,11 @@ def assure_args(args: Optional[Sequence[str]] = None):
7171 args = []
7272 return args
7373
74+
7475def _subcommands_default_appliable (kwargs , _crawling ):
7576 if len (_crawling .get ()):
76- return kwargs .get ("subcommands_default" )
77+ return kwargs .get ("subcommands_default" )
78+
7779
7880def parse_cli (
7981 env_or_list : Type [EnvClass ] | list [Type [EnvClass ]],
@@ -156,7 +158,6 @@ def annot(type_form):
156158 warn (f"Cannot apply { annotations } on Python <= 3.11." )
157159 return type_form
158160
159-
160161 #
161162 # --- Begin to launch tyro.cli ---
162163 # This will be divided into four sections.
@@ -189,7 +190,9 @@ def annot(type_form):
189190 except BaseException :
190191 # Why this exception handling? Try putting this out and test_strange_error_mitigation fails.
191192 if len (env_classes ) > 1 and kwargs .get ("default" ):
192- env = cli (annot (kwargs ["default" ].__class__ ), args = args [1 :], registry = _custom_registry , ** kwargs )
193+ env = cli (
194+ annot (kwargs ["default" ].__class__ ), args = args [1 :], registry = _custom_registry , ** kwargs
195+ )
193196 else :
194197 raise
195198 except SystemExit as exception :
@@ -199,13 +202,20 @@ def annot(type_form):
199202 # Help-text exception, continue here and try again with subcommands. As it raises SystemExit first,
200203 # it will raise SystemExit in the second run too.
201204 helponly = True
202- elif _crawled is None and _subcommands_default_appliable (kwargs , _crawling ) and exception .code == 2 and failed_fields .get ():
205+ elif (
206+ _crawled is None
207+ and _subcommands_default_appliable (kwargs , _crawling )
208+ and exception .code == 2
209+ and failed_fields .get ()
210+ ):
203211 # Some fields are missing, directly try again. If it raises again
204212 # (some fields are really missing which cannot be filled from the subcommanded-config),
205213 # it will immediately raise again and trigger the (C) dialog missing section.
206214 # If it worked (and no fields are missing), we continue here without triggering the (C) dialog missing section.
207215 _crawled = True
208- env , enforce_dialog = _try_with_subcommands (kwargs , m , args , type_form , env_classes , _custom_registry , annot , _req_fields )
216+ env , enforce_dialog = _try_with_subcommands (
217+ kwargs , m , args , type_form , env_classes , _custom_registry , annot , _req_fields
218+ )
209219 else :
210220 # This is either a recurrent call from the (C) dialog missing section (and thus subcommand-config re-parsing was done),
211221 # or there is no subcommand-config data and thus we continue as if this exception handling did not happen.
@@ -220,7 +230,9 @@ def annot(type_form):
220230 if _crawled is None and _subcommands_default_appliable (kwargs , _crawling ):
221231 # Why not catching enforce_dialog here? As we are here, calling tyro.cli worked for the first time.
222232 # For sure then, there were no choose_subcommand dialog, subcommands for sure are all written in the CLI.
223- env , _ = _try_with_subcommands (kwargs , None if helponly else m , args , type_form , env_classes , _custom_registry , annot , _req_fields )
233+ env , _ = _try_with_subcommands (
234+ kwargs , None if helponly else m , args , type_form , env_classes , _custom_registry , annot , _req_fields
235+ )
224236
225237 # Why setting m.env instead of putting into into a constructor of a new get_interface() call?
226238 # 1. Getting the interface is a costly operation
@@ -291,8 +303,9 @@ def annot(type_form):
291303
292304 return env , dialog_raised
293305
306+
294307def _try_with_subcommands (kwargs , m , args , type_form , env_classes , _custom_registry , annot , _req_fields ):
295- """ This awful method is here to re-parse the tyro.cli with the subcommand-config """
308+ """This awful method is here to re-parse the tyro.cli with the subcommand-config"""
296309
297310 failed_fields .set ([])
298311 old_defs = kwargs .get ("default" , {})
@@ -306,7 +319,7 @@ def _try_with_subcommands(kwargs, m, args, type_form, env_classes, _custom_regis
306319 if not old_defs :
307320 old_defs = kwargs ["subcommands_default_union" ][cl_name ]
308321 subc = kwargs ["subcommands_default" ].get (cl_name )
309- else : # we should never come here
322+ else : # we should never come here
310323 raise ValueError ("Subcommands parsing failed" )
311324 else :
312325 env = env_classes [0 ]
@@ -399,7 +412,14 @@ def _dialog_missing(
399412 else :
400413 disk = asdict (dc ) if (dc := kwargs .get ("default" )) else {}
401414 crawled = True
402- kwargs ["default" ] = create_with_missing (env_cl , disk , req_fields , m , subc = kwargs .get ("subcommands_default" ), subc_passage = [cl_name for _ , cl_name , _ in _crawling .get ()])
415+ kwargs ["default" ] = create_with_missing (
416+ env_cl ,
417+ disk ,
418+ req_fields ,
419+ m ,
420+ subc = kwargs .get ("subcommands_default" ),
421+ subc_passage = [cl_name for _ , cl_name , _ in _crawling .get ()],
422+ )
403423
404424 missing_req = _fetch_currently_failed (req_fields )
405425 """ Fields required and missing from CLI """
0 commit comments