11from collections .abc import MutableMapping , MutableSequence
22from enum import IntFlag , auto
3- from typing import Union , List , Any , Dict
3+ from typing import Union , List , Any , Dict , Callable
44
55from dpath import options , segments
66from dpath .exceptions import InvalidKeyName , PathNotFound
@@ -17,6 +17,9 @@ class MergeType(IntFlag):
1717# Type alias for dict path segments where integers are explicitly casted
1818PathSegment = Union [int , str ]
1919
20+ # Type alias for filter functions
21+ Filter = Callable [[Any ], bool ] # (Any) -> bool
22+
2023
2124def _split_path (path : str , separator : str ) -> Union [List [PathSegment ], PathSegment ]:
2225 """
@@ -45,7 +48,8 @@ def _split_path(path: str, separator: str) -> Union[List[PathSegment], PathSegme
4548 return split_segments
4649
4750
48- def new (obj : Dict , path : str , value , separator = "/" , creator = None ):
51+ # todo: Type hint creator arg
52+ def new (obj : Dict , path : str , value , separator = "/" , creator = None ) -> Dict :
4953 """
5054 Set the element at the terminus of path to value, and create
5155 it if it does not exist (as opposed to 'set' that can only
@@ -65,7 +69,7 @@ def new(obj: Dict, path: str, value, separator="/", creator=None):
6569 return segments .set (obj , split_segments , value )
6670
6771
68- def delete (obj , glob , separator = '/' , afilter = None ):
72+ def delete (obj : Dict , glob : str , separator = '/' , afilter : Filter = None ) -> int :
6973 """
7074 Given a obj, delete all elements that match the glob.
7175
@@ -122,7 +126,7 @@ def f(obj, pair, counter):
122126 return deleted
123127
124128
125- def set (obj , glob , value , separator = '/' , afilter = None ):
129+ def set (obj : Dict , glob : str , value , separator = '/' , afilter : Filter = None ) -> int :
126130 """
127131 Given a path glob, set all existing elements in the document
128132 to the given value. Returns the number of elements changed.
@@ -147,7 +151,7 @@ def f(obj, pair, counter):
147151 return changed
148152
149153
150- def get (obj : Dict , glob : str , separator = "/" , default : Any = _DEFAULT_SENTINEL ) -> dict :
154+ def get (obj : Dict , glob : str , separator = "/" , default : Any = _DEFAULT_SENTINEL ) -> Dict :
151155 """
152156 Given an object which contains only one possible match for the given glob,
153157 return the value for the leaf matching the given glob.
@@ -183,7 +187,7 @@ def f(_, pair, results):
183187 return results [0 ]
184188
185189
186- def values (obj , glob , separator = '/' , afilter = None , dirs = True ):
190+ def values (obj : Dict , glob : str , separator = '/' , afilter : Filter = None , dirs = True ):
187191 """
188192 Given an object and a path glob, return an array of all values which match
189193 the glob. The arguments to this function are identical to those of search().
@@ -193,7 +197,7 @@ def values(obj, glob, separator='/', afilter=None, dirs=True):
193197 return [v for p , v in search (obj , glob , yielded , separator , afilter , dirs )]
194198
195199
196- def search (obj , glob , yielded = False , separator = '/' , afilter = None , dirs = True ):
200+ def search (obj : Dict , glob : str , yielded = False , separator = '/' , afilter : Filter = None , dirs = True ):
197201 """
198202 Given a path glob, return a dictionary containing all keys
199203 that matched the given glob.
@@ -234,7 +238,7 @@ def f(obj, pair, result):
234238 return segments .fold (obj , f , {})
235239
236240
237- def merge (dst , src , separator = '/' , afilter = None , flags = MergeType .ADDITIVE ):
241+ def merge (dst : Dict , src : Dict , separator = '/' , afilter : Filter = None , flags = MergeType .ADDITIVE ):
238242 """
239243 Merge source into destination. Like dict.update() but performs deep
240244 merging.
0 commit comments