Skip to content

Commit f0795e1

Browse files
dcbakerjpakkane
authored andcommitted
environment: make fully type safe
This as much as anything is to stop lying to envconfig about the potential types it will be given.
1 parent dfa1185 commit f0795e1

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

mesonbuild/compilers/compilers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,6 +1424,8 @@ def get_global_options(lang: str,
14241424

14251425
comp_options = env.options.get(comp_key, [])
14261426
link_options = env.options.get(largkey, [])
1427+
assert isinstance(comp_options, (str, list)), 'for mypy'
1428+
assert isinstance(link_options, (str, list)), 'for mypy'
14271429

14281430
cargs = options.UserStringArrayOption(
14291431
argkey.name,

mesonbuild/envconfig.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
from . import mlog
1414
from pathlib import Path
1515

16+
if T.TYPE_CHECKING:
17+
from .options import ElementaryOptionValues
18+
1619

1720
# These classes contains all the data pulled from configuration files (native
1821
# and cross file currently), and also assists with the reading environment
@@ -153,7 +156,7 @@ class CMakeSkipCompilerTest(Enum):
153156
class Properties:
154157
def __init__(
155158
self,
156-
properties: T.Optional[T.Dict[str, T.Optional[T.Union[str, bool, int, T.List[str]]]]] = None,
159+
properties: T.Optional[T.Dict[str, ElementaryOptionValues]] = None,
157160
):
158161
self.properties = properties or {}
159162

@@ -270,7 +273,13 @@ def __repr__(self) -> str:
270273
return f'<MachineInfo: {self.system} {self.cpu_family} ({self.cpu})>'
271274

272275
@classmethod
273-
def from_literal(cls, literal: T.Dict[str, str]) -> 'MachineInfo':
276+
def from_literal(cls, raw: T.Dict[str, ElementaryOptionValues]) -> 'MachineInfo':
277+
# We don't have enough type information to be sure of what we loaded
278+
# So we need to accept that this might have ElementaryOptionValues, but
279+
# then ensure that it's actually strings, since that's what the
280+
# [*_machine] section should have.
281+
assert all(isinstance(v, str) for v in raw.values()), 'for mypy'
282+
literal = T.cast('T.Dict[str, str]', raw)
274283
minimum_literal = {'cpu', 'cpu_family', 'endian', 'system'}
275284
if set(literal) < minimum_literal:
276285
raise EnvironmentException(
@@ -389,7 +398,7 @@ class BinaryTable:
389398

390399
def __init__(
391400
self,
392-
binaries: T.Optional[T.Dict[str, T.Union[str, T.List[str]]]] = None,
401+
binaries: T.Optional[T.Mapping[str, ElementaryOptionValues]] = None,
393402
):
394403
self.binaries: T.Dict[str, T.List[str]] = {}
395404
if binaries:

mesonbuild/environment.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@
4141
from mesonbuild import envconfig
4242

4343
if T.TYPE_CHECKING:
44-
from configparser import ConfigParser
45-
4644
from .compilers import Compiler
4745
from .compilers.mixins.visualstudio import VisualStudioLikeCompiler
46+
from .options import ElementaryOptionValues
4847
from .wrap.wrap import Resolver
4948
from . import cargo
5049

@@ -633,7 +632,7 @@ def __init__(self, source_dir: str, build_dir: str, cmd_options: coredata.Shared
633632
#
634633
# Note that order matters because of 'buildtype', if it is after
635634
# 'optimization' and 'debug' keys, it override them.
636-
self.options: T.MutableMapping[OptionKey, T.Union[str, T.List[str]]] = collections.OrderedDict()
635+
self.options: T.MutableMapping[OptionKey, ElementaryOptionValues] = collections.OrderedDict()
637636

638637
self.machinestore = machinefile.MachineFileStore(self.coredata.config_files, self.coredata.cross_files, self.source_dir)
639638

@@ -701,7 +700,7 @@ def __init__(self, source_dir: str, build_dir: str, cmd_options: coredata.Shared
701700
# Store a global state of Cargo dependencies
702701
self.cargo: T.Optional[cargo.Interpreter] = None
703702

704-
def mfilestr2key(self, machine_file_string: str, section_subproject: str, machine: MachineChoice):
703+
def mfilestr2key(self, machine_file_string: str, section_subproject: str, machine: MachineChoice) -> OptionKey:
705704
key = OptionKey.from_string(machine_file_string)
706705
assert key.machine == MachineChoice.HOST
707706
if key.subproject:
@@ -712,7 +711,8 @@ def mfilestr2key(self, machine_file_string: str, section_subproject: str, machin
712711
return key.evolve(machine=machine)
713712
return key
714713

715-
def _load_machine_file_options(self, config: 'ConfigParser', properties: Properties, machine: MachineChoice) -> None:
714+
def _load_machine_file_options(self, config: T.Mapping[str, T.Mapping[str, ElementaryOptionValues]],
715+
properties: Properties, machine: MachineChoice) -> None:
716716
"""Read the contents of a Machine file and put it in the options store."""
717717

718718
# Look for any options in the deprecated paths section, warn about

run_mypy.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
# 'mesonbuild/coredata.py',
3737
'mesonbuild/depfile.py',
3838
'mesonbuild/envconfig.py',
39+
'mesonbuild/environment.py',
3940
'mesonbuild/interpreter/compiler.py',
4041
'mesonbuild/interpreter/mesonmain.py',
4142
'mesonbuild/interpreter/interpreterobjects.py',

0 commit comments

Comments
 (0)