aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEli Schwartz <eschwartz@archlinux.org>2022-10-22 23:42:58 -0400
committerJussi Pakkanen <jpakkane@gmail.com>2022-10-24 15:16:02 +0300
commite27653499a20b48c25424f645aae23d9dbca91af (patch)
tree07ca64ee01f9b1c6549ec89aed33336fefc3c169
parent9c4d6088b1f061c7cd8dba00eda5e72427ad3d0e (diff)
downloadmeson-e27653499a20b48c25424f645aae23d9dbca91af.zip
meson-e27653499a20b48c25424f645aae23d9dbca91af.tar.gz
meson-e27653499a20b48c25424f645aae23d9dbca91af.tar.bz2
add option to typed_kwargs that allows unknown kwargs through
Some functions cannot be fully type checked, because our API allows fully arbitrary kwargs and treats them as data to pass through to the underlying feature. For example, hotdoc command line arguments. This change allows us to type check some kwargs with known types and possibly required status, and make their values consistent(ly defaultable), while preserving the optional nature of the additional kwargs.
-rw-r--r--mesonbuild/interpreterbase/decorators.py13
1 files changed, 7 insertions, 6 deletions
diff --git a/mesonbuild/interpreterbase/decorators.py b/mesonbuild/interpreterbase/decorators.py
index 50f2475..10d04b5 100644
--- a/mesonbuild/interpreterbase/decorators.py
+++ b/mesonbuild/interpreterbase/decorators.py
@@ -452,7 +452,7 @@ class KwargInfo(T.Generic[_T]):
)
-def typed_kwargs(name: str, *types: KwargInfo) -> T.Callable[..., T.Any]:
+def typed_kwargs(name: str, *types: KwargInfo, allow_unknown: bool = False) -> T.Callable[..., T.Any]:
"""Decorator for type checking keyword arguments.
Used to wrap a meson DSL implementation function, where it checks various
@@ -529,11 +529,12 @@ def typed_kwargs(name: str, *types: KwargInfo) -> T.Callable[..., T.Any]:
# Cast here, as the convertor function may place something other than a TYPE_var in the kwargs
kwargs = T.cast('T.Dict[str, object]', _kwargs)
- all_names = {t.name for t in types}
- unknowns = set(kwargs).difference(all_names)
- if unknowns:
- ustr = ', '.join([f'"{u}"' for u in sorted(unknowns)])
- raise InvalidArguments(f'{name} got unknown keyword arguments {ustr}')
+ if not allow_unknown:
+ all_names = {t.name for t in types}
+ unknowns = set(kwargs).difference(all_names)
+ if unknowns:
+ ustr = ', '.join([f'"{u}"' for u in sorted(unknowns)])
+ raise InvalidArguments(f'{name} got unknown keyword arguments {ustr}')
for info in types:
types_tuple = info.types if isinstance(info.types, tuple) else (info.types,)