-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathkeys.py
More file actions
31 lines (23 loc) · 822 Bytes
/
keys.py
File metadata and controls
31 lines (23 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""The `keys` JSONPath filter function."""
from typing import Mapping
from typing import Tuple
from typing import Union
from jsonpath.filter import UNDEFINED
from jsonpath.filter import _Undefined
from .filter_function import ExpressionType
from .filter_function import FilterFunction
class Keys(FilterFunction):
"""The `keys` JSONPath filter function."""
arg_types = [ExpressionType.VALUE]
return_type = ExpressionType.VALUE
def __call__(
self, value: Mapping[str, object]
) -> Union[Tuple[str, ...], _Undefined]:
"""Return a tuple of keys in `value`.
If `value` does not have a `keys()` method, the special _Nothing_ value
is returned.
"""
try:
return tuple(value.keys())
except AttributeError:
return UNDEFINED