diff options
author | Dylan Baker <dylan@pnwbakers.com> | 2021-06-14 15:36:16 -0700 |
---|---|---|
committer | Dylan Baker <dylan@pnwbakers.com> | 2021-06-22 09:12:54 -0700 |
commit | dd296f321ba102052dcf3d0a67e4321752d560a2 (patch) | |
tree | 128a96df875cdcde36e3e647ec605866b5563270 /mesonbuild/interpreterbase | |
parent | a024f432dd1cd406fa80334f63e91939536d3f79 (diff) | |
download | meson-dd296f321ba102052dcf3d0a67e4321752d560a2.zip meson-dd296f321ba102052dcf3d0a67e4321752d560a2.tar.gz meson-dd296f321ba102052dcf3d0a67e4321752d560a2.tar.bz2 |
interpreterbase: Add evolve to KwargInfo
This works just like OptionKey.evolve, pass new keyword arguments to
override old ones, otherwise the original versions are copied to the new
one.
Diffstat (limited to 'mesonbuild/interpreterbase')
-rw-r--r-- | mesonbuild/interpreterbase/decorators.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/mesonbuild/interpreterbase/decorators.py b/mesonbuild/interpreterbase/decorators.py index a011b66..b05df5b 100644 --- a/mesonbuild/interpreterbase/decorators.py +++ b/mesonbuild/interpreterbase/decorators.py @@ -272,6 +272,11 @@ class ContainerTypeInfo: _T = T.TypeVar('_T') +class _NULL_T: + """Special null type for evolution, this is an implementation detail.""" + + +_NULL = _NULL_T() class KwargInfo(T.Generic[_T]): @@ -329,6 +334,42 @@ class KwargInfo(T.Generic[_T]): self.convertor = convertor self.not_set_warning = not_set_warning + def evolve(self, *, + required: T.Union[bool, _NULL_T] = _NULL, + listify: T.Union[bool, _NULL_T] = _NULL, + default: T.Union[_T, None, _NULL_T] = _NULL, + since: T.Union[str, None, _NULL_T] = _NULL, + since_values: T.Union[T.Dict[str, str], None, _NULL_T] = _NULL, + deprecated: T.Union[str, None, _NULL_T] = _NULL, + deprecated_values: T.Union[T.Dict[str, str], None, _NULL_T] = _NULL, + validator: T.Union[T.Callable[[_T], T.Optional[str]], None, _NULL_T] = _NULL, + convertor: T.Union[T.Callable[[_T], TYPE_var], None, _NULL_T] = _NULL) -> 'KwargInfo': + """Create a shallow copy of this KwargInfo, with modifications. + + This allows us to create a new copy of a KwargInfo with modifications. + This allows us to use a shared kwarg that implements complex logic, but + has slight differences in usage, such as being added to different + functions in different versions of Meson. + + The use the _NULL special value here allows us to pass None, which has + meaning in many of these cases. _NULL itself is never stored, always + being replaced by either the copy in self, or the provided new version. + """ + return type(self)( + self.name, + self.types, + listify=listify if not isinstance(listify, _NULL_T) else self.listify, + required=required if not isinstance(required, _NULL_T) else self.required, + default=default if not isinstance(default, _NULL_T) else self.default, + since=since if not isinstance(since, _NULL_T) else self.since, + since_values=since_values if not isinstance(since_values, _NULL_T) else self.since_values, + deprecated=deprecated if not isinstance(deprecated, _NULL_T) else self.deprecated, + deprecated_values=deprecated_values if not isinstance(deprecated_values, _NULL_T) else self.deprecated_values, + validator=validator if not isinstance(validator, _NULL_T) else self.validator, + convertor=convertor if not isinstance(convertor, _NULL_T) else self.convertor, + ) + + def typed_kwargs(name: str, *types: KwargInfo) -> T.Callable[..., T.Any]: """Decorator for type checking keyword arguments. |