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

Fix instantiate for object instantiation with attribute path #5866

Merged
merged 2 commits into from
Jan 18, 2023
Merged
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
10 changes: 5 additions & 5 deletions monai/utils/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def load_submodules(basemod, load_all: bool = True, exclude_pattern: str = "(.*[
return submodules, err_mod


def instantiate(path: str, **kwargs):
def instantiate(__path: str, **kwargs):
wyli marked this conversation as resolved.
Show resolved Hide resolved
"""
Create an object instance or partial function from a class or function represented by string.
`kwargs` will be part of the input arguments to the class constructor or function.
Expand All @@ -226,9 +226,9 @@ def instantiate(path: str, **kwargs):
for `partial` function.

"""
component = locate(path) if isinstance(path, str) else path
component = locate(__path) if isinstance(__path, str) else __path
if component is None:
raise ModuleNotFoundError(f"Cannot locate class or function path: '{path}'.")
raise ModuleNotFoundError(f"Cannot locate class or function path: '{__path}'.")
try:
if kwargs.pop("_debug_", False) or run_debug:
warnings.warn(
Expand All @@ -243,9 +243,9 @@ def instantiate(path: str, **kwargs):
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
raise RuntimeError(f"Failed to instantiate '{__path}' with kwargs: {kwargs}") from e

warnings.warn(f"Component to instantiate must represent a valid class or function, but got {path}.")
warnings.warn(f"Component to instantiate must represent a valid class or function, but got {__path}.")
return component


Expand Down
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ max_line_length = 120
# N812 lowercase 'torch.nn.functional' imported as non lowercase 'F'
# B023 https://github.com/Project-MONAI/MONAI/issues/4627
# B028 https://github.com/Project-MONAI/MONAI/issues/5855
# B907 https://github.com/Project-MONAI/MONAI/issues/5868
ignore =
E203
E501
Expand All @@ -155,6 +156,7 @@ ignore =
B023
B905
B028
B907
per_file_ignores = __init__.py: F401, __main__.py: F401
exclude = *.pyi,.git,.eggs,monai/_version.py,versioneer.py,venv,.venv,_version.py

Expand Down