Skip to content

Commit

Permalink
environment: make fully type safe
Browse files Browse the repository at this point in the history
This as much as anything is to stop lying to envconfig about the
potential types it will be given.
  • Loading branch information
dcbaker committed Oct 1, 2024
1 parent 529954c commit 5310b76
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
11 changes: 8 additions & 3 deletions mesonbuild/envconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {}

Expand Down Expand Up @@ -269,7 +272,9 @@ def __repr__(self) -> str:
return f'<MachineInfo: {self.system} {self.cpu_family} ({self.cpu})>'

@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(
Expand Down Expand Up @@ -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:
Expand Down
10 changes: 7 additions & 3 deletions mesonbuild/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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]

Expand All @@ -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
Expand All @@ -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:
Expand Down
1 change: 1 addition & 0 deletions run_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 5310b76

Please sign in to comment.