From 9212e2b5195e62ae3a93d1950c213bc41c9cb017 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Sat, 14 Jan 2023 19:38:33 +0000 Subject: [PATCH] fixes partial callable Signed-off-by: Wenqi Li --- monai/utils/module.py | 5 ++--- tests/test_config_parser.py | 4 ++++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/monai/utils/module.py b/monai/utils/module.py index 2f2d5069dd6..1670ecadadc 100644 --- a/monai/utils/module.py +++ b/monai/utils/module.py @@ -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 @@ -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 diff --git a/tests/test_config_parser.py b/tests/test_config_parser.py index 23e816861f5..00945a46ba7 100644 --- a/tests/test_config_parser.py +++ b/tests/test_config_parser.py @@ -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()