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.
pull/10909/head
Eli Schwartz 2 years ago committed by Jussi Pakkanen
parent 9c4d6088b1
commit e27653499a
  1. 13
      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,)

Loading…
Cancel
Save