From 9ef3c508543e0e00b8a54d4035a755f099d0c52d Mon Sep 17 00:00:00 2001 From: John Omotani Date: Sun, 20 Nov 2022 13:36:22 +0000 Subject: [PATCH] Better error message when wrong type is passed 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. --- optionsfactory/_utils.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/optionsfactory/_utils.py b/optionsfactory/_utils.py index 28dc77d..f662513 100644 --- a/optionsfactory/_utils.py +++ b/optionsfactory/_utils.py @@ -1,3 +1,4 @@ +from collections.abc import Sequence from numbers import Number @@ -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)}" ) @@ -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__