Skip to content

Commit

Permalink
Better error message when wrong type is passed
Browse files Browse the repository at this point in the history
Prints the meta.value_type.__name__. This avoids special characters so
ensures that the message is printed sensibly even if it is passed to
something that, e.g., renders it as HTML.
  • Loading branch information
johnomotani committed Nov 20, 2022
1 parent 12a4ad7 commit 9ef3c50
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion optionsfactory/_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections.abc import Sequence
from numbers import Number


Expand All @@ -11,7 +12,7 @@ def _checked(value, *, meta=None, name=None):
and (not isinstance(value, meta.value_type))
):
raise TypeError(
f"{value} is not of type {meta.value_type}"
f"{value} is not of type {_stringify_sequence_of_types(meta.value_type)}"
f"{'' if name is None else ' for key=' + str(name)}"
)

Expand Down Expand Up @@ -86,3 +87,10 @@ def _options_table_subsection(options, subsection_name):
result += _options_table_subsection(options, None)

return result


def _stringify_sequence_of_types(types):
if isinstance(types, Sequence):
return tuple(_stringify_sequence_of_types(t) for t in types)
else:
return types.__name__

0 comments on commit 9ef3c50

Please sign in to comment.