Skip to content

Commit b16c6ad

Browse files
committed
More type hinting
1 parent 2b73ab3 commit b16c6ad

2 files changed

Lines changed: 15 additions & 9 deletions

File tree

dpath/segments.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from copy import deepcopy
22
from fnmatch import fnmatchcase
3+
from typing import List, Sequence, Tuple
34

45
from dpath import options
56
from dpath.exceptions import InvalidGlob, InvalidKeyName, PathNotFound
7+
from dpath.util import PathSegment
68

79

810
def kvs(node):
@@ -263,7 +265,7 @@ def extend(thing, index, value=None):
263265
return thing
264266

265267

266-
def __default_creator__(current, segments, i, hints=()):
268+
def __default_creator__(current, segments: List[str], i: int, hints: Sequence[Tuple[PathSegment, type]] = ()):
267269
"""
268270
Create missing path components. If the segment is an int, then it will
269271
create a list. Otherwise a dictionary is created.

dpath/util.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from collections.abc import MutableMapping, MutableSequence
22
from enum import IntFlag, auto
3-
from typing import Union, List, Any, Dict
3+
from typing import Union, List, Any, Dict, Callable
44

55
from dpath import options, segments
66
from dpath.exceptions import InvalidKeyName, PathNotFound
@@ -17,6 +17,9 @@ class MergeType(IntFlag):
1717
# Type alias for dict path segments where integers are explicitly casted
1818
PathSegment = Union[int, str]
1919

20+
# Type alias for filter functions
21+
Filter = Callable[[Any], bool] # (Any) -> bool
22+
2023

2124
def _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

Comments
 (0)