Skip to content

Commit cf42df2

Browse files
committed
Add type hints
1 parent 560e0ce commit cf42df2

File tree

162 files changed

+2170
-1306
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

162 files changed

+2170
-1306
lines changed

benedict/core/clean.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
def _clean_dict(d, strings, collections):
1+
from collections.abc import MutableMapping, MutableSequence
2+
from typing import Any, TypeVar
3+
4+
_K = TypeVar("_K")
5+
_V = TypeVar("_V")
6+
_T = TypeVar("_T")
7+
8+
9+
def _clean_dict(
10+
d: MutableMapping[_K, _V], strings: bool, collections: bool
11+
) -> MutableMapping[_K, _V]:
212
keys = list(d.keys())
313
for key in keys:
414
d[key] = _clean_value(d[key], strings=strings, collections=collections)
@@ -7,41 +17,47 @@ def _clean_dict(d, strings, collections):
717
return d
818

919

10-
def _clean_list(ls, strings, collections):
20+
def _clean_list(
21+
ls: MutableSequence[_T], strings: bool, collections: bool
22+
) -> MutableSequence[_T]:
1123
for i in range(len(ls) - 1, -1, -1):
1224
ls[i] = _clean_value(ls[i], strings=strings, collections=collections)
1325
if ls[i] is None:
1426
ls.pop(i)
1527
return ls
1628

1729

18-
def _clean_set(values, strings, collections):
30+
def _clean_set(values: set[_T], strings: bool, collections: bool) -> set[_T]:
1931
return {
2032
value
2133
for value in values
2234
if _clean_value(value, strings=strings, collections=collections) is not None
2335
}
2436

2537

26-
def _clean_str(s, strings, collections):
38+
def _clean_str(s: str, strings: bool, collections: bool) -> str | None:
2739
return s if s and s.strip() else None
2840

2941

30-
def _clean_tuple(values, strings, collections):
42+
def _clean_tuple(
43+
values: tuple[_T, ...], strings: bool, collections: bool
44+
) -> tuple[_T, ...]:
3145
return tuple(
3246
value
3347
for value in values
3448
if _clean_value(value, strings=strings, collections=collections) is not None
3549
)
3650

3751

38-
def _clean_value(value, strings, collections):
52+
def _clean_value(value: Any, strings: bool, collections: bool) -> Any:
3953
if value is None:
4054
return value
41-
elif isinstance(value, list) and collections:
55+
elif isinstance(value, MutableSequence) and collections:
4256
value = _clean_list(value, strings=strings, collections=collections) or None
43-
elif isinstance(value, dict) and collections:
44-
value = _clean_dict(value, strings=strings, collections=collections) or None
57+
elif isinstance(value, MutableMapping) and collections:
58+
value = (
59+
_clean_dict(dict(value), strings=strings, collections=collections) or None
60+
)
4561
elif isinstance(value, set) and collections:
4662
value = _clean_set(value, strings=strings, collections=collections) or None
4763
elif isinstance(value, str) and strings:
@@ -51,5 +67,5 @@ def _clean_value(value, strings, collections):
5167
return value
5268

5369

54-
def clean(d, strings=True, collections=True):
70+
def clean(d: Any, strings: bool = True, collections: bool = True) -> Any:
5571
return _clean_dict(d, strings=strings, collections=collections)

benedict/core/clone.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import copy
2+
from collections.abc import MutableMapping
3+
from typing import Any, TypeVar
24

5+
_T = TypeVar("_T")
36

4-
def clone(obj, empty=False, memo=None):
7+
8+
def clone(
9+
obj: _T,
10+
empty: bool = False,
11+
memo: dict[int, Any] | None = None,
12+
) -> _T:
513
d = copy.deepcopy(obj, memo)
6-
if empty:
14+
if empty and isinstance(d, MutableMapping):
715
d.clear()
816
return d

benedict/core/dump.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
from typing import Any
2+
13
from benedict.serializers import JSONSerializer
24

35

4-
def dump(obj, **kwargs):
6+
def dump(obj: Any, **kwargs: Any) -> str:
57
serializer = JSONSerializer()
68
options = {"indent": 4, "sort_keys": True}
79
options.update(**kwargs)

