Skip to content

Commit dfa1185

Browse files
dcbakerjpakkane
authored andcommitted
machinefile: ensure that arrays are single deep arrays for strings
We don't actually allow anything except elementry types (`str`, `bool`, `int`, `array[str]`) here, so we can tighten the typing. This in turn helps us to simplify the typing in environment.py
1 parent fe43247 commit dfa1185

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

mesonbuild/machinefile.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,8 @@
1212
from .mesonlib import MesonException
1313

1414
if T.TYPE_CHECKING:
15-
from typing_extensions import TypeAlias
16-
1715
from .coredata import StrOrBytesPath
18-
19-
SectionT: TypeAlias = T.Union[str, int, bool, T.List[str], T.List['SectionT']]
20-
16+
from .options import ElementaryOptionValues
2117

2218
class CmdLineFileParser(configparser.ConfigParser):
2319
def __init__(self) -> None:
@@ -36,8 +32,8 @@ def optionxform(self, optionstr: str) -> str:
3632
class MachineFileParser():
3733
def __init__(self, filenames: T.List[str], sourcedir: str) -> None:
3834
self.parser = CmdLineFileParser()
39-
self.constants: T.Dict[str, SectionT] = {'True': True, 'False': False}
40-
self.sections: T.Dict[str, T.Dict[str, SectionT]] = {}
35+
self.constants: T.Dict[str, ElementaryOptionValues] = {'True': True, 'False': False}
36+
self.sections: T.Dict[str, T.Dict[str, ElementaryOptionValues]] = {}
4137

4238
for fname in filenames:
4339
try:
@@ -62,9 +58,9 @@ def __init__(self, filenames: T.List[str], sourcedir: str) -> None:
6258
continue
6359
self.sections[s] = self._parse_section(s)
6460

65-
def _parse_section(self, s: str) -> T.Dict[str, SectionT]:
61+
def _parse_section(self, s: str) -> T.Dict[str, ElementaryOptionValues]:
6662
self.scope = self.constants.copy()
67-
section: T.Dict[str, SectionT] = {}
63+
section: T.Dict[str, ElementaryOptionValues] = {}
6864
for entry, value in self.parser.items(s):
6965
if ' ' in entry or '\t' in entry or "'" in entry or '"' in entry:
7066
raise MesonException(f'Malformed variable name {entry!r} in machine file.')
@@ -83,7 +79,7 @@ def _parse_section(self, s: str) -> T.Dict[str, SectionT]:
8379
self.scope[entry] = res
8480
return section
8581

86-
def _evaluate_statement(self, node: mparser.BaseNode) -> SectionT:
82+
def _evaluate_statement(self, node: mparser.BaseNode) -> ElementaryOptionValues:
8783
if isinstance(node, (mparser.StringNode)):
8884
return node.value
8985
elif isinstance(node, mparser.BooleanNode):
@@ -93,7 +89,9 @@ def _evaluate_statement(self, node: mparser.BaseNode) -> SectionT:
9389
elif isinstance(node, mparser.ParenthesizedNode):
9490
return self._evaluate_statement(node.inner)
9591
elif isinstance(node, mparser.ArrayNode):
96-
return [self._evaluate_statement(arg) for arg in node.args.arguments]
92+
a = [self._evaluate_statement(arg) for arg in node.args.arguments]
93+
assert all(isinstance(s, str) for s in a), 'for mypy'
94+
return T.cast('T.List[str]', a)
9795
elif isinstance(node, mparser.IdNode):
9896
return self.scope[node.value]
9997
elif isinstance(node, mparser.ArithmeticNode):
@@ -109,7 +107,7 @@ def _evaluate_statement(self, node: mparser.BaseNode) -> SectionT:
109107
return os.path.join(l, r)
110108
raise MesonException('Unsupported node type')
111109

112-
def parse_machine_files(filenames: T.List[str], sourcedir: str) -> T.Dict[str, T.Dict[str, SectionT]]:
110+
def parse_machine_files(filenames: T.List[str], sourcedir: str) -> T.Dict[str, T.Dict[str, ElementaryOptionValues]]:
113111
parser = MachineFileParser(filenames, sourcedir)
114112
return parser.sections
115113

0 commit comments

Comments
 (0)