Skip to content

Commit

Permalink
fixes partial callable
Browse files Browse the repository at this point in the history
Signed-off-by: Wenqi Li <[email protected]>
  • Loading branch information
wyli committed Jan 14, 2023
1 parent f14d50a commit 9212e2b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
5 changes: 2 additions & 3 deletions monai/utils/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from collections.abc import Callable, Collection, Hashable, Mapping
from functools import partial, wraps
from importlib import import_module
from inspect import isclass, isfunction, ismethod
from inspect import isclass
from pkgutil import walk_packages
from pydoc import locate
from re import match
Expand Down Expand Up @@ -241,8 +241,7 @@ def instantiate(path: str, **kwargs):
pdb.set_trace()
if isclass(component):
return component(**kwargs)
# support regular function, static method and class method
if isfunction(component) or (ismethod(component) and isclass(getattr(component, "__self__", None))):
if callable(component): # support regular function, static method and class method
return partial(component, **kwargs)
except Exception as e:
raise RuntimeError(f"Failed to instantiate '{path}' with kwargs: {kwargs}") from e
Expand Down
4 changes: 4 additions & 0 deletions tests/test_config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ def test_get_via_attributes(self):
result = trans(np.ones(64))
self.assertTupleEqual(result.shape, (1, 8, 8))

def test_builtin(self):
config = {"import statements": "$import math", "calc": {"_target_": "math.isclose", "a": 0.001, "b": 0.001}}
self.assertEqual(ConfigParser(config).calc(), True)


if __name__ == "__main__":
unittest.main()

0 comments on commit 9212e2b

Please sign in to comment.