benedict/core/filter.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
from benedict.core import clone
1+
from collections.abc import Callable
2+
from typing import Any
23

4+
from benedict.core.clone import clone
35

4-
def filter(d, predicate):
6+
7+
def filter(d: Any, predicate: Callable[[Any, Any], bool]) -> Any:
58
if not callable(predicate):
69
raise ValueError("predicate argument must be a callable.")
710
new_dict = clone(d, empty=True)
811
keys = list(d.keys())
912
for key in keys:
10-
value = d.get(key, None)
13+
value = d.get(key)
1114
if predicate(key, value):
1215
new_dict[key] = value
1316
return new_dict

benedict/core/find.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
def find(d, keys, default=None):
1+
from collections.abc import Iterable, Mapping
2+
from typing import TypeVar
3+
4+
_K = TypeVar("_K")
5+
_V = TypeVar("_V")
6+
7+
8+
def find(
9+
d: Mapping[_K, _V], keys: Iterable[_K], default: _V | None = None
10+
) -> _V | None:
211
for key in keys:
312
if key in d:
4-
return d.get(key, default)
13+
return d[key]
514
return default

benedict/core/flatten.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1-
from benedict.core import clone
2-
from benedict.utils import type_util
1+
from collections.abc import Mapping
2+
from typing import Any
33

4+
from benedict.core.clone import clone
45

5-
def _flatten_key(base_key, key, separator):
6+
7+
def _flatten_key(base_key: str, key: str, separator: str) -> str:
68
if base_key and separator:
79
return f"{base_key}{separator}{key}"
810
return key
911

1012

11-
def _flatten_item(d, base_dict, base_key, separator):
13+
def _flatten_item(
14+
d: Any,
15+
base_dict: Any,
16+
base_key: str,
17+
separator: str,
18+
) -> Any:
1219
new_dict = base_dict
1320
keys = list(d.keys())
1421
for key in keys:
1522
new_key = _flatten_key(base_key, key, separator)
1623
value = d.get(key, None)
17-
if type_util.is_dict(value):
24+
if isinstance(value, Mapping):
1825
new_value = _flatten_item(
1926
value, base_dict=new_dict, base_key=new_key, separator=separator
2027
)
@@ -26,6 +33,6 @@ def _flatten_item(d, base_dict, base_key, separator):
2633
return new_dict
2734

2835

29-
def flatten(d, separator="_"):
36+
def flatten(d: Any, separator: str = "_") -> Any:
3037
new_dict = clone(d, empty=True)
3138
return _flatten_item(d, base_dict=new_dict, base_key="", separator=separator)

benedict/core/groupby.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
from collections.abc import Mapping, MutableSequence, Sequence
2+
from typing import Any, TypeVar
3+
14
from benedict.utils import type_util
25

6+
_K = TypeVar("_K")
7+
_V = TypeVar("_V", bound=MutableSequence[Any])
8+
39

4-
def groupby(items, key):
10+
def groupby(items: Sequence[Mapping[_K, Any]], key: _K) -> dict[Any, Any]:
511
if not type_util.is_list(items):
612
raise ValueError("items should be a list of dicts.")
7-
items_grouped = {}
13+
items_grouped: dict[Any, Any] = {}
814
for item in items:
915
if not type_util.is_dict(item):
1016
raise ValueError("item should be a dict.")

benedict/core/invert.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
1-
from benedict.core import clone
1+
from collections.abc import MutableMapping, Sequence
2+
from typing import Any, TypeVar
3+
4+
from benedict.core.clone import clone
25
from benedict.utils import type_util
36

7+
_K = TypeVar("_K")
8+
_V = TypeVar("_V")
9+
410

5-
def _invert_item(d, key, value, flat):
11+
def _invert_item(d: MutableMapping[Any, Any], key: _K, value: _V, flat: bool) -> None:
612
if flat:
7-
d.setdefault(value, key)
13+
d[value] = key
814
else:
9-
d.setdefault(value, []).append(key)
15+
d[value] = [key]
1016

