From 848614ababa9d10a3285029acf6635aeaad8567a Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Tue, 25 Jul 2023 09:41:53 -0700 Subject: modules/python: typed_kwargs for subdir We cannot re-use the existing KwargInfo, since we need to know if the keyword was set explicitly or not, since it conflicts with `install_dir` --- mesonbuild/modules/python.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'mesonbuild/modules/python.py') diff --git a/mesonbuild/modules/python.py b/mesonbuild/modules/python.py index 3ac4c87e0..cec98948c 100644 --- a/mesonbuild/modules/python.py +++ b/mesonbuild/modules/python.py @@ -36,7 +36,7 @@ from ..mesonlib import MachineChoice from ..programs import ExternalProgram, NonExistingExternalProgram if T.TYPE_CHECKING: - from typing_extensions import TypedDict + from typing_extensions import TypedDict, NotRequired from . import ModuleState from ..build import Build, SharedModule, Data @@ -59,7 +59,7 @@ if T.TYPE_CHECKING: class ExtensionModuleKw(SharedModuleKw): - pass + subdir: NotRequired[T.Optional[str]] mod_kwargs = {'subdir'} @@ -111,7 +111,7 @@ class PythonExternalProgram(BasicPythonExternalProgram): _PURE_KW = KwargInfo('pure', (bool, NoneType)) _SUBDIR_KW = KwargInfo('subdir', str, default='') - +_DEFAULTABLE_SUBDIR_KW = KwargInfo('subdir', (str, NoneType)) class PythonInstallation(ExternalProgramHolder): def __init__(self, python: 'PythonExternalProgram', interpreter: 'Interpreter'): @@ -144,15 +144,16 @@ class PythonInstallation(ExternalProgramHolder): }) @permittedKwargs(mod_kwargs) - @typed_kwargs('python.extension_module', *_MOD_KWARGS, allow_unknown=True) + @typed_kwargs('python.extension_module', *_MOD_KWARGS, _DEFAULTABLE_SUBDIR_KW, allow_unknown=True) def extension_module_method(self, args: T.List['TYPE_var'], kwargs: ExtensionModuleKw) -> 'SharedModule': if 'install_dir' in kwargs: - if 'subdir' in kwargs: + if kwargs['subdir'] is not None: raise InvalidArguments('"subdir" and "install_dir" are mutually exclusive') else: - subdir = kwargs.pop('subdir', '') - if not isinstance(subdir, str): - raise InvalidArguments('"subdir" argument must be a string.') + # We want to remove 'subdir', but it may be None and we want to replace it with '' + # It must be done this way since we don't allow both `install_dir` + # and `subdir` to be set at the same time + subdir = kwargs.pop('subdir') or '' kwargs['install_dir'] = self._get_install_dir_impl(False, subdir) -- cgit v1.2.3