Skip to content

Commit

Permalink
6976 improve config expr error msg (#6977)
Browse files Browse the repository at this point in the history
Fixes #6976 



### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [x] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [x] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [ ] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.

Signed-off-by: Wenqi Li <[email protected]>
  • Loading branch information
wyli authored Sep 13, 2023
1 parent d4dc055 commit c22a2bd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
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 @@ def get_config(self):
return self.config

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


class ConfigComponent(ConfigItem, Instantiable):
Expand Down Expand Up @@ -291,7 +292,7 @@ def instantiate(self, **kwargs: Any) -> object:
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


class ConfigExpression(ConfigItem):
Expand Down Expand Up @@ -372,7 +373,10 @@ def evaluate(self, globals: dict | None = None, locals: dict | None = None) -> s
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
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 @@ def test_is_import_stmt(self, stmt, expected):
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()


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

0 comments on commit c22a2bd

Please sign in to comment.