diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py index e744b45fef72..08d50624f918 100644 --- a/mesonbuild/compilers/compilers.py +++ b/mesonbuild/compilers/compilers.py @@ -35,7 +35,6 @@ from ..dependencies import Dependency CompilerType = T.TypeVar('CompilerType', bound='Compiler') - UserOptionType = T.TypeVar('UserOptionType', bound=options.UserOption) _T = T.TypeVar('_T') @@ -590,7 +589,7 @@ def make_option_name(self, key: OptionKey) -> str: return f'{self.language}_{key.name}' @staticmethod - def update_options(options: MutableKeyedOptionDictType, *args: T.Tuple[OptionKey, UserOptionType]) -> MutableKeyedOptionDictType: + def update_options(options: MutableKeyedOptionDictType, *args: T.Tuple[OptionKey, options.AnyOptionType]) -> MutableKeyedOptionDictType: options.update(args) return options diff --git a/mesonbuild/coredata.py b/mesonbuild/coredata.py index 746fe3ba29da..a5b599221815 100644 --- a/mesonbuild/coredata.py +++ b/mesonbuild/coredata.py @@ -60,8 +60,8 @@ class SharedCMDOptions(Protocol): cross_file: T.List[str] native_file: T.List[str] - OptionDictType = T.Union[T.Dict[str, 'options.UserOption[T.Any]'], 'OptionsView'] - MutableKeyedOptionDictType = T.Dict['OptionKey', 'options.UserOption[T.Any]'] + OptionDictType = T.Union[T.Dict[str, options.AnyOptionType], 'OptionsView'] + MutableKeyedOptionDictType = T.Dict['OptionKey', options.AnyOptionType] KeyedOptionDictType = T.Union['options.OptionStore', 'OptionsView'] CompilerCheckCacheKey = T.Tuple[T.Tuple[str, ...], str, FileOrString, T.Tuple[str, ...], CompileCheckMode] # code, args diff --git a/mesonbuild/mconf.py b/mesonbuild/mconf.py index cd8cd6c4aa82..f57ec4615c25 100644 --- a/mesonbuild/mconf.py +++ b/mesonbuild/mconf.py @@ -26,8 +26,6 @@ if T.TYPE_CHECKING: from typing_extensions import Protocol - from typing import Any - from .options import UserOption import argparse class CMDOptions(coredata.SharedCMDOptions, Protocol): @@ -189,7 +187,7 @@ def wrap_text(text: LOGLINE, width: int) -> mlog.TV_LoggableList: items = [l[i] if l[i] else ' ' * four_column[i] for i in range(4)] mlog.log(*items) - def split_options_per_subproject(self, options: 'T.Union[dict[OptionKey, UserOption[Any]], coredata.KeyedOptionDictType]') -> T.Dict[str, 'coredata.MutableKeyedOptionDictType']: + def split_options_per_subproject(self, options: T.Union[coredata.MutableKeyedOptionDictType, coredata.KeyedOptionDictType]) -> T.Dict[str, 'coredata.MutableKeyedOptionDictType']: result: T.Dict[str, 'coredata.MutableKeyedOptionDictType'] = {} for k, o in options.items(): if k.subproject: @@ -227,7 +225,7 @@ def add_section(self, section: str) -> None: self._add_line(mlog.normal_yellow(section + ':'), '', '', '') self.print_margin = 2 - def print_options(self, title: str, opts: 'T.Union[dict[OptionKey, UserOption[Any]], coredata.KeyedOptionDictType]') -> None: + def print_options(self, title: str, opts: T.Union[coredata.MutableKeyedOptionDictType, coredata.KeyedOptionDictType]) -> None: if not opts: return if title: diff --git a/mesonbuild/mintro.py b/mesonbuild/mintro.py index 4afc15830e6d..0d319c1f9aea 100644 --- a/mesonbuild/mintro.py +++ b/mesonbuild/mintro.py @@ -30,8 +30,6 @@ if T.TYPE_CHECKING: import argparse - from typing import Any - from .options import UserOption from .interpreter import Interpreter from .mparser import BaseNode @@ -305,7 +303,7 @@ def list_buildoptions(coredata: cdata.CoreData, subprojects: T.Optional[T.List[s for s in subprojects: core_options[k.evolve(subproject=s)] = v - def add_keys(opts: 'T.Union[dict[OptionKey, UserOption[Any]], cdata.KeyedOptionDictType]', section: str) -> None: + def add_keys(opts: T.Union[cdata.MutableKeyedOptionDictType, cdata.KeyedOptionDictType], section: str) -> None: for key, opt in sorted(opts.items()): optdict = {'name': str(key), 'value': opt.value, 'section': section, 'machine': key.machine.get_lower_case_name() if coredata.is_per_machine_option(key) else 'any'} diff --git a/mesonbuild/optinterpreter.py b/mesonbuild/optinterpreter.py index 2b36b612174c..034e7b483e0b 100644 --- a/mesonbuild/optinterpreter.py +++ b/mesonbuild/optinterpreter.py @@ -69,7 +69,7 @@ class OptionInterpreter: def __init__(self, optionstore: 'OptionStore', subproject: 'SubProject') -> None: self.options: 'coredata.MutableKeyedOptionDictType' = {} self.subproject = subproject - self.option_types: T.Dict[str, T.Callable[..., options.UserOption]] = { + self.option_types: T.Dict[str, T.Callable[..., options.AnyOptionType]] = { 'string': self.string_parser, 'boolean': self.boolean_parser, 'combo': self.combo_parser, diff --git a/mesonbuild/options.py b/mesonbuild/options.py index 6bebcfb82d98..8088f8bdd443 100644 --- a/mesonbuild/options.py +++ b/mesonbuild/options.py @@ -32,6 +32,10 @@ from typing_extensions import Literal, TypeAlias, TypedDict DeprecatedType: TypeAlias = T.Union[bool, str, T.Dict[str, str], T.List[str]] + AnyOptionType: TypeAlias = T.Union[ + 'UserBooleanOption', 'UserComboOption', 'UserFeatureOption', + 'UserIntegerOption', 'UserStdOption', 'UserStringArrayOption', + 'UserStringOption', 'UserUmaskOption'] class ArgparseKWs(TypedDict, total=False): @@ -734,7 +738,7 @@ def add_to_argparse(self, name: OptionKey, parser: argparse.ArgumentParser, help class OptionStore: def __init__(self) -> None: - self.d: T.Dict['OptionKey', 'UserOption[T.Any]'] = {} + self.d: T.Dict['OptionKey', AnyOptionType] = {} self.project_options: T.Set[OptionKey] = set() self.module_options: T.Set[OptionKey] = set() from .compilers import all_languages @@ -748,35 +752,35 @@ def ensure_key(self, key: T.Union[OptionKey, str]) -> OptionKey: return OptionKey(key) return key - def get_value_object(self, key: T.Union[OptionKey, str]) -> 'UserOption[T.Any]': + def get_value_object(self, key: T.Union[OptionKey, str]) -> AnyOptionType: return self.d[self.ensure_key(key)] def get_value(self, key: T.Union[OptionKey, str]) -> 'T.Any': return self.get_value_object(key).value - def add_system_option(self, key: T.Union[OptionKey, str], valobj: 'UserOption[T.Any]') -> None: + def add_system_option(self, key: T.Union[OptionKey, str], valobj: AnyOptionType) -> None: key = self.ensure_key(key) if '.' in key.name: raise MesonException(f'Internal error: non-module option has a period in its name {key.name}.') self.add_system_option_internal(key, valobj) - def add_system_option_internal(self, key: T.Union[OptionKey, str], valobj: 'UserOption[T.Any]') -> None: + def add_system_option_internal(self, key: T.Union[OptionKey, str], valobj: AnyOptionType) -> None: key = self.ensure_key(key) assert isinstance(valobj, UserOption) self.d[key] = valobj - def add_compiler_option(self, language: str, key: T.Union[OptionKey, str], valobj: 'UserOption[T.Any]') -> None: + def add_compiler_option(self, language: str, key: T.Union[OptionKey, str], valobj: AnyOptionType) -> None: key = self.ensure_key(key) if not key.name.startswith(language + '_'): raise MesonException(f'Internal error: all compiler option names must start with language prefix. ({key.name} vs {language}_)') self.add_system_option(key, valobj) - def add_project_option(self, key: T.Union[OptionKey, str], valobj: 'UserOption[T.Any]') -> None: + def add_project_option(self, key: T.Union[OptionKey, str], valobj: AnyOptionType) -> None: key = self.ensure_key(key) self.d[key] = valobj self.project_options.add(key) - def add_module_option(self, modulename: str, key: T.Union[OptionKey, str], valobj: 'UserOption[T.Any]') -> None: + def add_module_option(self, modulename: str, key: T.Union[OptionKey, str], valobj: AnyOptionType) -> None: key = self.ensure_key(key) if key.name.startswith('build.'): raise MesonException('FATAL internal error: somebody goofed option handling.') @@ -790,7 +794,7 @@ def set_value(self, key: T.Union[OptionKey, str], new_value: 'T.Any') -> bool: return self.d[key].set_value(new_value) # FIXME, this should be removed.or renamed to "change_type_of_existing_object" or something like that - def set_value_object(self, key: T.Union[OptionKey, str], new_object: 'UserOption[T.Any]') -> None: + def set_value_object(self, key: T.Union[OptionKey, str], new_object: AnyOptionType) -> None: key = self.ensure_key(key) self.d[key] = new_object @@ -807,20 +811,20 @@ def __repr__(self) -> str: def keys(self) -> T.KeysView[OptionKey]: return self.d.keys() - def values(self) -> T.ValuesView[UserOption[T.Any]]: + def values(self) -> T.ValuesView[AnyOptionType]: return self.d.values() - def items(self) -> T.ItemsView['OptionKey', 'UserOption[T.Any]']: + def items(self) -> T.ItemsView['OptionKey', AnyOptionType]: return self.d.items() # FIXME: this method must be deleted and users moved to use "add_xxx_option"s instead. - def update(self, **kwargs: UserOption[T.Any]) -> None: + def update(self, **kwargs: AnyOptionType) -> None: self.d.update(**kwargs) - def setdefault(self, k: OptionKey, o: UserOption[T.Any]) -> UserOption[T.Any]: + def setdefault(self, k: OptionKey, o: AnyOptionType) -> AnyOptionType: return self.d.setdefault(k, o) - def get(self, o: OptionKey, default: T.Optional[UserOption[T.Any]] = None) -> T.Optional[UserOption[T.Any]]: + def get(self, o: OptionKey, default: T.Optional[AnyOptionType] = None) -> T.Optional[AnyOptionType]: return self.d.get(o, default) def is_project_option(self, key: OptionKey) -> bool: