From d3fd11176df20e3545f00347680fd52ed3f142e8 Mon Sep 17 00:00:00 2001 From: Fiddle-Config Team Date: Tue, 29 Jul 2025 09:45:17 -0700 Subject: [PATCH] Swap the order of Ego TLS and VehicleStatus modalities. PiperOrigin-RevId: 788492879 --- fiddle/_src/printing.py | 21 +++++++++++++++++++-- fiddle/_src/printing_test.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/fiddle/_src/printing.py b/fiddle/_src/printing.py index f9f81b8e..7e6e14e0 100644 --- a/fiddle/_src/printing.py +++ b/fiddle/_src/printing.py @@ -338,13 +338,17 @@ def make_previous_text(entry: history.HistoryEntry) -> str: return f'{_path_str(path)} = {current}{past}' -def as_dict_flattened(cfg: config.Buildable) -> Dict[str, Any]: +def as_dict_flattened( + cfg: config.Buildable, output_fn_or_cls_name: bool = False +) -> Dict[str, Any]: """Returns a flattened dict of cfg's paths (dot syntax) and values. Default values and tags won't be included in the flattened dict. Args: cfg: A buildable to generate a flattened dict representation for. + output_fn_or_cls_name: If true, output the function or class name for + buildables. Returns: A flattened Dict representation of `cfg`. """ @@ -352,9 +356,22 @@ def as_dict_flattened(cfg: config.Buildable) -> Dict[str, Any]: def dict_generate(value, state=None) -> Iterator[_LeafSetting]: state = state or daglish.BasicTraversal.begin(dict_generate, value) - # Rearrange parameters in signature order. if isinstance(value, config.Buildable): + # Rearrange parameters in signature order. value = _rearrange_buildable_args(value, insert_unset_sentinels=False) + # Add the function or class name if it exists. + if ( + output_fn_or_cls_name + and hasattr(value, '__fn_or_cls__') + and hasattr(value.__fn_or_cls__, '__name__') + ): + current_path_with_fn_or_cls_name = state.current_path + ( + daglish.Attr('__fn_or_cls__'), + daglish.Attr('__name__'), + ) + yield _LeafSetting( + current_path_with_fn_or_cls_name, None, value.__fn_or_cls__.__name__ + ) if not _has_nested_builder(value): yield _LeafSetting(state.current_path, None, value) diff --git a/fiddle/_src/printing_test.py b/fiddle/_src/printing_test.py index 188b1dc7..8ebd91d2 100644 --- a/fiddle/_src/printing_test.py +++ b/fiddle/_src/printing_test.py @@ -493,6 +493,13 @@ def test_simple_flattened_dict(self): expected = {'x': 1, 'y': 'abc'} self.assertEqual(output, expected) + def test_simple_flattened_dict_fn_name(self): + cfg = fdl.Config(fn_x_y, 1, 'abc') + output = printing.as_dict_flattened(cfg, output_fn_or_cls_name=True) + + expected = {'__fn_or_cls__.__name__': 'fn_x_y', 'x': 1, 'y': 'abc'} + self.assertEqual(output, expected) + def test_skip_unset_argument(self): cfg = fdl.Config(fn_x_y, 3.14) output = printing.as_dict_flattened(cfg) @@ -507,6 +514,19 @@ def test_nested(self): expected = {'x': 'x', 'y.x': 'nest_x', 'y.y': 123} self.assertEqual(output, expected) + def test_nested_fn_name(self): + cfg = fdl.Config(fn_x_y, 'x', fdl.Config(fn_x_y, 'nest_x', 123)) + output = printing.as_dict_flattened(cfg, output_fn_or_cls_name=True) + + expected = { + '__fn_or_cls__.__name__': 'fn_x_y', + 'x': 'x', + 'y.__fn_or_cls__.__name__': 'fn_x_y', + 'y.x': 'nest_x', + 'y.y': 123, + } + self.assertEqual(output, expected) + def test_class(self): cfg = fdl.Config(SampleClass, 'a_param', b=123) output = printing.as_dict_flattened(cfg) @@ -514,6 +534,17 @@ def test_class(self): expected = {'a': 'a_param', 'b': 123} self.assertEqual(output, expected) + def test_class_cls_name(self): + cfg = fdl.Config(SampleClass, 'a_param', b=123) + output = printing.as_dict_flattened(cfg, output_fn_or_cls_name=True) + + expected = { + '__fn_or_cls__.__name__': 'SampleClass', + 'a': 'a_param', + 'b': 123, + } + self.assertEqual(output, expected) + def test_kwargs(self): cfg = fdl.Config(fn_with_kwargs, 1, abc='extra kwarg value') output = printing.as_dict_flattened(cfg)