Skip to content

Commit 7a565ee

Browse files
committed
Type hint improvements and exports
1 parent 36a6f5f commit 7a565ee

File tree

4 files changed

+43
-19
lines changed

4 files changed

+43
-19
lines changed

dpath/__init__.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,26 @@
1111
"segments",
1212
"types",
1313
"version",
14+
"MergeType",
15+
"PathSegment",
16+
"Filter",
17+
"Glob",
18+
"Path",
19+
"Hints",
20+
"Creator",
1421
]
1522

1623
from collections.abc import MutableMapping, MutableSequence
17-
from typing import Union, List, Dict, Any
24+
from typing import Union, List, Any, Callable, Optional
1825

1926
from dpath import segments, options
2027
from dpath.exceptions import InvalidKeyName, PathNotFound
21-
from dpath.types import MergeType, PathSegment, Creator, Filter
28+
from dpath.types import MergeType, PathSegment, Creator, Filter, Glob, Path, Hints
2229

2330
_DEFAULT_SENTINEL = object()
2431

2532

26-
def _split_path(path: str, separator: str) -> Union[List[PathSegment], PathSegment]:
33+
def _split_path(path: Path, separator: Optional[str]) -> Union[List[PathSegment], PathSegment]:
2734
"""
2835
Given a path and separator, return a tuple of segments. If path is
2936
already a non-leaf thing, return it.
@@ -51,7 +58,7 @@ def _split_path(path: str, separator: str) -> Union[List[PathSegment], PathSegme
5158
return split_segments
5259

5360

54-
def new(obj: Dict, path: str, value, separator="/", creator: Creator = None) -> Dict:
61+
def new(obj: MutableMapping, path: Path, value, separator="/", creator: Creator = None) -> MutableMapping:
5562
"""
5663
Set the element at the terminus of path to value, and create
5764
it if it does not exist (as opposed to 'set' that can only
@@ -71,7 +78,7 @@ def new(obj: Dict, path: str, value, separator="/", creator: Creator = None) ->
7178
return segments.set(obj, split_segments, value)
7279

7380

74-
def delete(obj: Dict, glob: str, separator="/", afilter: Filter = None) -> int:
81+
def delete(obj: MutableMapping, glob: Glob, separator="/", afilter: Filter = None) -> int:
7582
"""
7683
Given a obj, delete all elements that match the glob.
7784
@@ -130,7 +137,7 @@ def f(obj, pair, counter):
130137
return deleted
131138

132139

133-
def set(obj: Dict, glob: str, value, separator="/", afilter: Filter = None) -> int:
140+
def set(obj: MutableMapping, glob: Glob, value, separator="/", afilter: Filter = None) -> int:
134141
"""
135142
Given a path glob, set all existing elements in the document
136143
to the given value. Returns the number of elements changed.
@@ -155,7 +162,12 @@ def f(obj, pair, counter):
155162
return changed
156163

157164

158-
def get(obj: Dict, glob: str, separator="/", default: Any = _DEFAULT_SENTINEL) -> Dict:
165+
def get(
166+
obj: MutableMapping,
167+
glob: Glob,
168+
separator="/",
169+
default: Any = _DEFAULT_SENTINEL
170+
) -> Union[MutableMapping, object, Callable]:
159171
"""
160172
Given an object which contains only one possible match for the given glob,
161173
return the value for the leaf matching the given glob.
@@ -191,7 +203,7 @@ def f(_, pair, results):
191203
return results[0]
192204

193205

194-
def values(obj: Dict, glob: str, separator="/", afilter: Filter = None, dirs=True):
206+
def values(obj: MutableMapping, glob: Glob, separator="/", afilter: Filter = None, dirs=True):
195207
"""
196208
Given an object and a path glob, return an array of all values which match
197209
the glob. The arguments to this function are identical to those of search().
@@ -201,7 +213,7 @@ def values(obj: Dict, glob: str, separator="/", afilter: Filter = None, dirs=Tru
201213
return [v for p, v in search(obj, glob, yielded, separator, afilter, dirs)]
202214

203215

204-
def search(obj: Dict, glob: str, yielded=False, separator="/", afilter: Filter = None, dirs=True):
216+
def search(obj: MutableMapping, glob: Glob, yielded=False, separator="/", afilter: Filter = None, dirs=True):
205217
"""
206218
Given a path glob, return a dictionary containing all keys
207219
that matched the given glob.
@@ -243,7 +255,7 @@ def f(obj, pair, result):
243255
return segments.fold(obj, f, {})
244256

245257

246-
def merge(dst: Dict, src: Dict, separator="/", afilter: Filter = None, flags=MergeType.ADDITIVE):
258+
def merge(dst: MutableMapping, src: MutableMapping, separator="/", afilter: Filter = None, flags=MergeType.ADDITIVE):
247259
"""
248260
Merge source into destination. Like dict.update() but performs deep
249261
merging.

dpath/segments.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from copy import deepcopy
22
from fnmatch import fnmatchcase
3-
from typing import List, Sequence, Tuple, Iterator, Any, Dict, Union, Optional
3+
from typing import List, Sequence, Tuple, Iterator, Any, Union, Optional, MutableMapping
44

55
from dpath import options
66
from dpath.exceptions import InvalidGlob, InvalidKeyName, PathNotFound
@@ -266,7 +266,7 @@ def extend(thing: List, index: int, value=None):
266266

267267

268268
def _default_creator(
269-
current: Union[Dict, List],
269+
current: Union[MutableMapping, List],
270270
segments: Sequence[PathSegment],
271271
i: int,
272272
hints: Sequence[Tuple[PathSegment, type]] = ()
@@ -301,12 +301,12 @@ def _default_creator(
301301

302302

303303
def set(
304-
obj,
304+
obj: MutableMapping,
305305
segments: Sequence[PathSegment],
306306
value,
307307
creator: Optional[Creator] = _default_creator,
308308
hints: Hints = ()
309-
):
309+
) -> MutableMapping:
310310
"""
311311
Set the value in obj at the place indicated by segments. If creator is not
312312
None (default __default_creator__), then call the creator function to

dpath/types.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from enum import IntFlag, auto
2-
from typing import Union, Any, Callable, Sequence, Tuple, Dict, List, Optional
2+
from typing import Union, Any, Callable, Sequence, Tuple, List, Optional, MutableMapping
33

44

55
class MergeType(IntFlag):
@@ -23,16 +23,22 @@ class MergeType(IntFlag):
2323
2424
(Any) -> bool"""
2525

26+
Glob = Union[str, Sequence[str]]
27+
"""Type alias for glob parameters."""
28+
29+
Path = Union[str, Sequence[PathSegment]]
30+
"""Type alias for path parameters."""
31+
2632
Hints = Sequence[Tuple[PathSegment, type]]
2733
"""Type alias for creator function hint sequences."""
2834

29-
Creator = Callable[[Union[Dict, List], Sequence[PathSegment], int, Optional[Hints]], None]
35+
Creator = Callable[[Union[MutableMapping, List], Path, int, Optional[Hints]], None]
3036
"""Type alias for creator functions.
3137
3238
Example creator function signature:
3339
3440
def creator(
35-
current: Union[Dict, List],
41+
current: Union[MutableMapping, List],
3642
segments: Sequence[PathSegment],
3743
i: int,
3844
hints: Sequence[Tuple[PathSegment, type]] = ()

tests/test_types.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77

88

99
class TestMapping(MutableMapping):
10-
def __init__(self, data={}):
10+
def __init__(self, data=None):
11+
if data is None:
12+
data = {}
13+
1114
self._mapping = {}
1215
self._mapping.update(data)
1316

@@ -31,7 +34,10 @@ def __delitem__(self, key):
3134

3235

3336
class TestSequence(MutableSequence):
34-
def __init__(self, data=list()):
37+
def __init__(self, data=None):
38+
if data is None:
39+
data = list()
40+
3541
self._list = [] + data
3642

3743
def __len__(self):

0 commit comments

Comments
 (0)