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

6976 improve config expr error msg #6977

Merged
merged 1 commit into from
Sep 13, 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: 7 additions & 3 deletions monai/bundle/config_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from abc import ABC, abstractmethod
from collections.abc import Mapping, Sequence
from importlib import import_module
from pprint import pformat
from typing import Any

from monai.bundle.utils import EXPR_KEY
Expand Down Expand Up @@ -157,7 +158,7 @@
return self.config

def __repr__(self) -> str:
return str(self.config)
return f"{type(self).__name__}: \n{pformat(self.config)}"

Check warning on line 161 in monai/bundle/config_item.py

View check run for this annotation

Codecov / codecov/patch

monai/bundle/config_item.py#L161

Added line #L161 was not covered by tests


class ConfigComponent(ConfigItem, Instantiable):
Expand Down Expand Up @@ -291,7 +292,7 @@
try:
return instantiate(modname, mode, **args)
except Exception as e:
raise RuntimeError(f"Failed to instantiate {self}.") from e
raise RuntimeError(f"Failed to instantiate {self}") from e

Check warning on line 295 in monai/bundle/config_item.py

View check run for this annotation

Codecov / codecov/patch

monai/bundle/config_item.py#L295

Added line #L295 was not covered by tests


class ConfigExpression(ConfigItem):
Expand Down Expand Up @@ -372,7 +373,10 @@
warnings.warn(f"the new global variable `{k}` conflicts with `self.globals`, override it.")
globals_[k] = v
if not run_debug:
return eval(value[len(self.prefix) :], globals_, locals)
try:
return eval(value[len(self.prefix) :], globals_, locals)
except Exception as e:
raise RuntimeError(f"Failed to evaluate {self}") from e

Check warning on line 379 in monai/bundle/config_item.py

View check run for this annotation

Codecov / codecov/patch

monai/bundle/config_item.py#L378-L379

Added lines #L378 - L379 were not covered by tests
warnings.warn(
f"\n\npdb: value={value}\n"
f"See also Debugger commands documentation: https://docs.python.org/3/library/pdb.html\n"
Expand Down
4 changes: 4 additions & 0 deletions tests/test_config_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@
flag = expr.is_import_statement(expr.config)
self.assertEqual(flag, expected)

def test_error_expr(self):
with self.assertRaisesRegex(RuntimeError, r"1\+\[\]"):
ConfigExpression(id="", config="$1+[]").evaluate()

Check warning on line 133 in tests/test_config_item.py

View check run for this annotation

Codecov / codecov/patch

tests/test_config_item.py#L132-L133

Added lines #L132 - L133 were not covered by tests


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