|
1 | 1 | from collections.abc import MutableMapping, MutableSequence |
2 | 2 | from enum import IntFlag, auto |
3 | | -from typing import Union, List, Any, Dict, Callable, Sequence, Tuple |
| 3 | +from typing import Union, List, Any, Dict, Callable, Sequence, Tuple, Optional |
4 | 4 |
|
5 | 5 | from dpath import options, segments |
6 | 6 | from dpath.exceptions import InvalidKeyName, PathNotFound |
|
9 | 9 |
|
10 | 10 |
|
11 | 11 | class MergeType(IntFlag): |
12 | | - REPLACE = auto() |
13 | 12 | ADDITIVE = auto() |
| 13 | + """List objects are combined onto one long list (NOT a set). This is the default flag.""" |
| 14 | + |
| 15 | + REPLACE = auto() |
| 16 | + """Instead of combining list objects, when 2 list objects are at an equal depth of merge, replace the destination |
| 17 | + with the source.""" |
| 18 | + |
14 | 19 | TYPESAFE = auto() |
| 20 | + """When 2 keys at equal levels are of different types, raise a TypeError exception. By default, the source |
| 21 | + replaces the destination in this situation.""" |
15 | 22 |
|
16 | 23 |
|
17 | | -# Type alias for dict path segments where integers are explicitly casted |
18 | 24 | PathSegment = Union[int, str] |
| 25 | +"""Type alias for dict path segments where integers are explicitly casted.""" |
| 26 | + |
| 27 | +Filter = Callable[[Any], bool] |
| 28 | +"""Type alias for filter functions. |
| 29 | +
|
| 30 | +(Any) -> bool""" |
| 31 | + |
| 32 | +Hints = Sequence[Tuple[PathSegment, type]] |
| 33 | +"""Type alias for creator function hint sequences.""" |
19 | 34 |
|
20 | | -# Type alias for filter functions |
21 | | -Filter = Callable[[Any], bool] # (Any) -> bool |
| 35 | +Creator = Callable[[Union[Dict, List], Sequence[PathSegment], int, Optional[Hints]], None] |
| 36 | +"""Type alias for creator functions. |
22 | 37 |
|
23 | | -# Type alias for creator functions |
24 | | -Creator = Callable[[Union[Dict, List], List[PathSegment], int, Sequence[Tuple[PathSegment, type]]], None] |
| 38 | +Example creator function signature: |
| 39 | + |
| 40 | + def creator( |
| 41 | + current: Union[Dict, List], |
| 42 | + segments: Sequence[PathSegment], |
| 43 | + i: int, |
| 44 | + hints: Sequence[Tuple[PathSegment, type]] = () |
| 45 | + )""" |
25 | 46 |
|
26 | 47 |
|
27 | 48 | def _split_path(path: str, separator: str) -> Union[List[PathSegment], PathSegment]: |
@@ -270,14 +291,6 @@ def merge(dst: Dict, src: Dict, separator='/', afilter: Filter = None, flags=Mer |
270 | 291 | https://github.com/akesterson/dpath-python/issues/58 |
271 | 292 |
|
272 | 293 | flags is an OR'ed combination of MergeType enum members. |
273 | | - * ADDITIVE : List objects are combined onto one long |
274 | | - list (NOT a set). This is the default flag. |
275 | | - * REPLACE : Instead of combining list objects, when |
276 | | - 2 list objects are at an equal depth of merge, replace |
277 | | - the destination with the source. |
278 | | - * TYPESAFE : When 2 keys at equal levels are of different |
279 | | - types, raise a TypeError exception. By default, the source |
280 | | - replaces the destination in this situation. |
281 | 294 | """ |
282 | 295 | filtered_src = search(src, '**', afilter=afilter, separator='/') |
283 | 296 |
|
|
0 commit comments