Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/markdown/snippets/add_dict_values.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## Added a `values()` method for dictionaries

Mesons built-in [[@dict]] type now supports the [[dict.values]] method
to retrieve the dictionary values as an array, analogous to the
[[dict.keys]] method.

```meson
dict = { 'b': 'world', 'a': 'hello' }

[[#dict.keys]] # Returns ['a', 'b']
[[#dict.values]] # Returns ['hello', 'world']
```
9 changes: 8 additions & 1 deletion docs/yaml/elementary/dict.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,11 @@ methods:

- name: keys
returns: array[str]
description: Returns an array of keys in the dictionary.
description: Returns an array of keys in the dictionary, sorted in ascending order.

- name: values
returns: array[any]
since: 1.10.0
description: |
Returns an array of values in the dictionary, sorted by the
corresponding keys in ascending order.
13 changes: 12 additions & 1 deletion mesonbuild/interpreter/primitives/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
IterableObject,
MesonOperator,
ObjectHolder,
FeatureNew,
typed_operator,
noKwargs,
noPosargs,
Expand Down Expand Up @@ -48,6 +49,9 @@ def iter_self(self) -> T.Iterator[T.Tuple[str, TYPE_var]]:
def size(self) -> int:
return len(self.held_object)

def _keys_getter(self) -> T.List[str]:
return sorted(self.held_object)

@noKwargs
@typed_pos_args('dict.has_key', str)
@InterpreterObject.method('has_key')
Expand All @@ -58,7 +62,14 @@ def has_key_method(self, args: T.Tuple[str], kwargs: TYPE_kwargs) -> bool:
@noPosargs
@InterpreterObject.method('keys')
def keys_method(self, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> T.List[str]:
return sorted(self.held_object)
return self._keys_getter()

@noKwargs
@noPosargs
@InterpreterObject.method('values')
@FeatureNew('dict.values', '1.10.0')
def values_method(self, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> T.List[TYPE_var]:
return [self.held_object[k] for k in self._keys_getter()]

@noArgsFlattening
@noKwargs
Expand Down
12 changes: 12 additions & 0 deletions test cases/common/188 dict/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,24 @@ endforeach

assert(i == 3, 'There should be three elements in that dictionary')

# Test keys() works as expected (note that it sorts the keys)
assert(dict.keys() == ['baz', 'foo', 'foo bar'], 'Keys returned unexpected list')

# Test values() works as expected (note that it sorts first, to match keys())
assert(dict.values() == ['foo', 'bar', 'baz'])

empty_dict = {}

foreach key, value : empty_dict
assert(false, 'This dict should be empty')
endforeach

# Test keys() returns an empty list
assert(empty_dict.keys() == [], 'Keys for an empty dict should be an empty list')

# Test values() returns an empty list
assert(empty_dict.values() == [], 'Values for an empty dict should be an empty list')

d1 = empty_dict + {'a' : 'b'}
assert(d1 == {'a' : 'b'}, 'dict addition is not working')

Expand Down
Loading