From 0f703f1d8d3bbf14a4874954e4b2e87308321fb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20=C3=96zdemir?= Date: Mon, 13 Jul 2026 08:04:36 -0700 Subject: [PATCH 1/2] Print dicts of namedtuples as values Treat dicts whose values are namedtuples as simple groups so Fire prints them inline instead of showing a help screen. Fixes #230. Co-authored-by: Cursor --- fire/core.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/fire/core.py b/fire/core.py index 8e23e76b..7dabe5c3 100644 --- a/fire/core.py +++ b/fire/core.py @@ -351,6 +351,9 @@ def _OneLineResult(result): if inspect.ismodule(result): return f'' + if inspectutils.IsNamedTuple(result): + return json.dumps(result._asdict(), ensure_ascii=False) + try: # Don't force conversion to ascii. return json.dumps(result, ensure_ascii=False) From d9cb59e0234b0c129772953ad676d9aee1c692aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20=C3=96zdemir?= Date: Mon, 13 Jul 2026 08:21:43 -0700 Subject: [PATCH 2/2] Add tests and IsSimpleGroup namedtuple handling for #230. Co-authored-by: Cursor --- fire/core_test.py | 7 +++++++ fire/value_types.py | 3 ++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/fire/core_test.py b/fire/core_test.py index f48d6e2d..5d85cb56 100644 --- a/fire/core_test.py +++ b/fire/core_test.py @@ -14,6 +14,7 @@ """Tests for the core module.""" +import collections from unittest import mock from fire import core @@ -137,6 +138,12 @@ def testPrintOrderedDict(self): with self.assertOutputMatches(stdout='{}'): core.Fire(tc.OrderedDictionary, command=['empty']) + def testPrintDictOfNamedTuples(self): + point = collections.namedtuple('Point', ['x', 'y']) + data = {'a': point(1, 2), 'b': point(3, 4)} + with self.assertOutputMatches(stdout=r'a:\s+\{"x": 1, "y": 2\}', stderr=None): + core.Fire(data, command=[]) + def testPrintNamedTupleField(self): with self.assertOutputMatches(stdout='11', stderr=None): core.Fire(tc.NamedTuple, command=['point', 'x']) diff --git a/fire/value_types.py b/fire/value_types.py index 81308973..36f032f6 100644 --- a/fire/value_types.py +++ b/fire/value_types.py @@ -50,7 +50,8 @@ def IsSimpleGroup(component): """ assert isinstance(component, dict) for unused_key, value in component.items(): - if not IsValue(value) and not isinstance(value, (list, dict)): + if (not IsValue(value) and not isinstance(value, (list, dict)) + and not inspectutils.IsNamedTuple(value)): return False return True