| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2021 The Meson development team |
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import typing as T |
| 6 |
|
| 7 |
from ...interpreterbase import ( |
| 8 |
InterpreterObject, |
| 9 |
IterableObject, |
| 10 |
MesonOperator, |
| 11 |
ObjectHolder, |
| 12 |
FeatureNew, |
| 13 |
typed_operator, |
| 14 |
noKwargs, |
| 15 |
noPosargs, |
| 16 |
noArgsFlattening, |
| 17 |
typed_pos_args, |
| 18 |
|
| 19 |
TYPE_var, |
| 20 |
|
| 21 |
InvalidArguments, |
| 22 |
) |
| 23 |
|
| 24 |
if T.TYPE_CHECKING: |
| 25 |
from ...interpreterbase import TYPE_kwargs |
| 26 |
|
| 27 |
class DictHolder(ObjectHolder[T.Dict[str, TYPE_var]], IterableObject): |
| 28 |
# Operators that only require type checks |
| 29 |
TRIVIAL_OPERATORS = { |
| 30 |
# Arithmetic |
| 31 |
MesonOperator.PLUS: (dict, lambda obj, x: {**obj.held_object, **x}), |
| 32 |
|
| 33 |
# Comparison |
| 34 |
MesonOperator.EQUALS: (dict, lambda obj, x: obj.held_object == x), |
| 35 |
MesonOperator.NOT_EQUALS: (dict, lambda obj, x: obj.held_object != x), |
| 36 |
MesonOperator.IN: (str, lambda obj, x: x in obj.held_object), |
| 37 |
MesonOperator.NOT_IN: (str, lambda obj, x: x not in obj.held_object), |
| 38 |
} |
| 39 |
|
| 40 |
def display_name(self) -> str: |
| 41 |
return 'dict' |
| 42 |
|
| 43 |
def iter_tuple_size(self) -> int: |
| 44 |
return 2 |
| 45 |
|
| 46 |
def iter_self(self) -> T.Iterator[T.Tuple[str, TYPE_var]]: |
| 47 |
return iter(self.held_object.items()) |
| 48 |
|
| 49 |
def size(self) -> int: |
| 50 |
return len(self.held_object) |
| 51 |
|
| 52 |
def _keys_getter(self) -> T.List[str]: |
| 53 |
return sorted(self.held_object) |
| 54 |
|
| 55 |
@noKwargs |
| 56 |
@typed_pos_args('dict.has_key', str) |
| 57 |
@InterpreterObject.method('has_key') |
| 58 |
def has_key_method(self, args: T.Tuple[str], kwargs: TYPE_kwargs) -> bool: |
| 59 |
return args[0] in self.held_object |
| 60 |
|
| 61 |
@noKwargs |
| 62 |
@noPosargs |
| 63 |
@InterpreterObject.method('keys') |
| 64 |
def keys_method(self, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> T.List[str]: |
| 65 |
return self._keys_getter() |
| 66 |
|
| 67 |
@noKwargs |
| 68 |
@noPosargs |
| 69 |
@InterpreterObject.method('values') |
| 70 |
@FeatureNew('dict.values', '1.10.0') |
| 71 |
def values_method(self, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> T.List[TYPE_var]: |
| 72 |
return [self.held_object[k] for k in self._keys_getter()] |
| 73 |
|
| 74 |
@noArgsFlattening |
| 75 |
@noKwargs |
| 76 |
@typed_pos_args('dict.get', str, optargs=[object]) |
| 77 |
@InterpreterObject.method('get') |
| 78 |
def get_method(self, args: T.Tuple[str, T.Optional[TYPE_var]], kwargs: TYPE_kwargs) -> TYPE_var: |
| 79 |
if args[0] in self.held_object: |
| 80 |
return self.held_object[args[0]] |
| 81 |
if args[1] is not None: |
| 82 |
return args[1] |
| 83 |
raise InvalidArguments(f'Key {args[0]!r} is not in the dictionary.') |
| 84 |
|
| 85 |
@typed_operator(MesonOperator.INDEX, str) |
| 86 |
@InterpreterObject.operator(MesonOperator.INDEX) |
| 87 |
def op_index(self, other: str) -> TYPE_var: |
| 88 |
if other not in self.held_object: |
| 89 |
raise InvalidArguments(f'Key {other} is not in the dictionary.') |
| 90 |
return self.held_object[other] |