summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDylan Baker <dylan@pnwbakers.com>2025-01-29 11:40:27 -0800
committerDylan Baker <dylan@pnwbakers.com>2025-02-12 10:35:35 -0800
commit5e7b1a9d1decdb8773656ce187bbdb3c39987689 (patch)
tree4909245906e19e8d3027c442677e1da38148b527
parentab9c79bd1cda545c3ce18b0c62b820f6d85221d1 (diff)
downloadmeson-5e7b1a9d1decdb8773656ce187bbdb3c39987689.tar.gz
options: Add a TypeAlias for option values
This gives us a simpler way to annotate things returning an option value, and gives us an easy single place to change that will affect everywhere if the option value types are changed.
-rw-r--r--mesonbuild/coredata.py10
-rw-r--r--mesonbuild/interpreter/interpreter.py8
-rw-r--r--mesonbuild/interpreter/kwargs.py14
-rw-r--r--mesonbuild/interpreter/type_checking.py9
-rw-r--r--mesonbuild/options.py1
5 files changed, 22 insertions, 20 deletions
diff --git a/mesonbuild/coredata.py b/mesonbuild/coredata.py
index c2a7c140a..fcf93a7e6 100644
--- a/mesonbuild/coredata.py
+++ b/mesonbuild/coredata.py
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2013-2024 The Meson development team
-# Copyright © 2023-2024 Intel Corporation
+# Copyright © 2023-2025 Intel Corporation
from __future__ import annotations
@@ -43,7 +43,7 @@ if T.TYPE_CHECKING:
from .mesonlib import FileOrString
from .cmake.traceparser import CMakeCacheEntry
from .interpreterbase import SubProject
- from .options import UserOption
+ from .options import UserOption, ElementaryOptionValues
class SharedCMDOptions(Protocol):
@@ -442,7 +442,7 @@ class CoreData:
'Default project to execute in Visual Studio',
''))
- def get_option(self, key: OptionKey) -> T.Union[T.List[str], str, int, bool]:
+ def get_option(self, key: OptionKey) -> ElementaryOptionValues:
try:
v = self.optstore.get_value(key)
return v
@@ -916,7 +916,7 @@ class OptionsView(abc.Mapping):
# python 3.8 or typing_extensions
original_options: T.Union[KeyedOptionDictType, 'dict[OptionKey, UserOption[Any]]']
subproject: T.Optional[str] = None
- overrides: T.Optional[T.Mapping[OptionKey, T.Union[str, int, bool, T.List[str]]]] = dataclasses.field(default_factory=dict)
+ overrides: T.Optional[T.Mapping[OptionKey, ElementaryOptionValues]] = dataclasses.field(default_factory=dict)
def __getitem__(self, key: OptionKey) -> options.UserOption:
# FIXME: This is fundamentally the same algorithm than interpreter.get_option_internal().
@@ -960,7 +960,7 @@ class OptionsView(abc.Mapping):
key = OptionKey(key)
return self[key].value
- def set_value(self, key: T.Union[str, OptionKey], value: T.Union[str, int, bool, T.List[str]]):
+ def set_value(self, key: T.Union[str, OptionKey], value: ElementaryOptionValues):
if isinstance(key, str):
key = OptionKey(key)
self.overrides[key] = value
diff --git a/mesonbuild/interpreter/interpreter.py b/mesonbuild/interpreter/interpreter.py
index db8ad8057..f6e1bfa01 100644
--- a/mesonbuild/interpreter/interpreter.py
+++ b/mesonbuild/interpreter/interpreter.py
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2012-2021 The Meson development team
-# Copyright © 2023-2024 Intel Corporation
+# Copyright © 2023-2025 Intel Corporation
from __future__ import annotations
@@ -1668,7 +1668,7 @@ class Interpreter(InterpreterBase, HoldableObject):
# the host machine.
def find_program_impl(self, args: T.List[mesonlib.FileOrString],
for_machine: MachineChoice = MachineChoice.HOST,
- default_options: T.Optional[T.Dict[OptionKey, T.Union[str, int, bool, T.List[str]]]] = None,
+ default_options: T.Optional[T.Dict[OptionKey, options.ElementaryOptionValues]] = None,
required: bool = True, silent: bool = True,
wanted: T.Union[str, T.List[str]] = '',
search_dirs: T.Optional[T.List[str]] = None,
@@ -1699,7 +1699,7 @@ class Interpreter(InterpreterBase, HoldableObject):
return progobj
def program_lookup(self, args: T.List[mesonlib.FileOrString], for_machine: MachineChoice,
- default_options: T.Optional[T.Dict[OptionKey, T.Union[str, int, bool, T.List[str]]]],
+ default_options: T.Optional[T.Dict[OptionKey, options.ElementaryOptionValues]],
required: bool,
search_dirs: T.Optional[T.List[str]],
wanted: T.Union[str, T.List[str]],
@@ -1767,7 +1767,7 @@ class Interpreter(InterpreterBase, HoldableObject):
return True
def find_program_fallback(self, fallback: str, args: T.List[mesonlib.FileOrString],
- default_options: T.Dict[OptionKey, T.Union[str, int, bool, T.List[str]]],
+ default_options: T.Dict[OptionKey, options.ElementaryOptionValues],
required: bool, extra_info: T.List[mlog.TV_Loggable]
) -> T.Optional[T.Union[ExternalProgram, build.Executable, OverrideProgram]]:
mlog.log('Fallback to subproject', mlog.bold(fallback), 'which provides program',
diff --git a/mesonbuild/interpreter/kwargs.py b/mesonbuild/interpreter/kwargs.py
index 87f121e90..20cafcdf0 100644
--- a/mesonbuild/interpreter/kwargs.py
+++ b/mesonbuild/interpreter/kwargs.py
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
-# Copyright © 2021 The Meson Developers
-# Copyright © 2021-2024 Intel Corporation
+# Copyright © 2021-2025 Intel Corporation
+# Copyright © 2021-2025 Intel Corporation
from __future__ import annotations
"""Keyword Argument type annotations."""
@@ -209,7 +209,7 @@ class Project(TypedDict):
version: T.Optional[FileOrString]
meson_version: T.Optional[str]
- default_options: T.Dict[OptionKey, T.Union[str, int, bool, T.List[str]]]
+ default_options: T.Dict[OptionKey, options.ElementaryOptionValues]
license: T.List[str]
license_files: T.List[str]
subproject_dir: str
@@ -239,7 +239,7 @@ class Summary(TypedDict):
class FindProgram(ExtractRequired, ExtractSearchDirs):
- default_options: T.Dict[OptionKey, T.Union[str, int, bool, T.List[str]]]
+ default_options: T.Dict[OptionKey, options.ElementaryOptionValues]
native: MachineChoice
version: T.List[str]
@@ -312,13 +312,13 @@ class ConfigureFile(TypedDict):
class Subproject(ExtractRequired):
- default_options: T.Dict[OptionKey, T.Union[str, int, bool, T.List[str]]]
+ default_options: T.Dict[OptionKey, options.ElementaryOptionValues]
version: T.List[str]
class DoSubproject(ExtractRequired):
- default_options: T.Dict[OptionKey, T.Union[str, int, bool, T.List[str]]]
+ default_options: T.Dict[OptionKey, options.ElementaryOptionValues]
version: T.List[str]
cmake_options: T.List[str]
options: T.Optional[CMakeSubprojectOptions]
@@ -346,7 +346,7 @@ class _BaseBuildTarget(TypedDict):
name_suffix: T.Optional[str]
native: MachineChoice
objects: T.List[build.ObjectTypes]
- override_options: T.Dict[OptionKey, T.Union[str, int, bool, T.List[str]]]
+ override_options: T.Dict[OptionKey, options.ElementaryOptionValues]
depend_files: NotRequired[T.List[File]]
resources: T.List[str]
diff --git a/mesonbuild/interpreter/type_checking.py b/mesonbuild/interpreter/type_checking.py
index ed34be950..08046411c 100644
--- a/mesonbuild/interpreter/type_checking.py
+++ b/mesonbuild/interpreter/type_checking.py
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: Apache-2.0
-# Copyright © 2021 Intel Corporation
+# Copyright © 2021-2025 Intel Corporation
"""Helpers for strict type checking."""
@@ -27,6 +27,7 @@ if T.TYPE_CHECKING:
from ..build import ObjectTypes
from ..interpreterbase import TYPE_var
+ from ..options import ElementaryOptionValues
from ..mesonlib import EnvInitValueType
_FullEnvInitValueType = T.Union[EnvironmentVariables, T.List[str], T.List[T.List[str]], EnvInitValueType, str, None]
@@ -292,11 +293,11 @@ COMMAND_KW: KwargInfo[T.List[T.Union[str, BuildTarget, CustomTarget, CustomTarge
default=[],
)
-def _override_options_convertor(raw: T.Union[str, T.List[str], T.Dict[str, T.Union[str, int, bool, T.List[str]]]]) -> T.Dict[OptionKey, T.Union[str, int, bool, T.List[str]]]:
+def _override_options_convertor(raw: T.Union[str, T.List[str], T.Dict[str, ElementaryOptionValues]]) -> T.Dict[OptionKey, ElementaryOptionValues]:
if isinstance(raw, str):
raw = [raw]
if isinstance(raw, list):
- output: T.Dict[OptionKey, T.Union[str, int, bool, T.List[str]]] = {}
+ output: T.Dict[OptionKey, ElementaryOptionValues] = {}
for each in raw:
k, v = split_equal_string(each)
output[OptionKey.from_string(k)] = v
@@ -304,7 +305,7 @@ def _override_options_convertor(raw: T.Union[str, T.List[str], T.Dict[str, T.Uni
return {OptionKey.from_string(k): v for k, v in raw.items()}
-OVERRIDE_OPTIONS_KW: KwargInfo[T.Union[str, T.Dict[str, T.Union[str, int, bool, T.List[str]]], T.List[str]]] = KwargInfo(
+OVERRIDE_OPTIONS_KW: KwargInfo[T.Union[str, T.Dict[str, ElementaryOptionValues], T.List[str]]] = KwargInfo(
'override_options',
(str, ContainerTypeInfo(list, str), ContainerTypeInfo(dict, (str, int, bool, list))),
default={},
diff --git a/mesonbuild/options.py b/mesonbuild/options.py
index 7aa9a5efa..c31254d23 100644
--- a/mesonbuild/options.py
+++ b/mesonbuild/options.py
@@ -36,6 +36,7 @@ if T.TYPE_CHECKING:
'UserBooleanOption', 'UserComboOption', 'UserFeatureOption',
'UserIntegerOption', 'UserStdOption', 'UserStringArrayOption',
'UserStringOption', 'UserUmaskOption']
+ ElementaryOptionValues: TypeAlias = T.Union[str, int, bool, T.List[str]]
class ArgparseKWs(TypedDict, total=False):