diff --git a/mesonbuild/envconfig.py b/mesonbuild/envconfig.py index 86bad9be23ee..112bfc2ace9a 100644 --- a/mesonbuild/envconfig.py +++ b/mesonbuild/envconfig.py @@ -13,6 +13,9 @@ from . import mlog from pathlib import Path +if T.TYPE_CHECKING: + from .options import ElementaryOptionValues + # These classes contains all the data pulled from configuration files (native # and cross file currently), and also assists with the reading environment @@ -152,7 +155,7 @@ class CMakeSkipCompilerTest(Enum): class Properties: def __init__( self, - properties: T.Optional[T.Dict[str, T.Optional[T.Union[str, bool, int, T.List[str]]]]] = None, + properties: T.Optional[T.Dict[str, ElementaryOptionValues]] = None, ): self.properties = properties or {} @@ -269,7 +272,9 @@ def __repr__(self) -> str: return f'' @classmethod - def from_literal(cls, literal: T.Dict[str, str]) -> 'MachineInfo': + def from_literal(cls, raw: T.Dict[str, ElementaryOptionValues]) -> 'MachineInfo': + assert all(isinstance(v, str) for v in raw.values()), 'for mypy' + literal = T.cast('T.Dict[str, str]', raw) minimum_literal = {'cpu', 'cpu_family', 'endian', 'system'} if set(literal) < minimum_literal: raise EnvironmentException( @@ -388,7 +393,7 @@ class BinaryTable: def __init__( self, - binaries: T.Optional[T.Dict[str, T.Union[str, T.List[str]]]] = None, + binaries: T.Optional[T.Mapping[str, ElementaryOptionValues]] = None, ): self.binaries: T.Dict[str, T.List[str]] = {} if binaries: diff --git a/mesonbuild/environment.py b/mesonbuild/environment.py index 3feca193bc67..18b03393882e 100644 --- a/mesonbuild/environment.py +++ b/mesonbuild/environment.py @@ -42,9 +42,8 @@ from mesonbuild import envconfig if T.TYPE_CHECKING: - from configparser import ConfigParser - from .compilers import Compiler + from .options import ElementaryOptionValues from .wrap.wrap import Resolver CompilersDict = T.Dict[str, Compiler] @@ -698,7 +697,8 @@ def __init__(self, source_dir: str, build_dir: str, cmd_options: coredata.Shared self.default_pkgconfig = ['pkg-config'] self.wrap_resolver: T.Optional['Resolver'] = None - def _load_machine_file_options(self, config: 'ConfigParser', properties: Properties, machine: MachineChoice) -> None: + def _load_machine_file_options(self, config: T.Mapping[str, T.Mapping[str, ElementaryOptionValues]], + properties: Properties, machine: MachineChoice) -> None: """Read the contents of a Machine file and put it in the options store.""" # Look for any options in the deprecated paths section, warn about @@ -708,6 +708,7 @@ def _load_machine_file_options(self, config: 'ConfigParser', properties: Propert if paths: mlog.deprecation('The [paths] section is deprecated, use the [built-in options] section instead.') for k, v in paths.items(): + assert isinstance(v, (str, list)), 'for mypy' self.options[OptionKey.from_string(k).evolve(machine=machine)] = v # Next look for compiler options in the "properties" section, this is @@ -720,6 +721,7 @@ def _load_machine_file_options(self, config: 'ConfigParser', properties: Propert for k, v in properties.properties.copy().items(): if k in deprecated_properties: mlog.deprecation(f'{k} in the [properties] section of the machine file is deprecated, use the [built-in options] section.') + assert isinstance(v, (str, list)), 'for mypy' self.options[OptionKey.from_string(k).evolve(machine=machine)] = v del properties.properties[k] @@ -736,6 +738,7 @@ def _load_machine_file_options(self, config: 'ConfigParser', properties: Propert mlog.deprecation('Setting build machine options in cross files, please use a native file instead, this will be removed in meson 2.0', once=True) if key.subproject: raise MesonException('Do not set subproject options in [built-in options] section, use [subproject:built-in options] instead.') + assert isinstance(v, (str, list)), 'for mypy' self.options[key.evolve(subproject=subproject, machine=machine)] = v elif section == 'project options' and machine is MachineChoice.HOST: # Project options are only for the host machine, we don't want @@ -745,6 +748,7 @@ def _load_machine_file_options(self, config: 'ConfigParser', properties: Propert key = OptionKey.from_string(k) if key.subproject: raise MesonException('Do not set subproject options in [built-in options] section, use [subproject:built-in options] instead.') + assert isinstance(v, (str, list)), 'for mypy' self.options[key.evolve(subproject=subproject)] = v def _set_default_options_from_env(self) -> None: diff --git a/run_mypy.py b/run_mypy.py index db09e9be6280..af4c7f82cc0c 100755 --- a/run_mypy.py +++ b/run_mypy.py @@ -36,6 +36,7 @@ 'mesonbuild/coredata.py', 'mesonbuild/depfile.py', 'mesonbuild/envconfig.py', + 'mesonbuild/environment.py', 'mesonbuild/interpreter/compiler.py', 'mesonbuild/interpreter/mesonmain.py', 'mesonbuild/interpreter/interpreterobjects.py',