From dc80ec9511ada0a10691a116189721d957e70040 Mon Sep 17 00:00:00 2001 From: saivedant169 Date: Mon, 16 Mar 2026 14:02:28 -0400 Subject: [PATCH] Catch TypeError from function calls and convert to FireError MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a function call raises TypeError due to missing or unexpected arguments, convert it to FireError so it flows through Fire's error display pipeline (error message + usage text) instead of crashing with a raw Python traceback. The existing _ParseArgs validation catches most missing-argument cases, but edge cases (e.g. *args unpacking mismatches, argument type conflicts) can slip through to the actual fn() call. Only argument-related TypeErrors are caught — other TypeErrors (e.g. from the function body itself) are re-raised unchanged. Fixes #633 --- fire/core.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/fire/core.py b/fire/core.py index 8e23e76b..52403677 100644 --- a/fire/core.py +++ b/fire/core.py @@ -677,17 +677,22 @@ def _CallAndUpdateTrace(component, args, component_trace, treatment='class', (varargs, kwargs), consumed_args, remaining_args, capacity = parse(args) # Call the function. - if inspectutils.IsCoroutineFunction(fn): - try: - loop = asyncio.get_running_loop() - except RuntimeError: - # No event loop running, create a new one - component = asyncio.run(fn(*varargs, **kwargs)) + try: + if inspectutils.IsCoroutineFunction(fn): + try: + loop = asyncio.get_running_loop() + except RuntimeError: + # No event loop running, create a new one + component = asyncio.run(fn(*varargs, **kwargs)) + else: + # Event loop is already running + component = loop.run_until_complete(fn(*varargs, **kwargs)) else: - # Event loop is already running - component = loop.run_until_complete(fn(*varargs, **kwargs)) - else: - component = fn(*varargs, **kwargs) + component = fn(*varargs, **kwargs) + except TypeError as e: + if 'required' in str(e) or 'missing' in str(e) or 'argument' in str(e): + raise FireError(str(e)) from None + raise if treatment == 'class': action = trace.INSTANTIATED_CLASS