diff options
author | Daniel Mensinger <daniel@mensinger-ka.de> | 2019-12-07 14:42:23 +0100 |
---|---|---|
committer | Daniel Mensinger <daniel@mensinger-ka.de> | 2020-03-02 10:34:55 +0100 |
commit | c14aea2812fd2be94998bdb174e9a4681aeea394 (patch) | |
tree | 7f8532528ea3d84db5491f5d766415649592bcd1 /mesonbuild/interpreterbase.py | |
parent | 200738a3e6b48671aac2865c304dded96179e4ac (diff) | |
download | meson-c14aea2812fd2be94998bdb174e9a4681aeea394.zip meson-c14aea2812fd2be94998bdb174e9a4681aeea394.tar.gz meson-c14aea2812fd2be94998bdb174e9a4681aeea394.tar.bz2 |
types: Annotate mparser.py
This also fixes that the keys in ArgumentNode.kwargs are
all of the type BaseNode now. Before this commit, it was
possible that both strings and Nodes where used as keys.
Diffstat (limited to 'mesonbuild/interpreterbase.py')
-rw-r--r-- | mesonbuild/interpreterbase.py | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/mesonbuild/interpreterbase.py b/mesonbuild/interpreterbase.py index d723da5..152c65b 100644 --- a/mesonbuild/interpreterbase.py +++ b/mesonbuild/interpreterbase.py @@ -20,6 +20,7 @@ from . import environment, dependencies import os, copy, re from functools import wraps +from typing import Union, Optional class ObjectHolder: def __init__(self, obj, subproject=None): @@ -494,7 +495,7 @@ class InterpreterBase: @FeatureNew('dict', '0.47.0') def evaluate_dictstatement(self, cur): - (arguments, kwargs) = self.reduce_arguments(cur.args) + (arguments, kwargs) = self.reduce_arguments(cur.args, resolve_key_nodes=False) assert (not arguments) result = {} self.argument_depth += 1 @@ -693,7 +694,7 @@ The result of this is undefined and will become a hard error in a future Meson r if isinstance(items, list): if len(node.varnames) != 1: raise InvalidArguments('Foreach on array does not unpack') - varname = node.varnames[0].value + varname = node.varnames[0] for item in items: self.set_variable(varname, item) try: @@ -706,8 +707,8 @@ The result of this is undefined and will become a hard error in a future Meson r if len(node.varnames) != 2: raise InvalidArguments('Foreach on dict unpacks key and value') for key, value in items.items(): - self.set_variable(node.varnames[0].value, key) - self.set_variable(node.varnames[1].value, value) + self.set_variable(node.varnames[0], key) + self.set_variable(node.varnames[1], value) try: self.evaluate_codeblock(node.block) except ContinueRequest: @@ -1025,7 +1026,7 @@ The result of this is undefined and will become a hard error in a future Meson r raise InterpreterException('Dictionaries do not have a method called "%s".' % method_name) - def reduce_arguments(self, args): + def reduce_arguments(self, args: mparser.ArgumentNode, resolve_key_nodes: Optional[bool] = True): assert(isinstance(args, mparser.ArgumentNode)) if args.incorrect_order(): raise InvalidArguments('All keyword arguments must be after positional arguments.') @@ -1033,8 +1034,12 @@ The result of this is undefined and will become a hard error in a future Meson r reduced_pos = [self.evaluate_statement(arg) for arg in args.arguments] reduced_kw = {} for key in args.kwargs.keys(): + reduced_key = key # type: Union[str, mparser.BaseNode] + if resolve_key_nodes and isinstance(key, mparser.IdNode): + assert isinstance(key.value, str) + reduced_key = key.value a = args.kwargs[key] - reduced_kw[key] = self.evaluate_statement(a) + reduced_kw[reduced_key] = self.evaluate_statement(a) self.argument_depth -= 1 final_kw = self.expand_default_kwargs(reduced_kw) return reduced_pos, final_kw |