From e27653499a20b48c25424f645aae23d9dbca91af Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Sat, 22 Oct 2022 23:42:58 -0400 Subject: 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. --- mesonbuild/interpreterbase/decorators.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'mesonbuild/interpreterbase') 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,) -- cgit v1.1