interpreter: simplify and export env validator/convertor mypy is now able to deal with extra optional arguments. Also export env_validator instead of fishing it out of ENV_KW. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
diff --git a/mesonbuild/interpreter/interpreter.py b/mesonbuild/interpreter/interpreter.py index 707e856..fa8ee55 100644 --- a/mesonbuild/interpreter/interpreter.py +++ b/mesonbuild/interpreter/interpreter.py
@@ -98,7 +98,8 @@ TEST_KWS, NoneType, in_set_validator, - env_convertor_with_method + env_convertor, + env_validator, ) from . import primitives as P_OBJ @@ -3226,13 +3227,13 @@ init = args[0] if init is not None: FeatureNew.single_use('environment positional arguments', '0.52.0', self.subproject, location=node) - msg = ENV_KW.validator(init) + msg = env_validator(init) if msg: raise InvalidArguments(f'"environment": {msg}') if isinstance(init, dict) and any(i for i in init.values() if isinstance(i, list)): FeatureNew.single_use('List of string in dictionary value', '0.62.0', self.subproject, location=node) # the validator call above ensured that we have the correct type - return env_convertor_with_method(T.cast('FullEnvInitValueType', init), kwargs['method'], kwargs['separator']) + return env_convertor(T.cast('FullEnvInitValueType', init), kwargs['method'], kwargs['separator']) return EnvironmentVariables() @typed_pos_args('join_paths', varargs=str, min_varargs=1)
diff --git a/mesonbuild/interpreter/mesonmain.py b/mesonbuild/interpreter/mesonmain.py index 9501131..c1436ed 100644 --- a/mesonbuild/interpreter/mesonmain.py +++ b/mesonbuild/interpreter/mesonmain.py
@@ -15,13 +15,13 @@ from ..mesonlib import MachineChoice from ..options import OptionKey from ..programs import Program, ExternalProgram -from ..interpreter.type_checking import ENV_KW, ENV_METHOD_KW, ENV_SEPARATOR_KW, env_convertor_with_method +from ..interpreter.type_checking import ENV_METHOD_KW, ENV_SEPARATOR_KW, env_convertor from ..interpreterbase import (MesonInterpreterObject, FeatureNew, FeatureDeprecated, FeatureBroken, typed_pos_args, noArgsFlattening, noPosargs, noKwargs, typed_kwargs, KwargInfo, InterpreterException, InterpreterObject) from .decorators import apply_machine_map from .primitives import MesonVersionString -from .type_checking import NATIVE_KW, NoneType +from .type_checking import NATIVE_KW, NoneType, env_validator if T.TYPE_CHECKING: from typing_extensions import Literal, TypedDict @@ -488,10 +488,10 @@ def add_devenv_method(self, args: T.Tuple[T.Union[str, list, dict, mesonlib.EnvironmentVariables]], kwargs: 'AddDevenvKW') -> None: env = args[0] - msg = ENV_KW.validator(env) + msg = env_validator(env) if msg: raise build.InvalidArguments(f'"add_devenv": {msg}') - converted = env_convertor_with_method(env, kwargs['method'], kwargs['separator']) + converted = env_convertor(env, kwargs['method'], kwargs['separator']) assert isinstance(converted, mesonlib.EnvironmentVariables) self.build.devenv.append(converted)
diff --git a/mesonbuild/interpreter/type_checking.py b/mesonbuild/interpreter/type_checking.py index 03bf408..6f255db 100644 --- a/mesonbuild/interpreter/type_checking.py +++ b/mesonbuild/interpreter/type_checking.py
@@ -196,8 +196,8 @@ DISABLER_KW: KwargInfo[bool] = KwargInfo('disabler', bool, default=False) -def _env_validator(value: T.Union[EnvironmentVariables, T.List['TYPE_var'], T.Dict[str, 'TYPE_var'], str, None], - only_dict_str: bool = True) -> T.Optional[str]: +def env_validator(value: T.Union[EnvironmentVariables, T.List['TYPE_var'], T.Dict[str, 'TYPE_var'], str, None], + only_dict_str: bool = True) -> T.Optional[str]: def _splitter(v: str) -> T.Optional[str]: split = v.split('=', 1) if len(split) == 1: @@ -232,7 +232,7 @@ def _options_validator(value: T.Union[EnvironmentVariables, T.List['TYPE_var'], T.Dict[str, 'TYPE_var'], str, None]) -> T.Optional[str]: # Reusing the env validator is a little overkill, but nicer than duplicating the code - return _env_validator(value, only_dict_str=False) + return env_validator(value, only_dict_str=False) def split_equal_string(input: str) -> T.Tuple[str, str]: """Split a string in the form `x=y` @@ -242,11 +242,9 @@ a, b = input.split('=', 1) return (a, b) -# Split _env_convertor() and env_convertor_with_method() to make mypy happy. -# It does not want extra arguments in KwargInfo convertor callable. -def env_convertor_with_method(value: FullEnvInitValueType, - init_method: Literal['set', 'prepend', 'append'] = 'set', - separator: str = os.pathsep) -> EnvironmentVariables: +def env_convertor(value: FullEnvInitValueType, + init_method: Literal['set', 'prepend', 'append'] = 'set', + separator: str = os.pathsep) -> EnvironmentVariables: if isinstance(value, str): return EnvironmentVariables(dict([split_equal_string(value)]), init_method, separator) elif isinstance(value, list): @@ -257,14 +255,11 @@ return EnvironmentVariables() return value -def _env_convertor(value: FullEnvInitValueType) -> EnvironmentVariables: - return env_convertor_with_method(value) - ENV_KW: KwargInfo[T.Union[EnvironmentVariables, T.List, T.Dict, str, None]] = KwargInfo( 'env', (EnvironmentVariables, list, dict, str, NoneType), - validator=_env_validator, - convertor=_env_convertor, + validator=env_validator, + convertor=env_convertor, ) DEPFILE_KW: KwargInfo[T.Optional[str]] = KwargInfo(