diff --git a/gin/config.py b/gin/config.py index 4e46d43..208cc77 100644 --- a/gin/config.py +++ b/gin/config.py @@ -1217,7 +1217,10 @@ def _get_bindings(selector: str) -> Dict[str, Any]: return new_kwargs -def get_bindings(fn_or_cls_or_selector: _FnOrClsOrSelector) -> Dict[str, Any]: +def get_bindings( + fn_or_cls_or_selector: _FnOrClsOrSelector, + resolve_references: bool = True, +) -> Dict[str, Any]: """Returns the bindings associated with the given configurable. Any configurable references in the bindings will be resolved during the call @@ -1235,11 +1238,17 @@ def get_bindings(fn_or_cls_or_selector: _FnOrClsOrSelector) -> Dict[str, Any]: Args: fn_or_cls_or_selector: Configurable function, class or selector `str`. + resolve_references: Whether or not macro and references should be resolved. + If `False`, the output should not be mutated. Returns: The bindings kwargs injected by Gin. """ - return copy.deepcopy(_get_bindings(_as_selector(fn_or_cls_or_selector))) + bindings_kwargs = _get_bindings(_as_selector(fn_or_cls_or_selector)) + if resolve_references: + return copy.deepcopy(bindings_kwargs) + else: + return bindings_kwargs def get_configurable( diff --git a/tests/config_test.py b/tests/config_test.py index 311f4d9..b599735 100644 --- a/tests/config_test.py +++ b/tests/config_test.py @@ -2255,6 +2255,22 @@ def testGetBindingsScope(self): 'kwarg2': 456, }) + def testGetBindingsReferences(self): + # `resolve_references=True` + config_str = """ + configurable1.non_kwarg = @configurable2 + """ + config.parse_config(config_str) + self.assertDictEqual(config.get_bindings('configurable1'), { + 'non_kwarg': configurable2, + }) + + # `resolve_references=False` + config.parse_config(config_str) + non_kwarg = config.get_bindings( + 'configurable1', resolve_references=False)['non_kwarg'] + self.assertIsInstance(non_kwarg, config.ConfigurableReference) + def testGetBindingsUnknown(self): expected_msg = 'Could not find .* in the Gin registry' with self.assertRaisesRegex(ValueError, expected_msg):