Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parent PR #130

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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