Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions gin/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
16 changes: 16 additions & 0 deletions tests/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down