diff options
author | Dylan Baker <dylan@pnwbakers.com> | 2021-11-19 10:39:21 -0800 |
---|---|---|
committer | Dylan Baker <dylan@pnwbakers.com> | 2021-11-22 12:24:14 -0800 |
commit | ecc47d67a945760c3d5e739581fa984bfea6a840 (patch) | |
tree | 8bf93ff2ef715e9f41d010bd77af76b7ba56eb10 /mesonbuild/interpreterbase | |
parent | 1f6351461c5d3654aeff2355c96fe54bb28469f8 (diff) | |
download | meson-ecc47d67a945760c3d5e739581fa984bfea6a840.zip meson-ecc47d67a945760c3d5e739581fa984bfea6a840.tar.gz meson-ecc47d67a945760c3d5e739581fa984bfea6a840.tar.bz2 |
typed_kwargs: provide better error messages for wrong container types
Currently, if you pass a `[]string`, but the argument expects
`[]number`, then you get a message like `expected list[str] but got
list`. That isn't helpful. With this patch arrays and dictionaries will
both print messages with the types provided.
Diffstat (limited to 'mesonbuild/interpreterbase')
-rw-r--r-- | mesonbuild/interpreterbase/decorators.py | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/mesonbuild/interpreterbase/decorators.py b/mesonbuild/interpreterbase/decorators.py index 16aa7d2..817cb85 100644 --- a/mesonbuild/interpreterbase/decorators.py +++ b/mesonbuild/interpreterbase/decorators.py @@ -467,6 +467,18 @@ def typed_kwargs(name: str, *types: KwargInfo) -> T.Callable[..., T.Any]: shouldbe += ', '.join(candidates) return shouldbe + def raw_description(t: object) -> str: + """describe a raw type (ie, one that is not a ContainerTypeInfo).""" + if isinstance(t, list): + if t: + return f"list[{' | '.join(sorted(mesonlib.OrderedSet(type(v).__name__ for v in t)))}]" + return 'list[]' + elif isinstance(t, dict): + if t: + return f"dict[{' | '.join(sorted(mesonlib.OrderedSet(type(v).__name__ for v in t.values())))}]" + return 'dict[]' + return type(t).__name__ + def check_value_type(types_tuple: T.Tuple[T.Union[T.Type, ContainerTypeInfo], ...], value: T.Any) -> bool: for t in types_tuple: @@ -503,7 +515,7 @@ def typed_kwargs(name: str, *types: KwargInfo) -> T.Callable[..., T.Any]: kwargs[info.name] = value = mesonlib.listify(value) if not check_value_type(types_tuple, value): shouldbe = types_description(types_tuple) - raise InvalidArguments(f'{name} keyword argument {info.name!r} was of type {type(value).__name__!r} but should have been {shouldbe}') + raise InvalidArguments(f'{name} keyword argument {info.name!r} was of type {raw_description(value)} but should have been {shouldbe}') if info.validator is not None: msg = info.validator(value) |