Skip to content

Commit ff479ac

Browse files
committed
Make exceptions less verbose by default
1 parent 92c3b49 commit ff479ac

File tree

3 files changed

+28
-34
lines changed

3 files changed

+28
-34
lines changed

pytensor/compile/function/types.py

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -950,32 +950,27 @@ def __call__(self, *args, output_subset=None, **kwargs):
950950

951951
except Exception as e:
952952
i = input_storage.index(arg_container)
953-
function_name = "pytensor function"
954-
argument_name = "argument"
955-
if self.name:
956-
function_name += ' with name "' + self.name + '"'
957-
if hasattr(arg, "name") and arg.name:
958-
argument_name += ' with name "' + arg.name + '"'
959-
where = get_variable_trace_string(self.maker.inputs[i].variable)
960-
if len(e.args) == 1:
961-
e.args = (
962-
"Bad input "
963-
+ argument_name
964-
+ " to "
965-
+ function_name
966-
+ f" at index {int(i)} (0-based). {where}"
967-
+ e.args[0],
953+
function_name = (
954+
"pytensor function"
955+
if self.name
956+
else f"pytensor function '{self.name}'"
957+
)
958+
argument_name = (
959+
f"argument {arg.name}"
960+
if hasattr(arg, "name")
961+
else "argument"
962+
)
963+
where = (
964+
""
965+
if config.exception_verbosity == "low"
966+
else get_variable_trace_string(
967+
self.maker.inputs[i].variable
968968
)
969-
else:
970-
e.args = (
971-
"Bad input "
972-
+ argument_name
973-
+ " to "
974-
+ function_name
975-
+ f" at index {int(i)} (0-based). {where}"
976-
) + e.args
977-
self._restore_defaults()
978-
raise
969+
)
970+
e.add_note(
971+
f"\nInvalid {argument_name} to {function_name} at index {i}.{where}"
972+
)
973+
raise e
979974
arg_container.provided += 1
980975

981976
# Set keyword arguments

pytensor/configdefaults.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -644,15 +644,8 @@ def add_error_and_warning_configvars():
644644
# on all important apply nodes.
645645
config.add(
646646
"exception_verbosity",
647-
"If 'low', the text of exceptions will generally refer "
648-
"to apply nodes with short names such as "
649-
"Elemwise{add_no_inplace}. If 'high', some exceptions "
650-
"will also refer to apply nodes with long descriptions "
651-
""" like:
652-
A. Elemwise{add_no_inplace}
653-
B. log_likelihood_v_given_h
654-
C. log_likelihood_h""",
655-
EnumStr("low", ["high"]),
647+
"Verbosity of exceptions generated by PyTensor functions.",
648+
EnumStr("low", ["medium", "high"]),
656649
in_c_key=False,
657650
)
658651

pytensor/link/utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,12 @@ def raise_with_op(
313313
# print a simple traceback from KeyboardInterrupt
314314
raise exc_value.with_traceback(exc_trace)
315315

316+
if verbosity == "low":
317+
exc_value.add_note(
318+
"\nHINT: Set PyTensor `config.exception_verbosity` to `medium` or `high` for more information about the source of the error."
319+
)
320+
raise exc_value.with_traceback(exc_trace)
321+
316322
trace = getattr(node.outputs[0].tag, "trace", ())
317323

318324
exc_value.__thunk_trace__ = trace

0 commit comments

Comments
 (0)