Skip to content

Commit 4ba89bd

Browse files
authored
fix: adaptor map_names calls dict as function when inputs is a name-mapping dict (#8907)
**fix: adaptor map_names calls dict as function when inputs is a name-mapping dict** When `adaptor` wraps a function with `**kwargs` and `inputs` is a dict, the `map_names` helper calls `input_map(k, k)` instead of `input_map.get(k, k)`, raising `TypeError` before any data reaches the wrapped function. The fix uses `input_map.get(k, k)` so the dict is looked up rather than called, falling back to the original key when no mapping exists. ### Types of changes - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [x] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. --------- Signed-off-by: Soumya Snigdha Kundu <soumya_snigdha.kundu@kcl.ac.uk>
1 parent 5e42fbf commit 4ba89bd

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

‎monai/transforms/adaptors.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def must_be_types(variable_name, variable, types):
140140
raise TypeError(f"'{variable_name}' must be one of {types} but is {type(variable)}")
141141

142142
def map_names(ditems, input_map):
143-
return {input_map(k, k): v for k, v in ditems.items()}
143+
return {input_map.get(k, k): v for k, v in ditems.items()}
144144

145145
def map_only_names(ditems, input_map):
146146
return {v: ditems[k] for k, v in input_map.items()}

‎tests/transforms/test_adaptors.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ def foo(a):
125125
dres = adaptor(foo, {"a": "b"}, {"b": "a"})(d)
126126
self.assertEqual(dres["b"], 4)
127127

128+
def test_kwargs_with_dict_inputs(self):
129+
130+
def foo(**kwargs):
131+
return {k: v * 2 for k, v in kwargs.items()}
132+
133+
d = {"x": 3}
134+
dres = adaptor(foo, {"out": "out"}, {"x": "out"})(d)
135+
self.assertEqual(dres["out"], 6)
136+
128137

129138
class TestApplyAlias(unittest.TestCase):
130139

0 commit comments

Comments
 (0)