1117

12-
def _invert_list(d, key, value, flat):
18+
def _invert_list(
19+
d: MutableMapping[Any, Any], key: _K, value: Sequence[Any], flat: bool
20+
) -> None:
1321
for value_item in value:
1422
_invert_item(d, key, value_item, flat)
1523

1624

17-
def invert(d, flat=False):
18-
new_dict = clone(d, empty=True)
19-
for key, value in d.items():
25+
def invert(d: Any, flat: bool = False) -> Any:
26+
orig_dict = clone(d, empty=True)
27+
new_dict: dict[Any, Any] = {}
28+
for key, value in orig_dict.items():
2029
if type_util.is_list_or_tuple(value):
2130
_invert_list(new_dict, key, value, flat)
2231
else:

benedict/core/items_sorted.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1-
def _items_sorted_by_item_at_index(d, index, reverse):
1+
from collections.abc import Mapping
2+
3+
from useful_types import SupportsRichComparisonT
4+
5+
6+
def _items_sorted_by_item_at_index(
7+
d: Mapping[SupportsRichComparisonT, SupportsRichComparisonT],
8+
index: int,
9+
reverse: bool,
10+
) -> list[tuple[SupportsRichComparisonT, SupportsRichComparisonT]]:
211
return sorted(d.items(), key=lambda item: item[index], reverse=reverse)
312

413

5-
def items_sorted_by_keys(d, reverse=False):
14+
def items_sorted_by_keys(
15+
d: Mapping[SupportsRichComparisonT, SupportsRichComparisonT], reverse: bool = False
16+
) -> list[tuple[SupportsRichComparisonT, SupportsRichComparisonT]]:
617
return _items_sorted_by_item_at_index(d, 0, reverse)
718

819

9-
def items_sorted_by_values(d, reverse=False):
20+
def items_sorted_by_values(
21+
d: Mapping[SupportsRichComparisonT, SupportsRichComparisonT], reverse: bool = False
22+
) -> list[tuple[SupportsRichComparisonT, SupportsRichComparisonT]]:
1023
return _items_sorted_by_item_at_index(d, 1, reverse)

benedict/core/keylists.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
from collections.abc import Mapping, Sequence
2+
from typing import Any
3+
14
from benedict.utils import type_util
25

36

4-
def _get_keylist_for_dict(d, parent_keys, indexes):
7+
def _get_keylist_for_dict(
8+
d: Mapping[Any, Any], parent_keys: list[Any], indexes: bool
9+
) -> list[list[Any]]:
510
keylist = []
611
for key, value in d.items():
712
keys = parent_keys + [key]
@@ -10,7 +15,9 @@ def _get_keylist_for_dict(d, parent_keys, indexes):
1015
return keylist
1116

1217

13-
def _get_keylist_for_list(ls, parent_keys, indexes):
18+
def _get_keylist_for_list(
19+
ls: Sequence[Any], parent_keys: list[Any], indexes: bool
20+
) -> list[list[Any]]:
1421
keylist = []
1522
for key, value in enumerate(ls):
1623
keys = list(parent_keys)
@@ -20,13 +27,17 @@ def _get_keylist_for_list(ls, parent_keys, indexes):
2027
return keylist
2128

2229

23-
def _get_keylist_for_value(value, parent_keys, indexes):
30+
def _get_keylist_for_value(
31+
value: Mapping[Any, Any] | Sequence[Any], parent_keys: list[Any], indexes: bool
32+
) -> list[list[Any]]:
2433
if type_util.is_dict(value):
2534
return _get_keylist_for_dict(value, parent_keys, indexes)
2635
elif type_util.is_list(value) and indexes:
2736
return _get_keylist_for_list(value, parent_keys, indexes)
2837
return []
2938

3039

31-
def keylists(d, indexes=False):
40+
def keylists(
41+
d: Mapping[Any, Any] | Sequence[Any], indexes: bool = False
42+
) -> list[list[Any]]:
3243
return _get_keylist_for_value(d, [], indexes)

0 commit comments

Comments
 (0)