1- def _clean_dict (d , strings , collections ):
1+ from __future__ import annotations
2+
3+ from collections .abc import MutableMapping , MutableSequence
4+ from typing import Any , TypeVar
5+
6+ _K = TypeVar ("_K" )
7+ _V = TypeVar ("_V" )
8+ _T = TypeVar ("_T" )
9+
10+
11+ def _clean_dict (
12+ d : MutableMapping [_K , _V ], strings : bool , collections : bool
13+ ) -> MutableMapping [_K , _V ]:
214 keys = list (d .keys ())
315 for key in keys :
416 d [key ] = _clean_value (d [key ], strings = strings , collections = collections )
@@ -7,41 +19,47 @@ def _clean_dict(d, strings, collections):
719 return d
820
921
10- def _clean_list (ls , strings , collections ):
22+ def _clean_list (
23+ ls : MutableSequence [_T ], strings : bool , collections : bool
24+ ) -> MutableSequence [_T ]:
1125 for i in range (len (ls ) - 1 , - 1 , - 1 ):
1226 ls [i ] = _clean_value (ls [i ], strings = strings , collections = collections )
1327 if ls [i ] is None :
1428 ls .pop (i )
1529 return ls
1630
1731
18- def _clean_set (values , strings , collections ) :
32+ def _clean_set (values : set [ _T ] , strings : bool , collections : bool ) -> set [ _T ] :
1933 return {
2034 value
2135 for value in values
2236 if _clean_value (value , strings = strings , collections = collections ) is not None
2337 }
2438
2539
26- def _clean_str (s , strings , collections ) :
40+ def _clean_str (s : str , strings : bool , collections : bool ) -> str | None :
2741 return s if s and s .strip () else None
2842
2943
30- def _clean_tuple (values , strings , collections ):
44+ def _clean_tuple (
45+ values : tuple [_T , ...], strings : bool , collections : bool
46+ ) -> tuple [_T , ...]:
3147 return tuple (
3248 value
3349 for value in values
3450 if _clean_value (value , strings = strings , collections = collections ) is not None
3551 )
3652
3753
38- def _clean_value (value , strings , collections ) :
54+ def _clean_value (value : Any , strings : bool , collections : bool ) -> Any :
3955 if value is None :
4056 return value
41- elif isinstance (value , list ) and collections :
57+ elif isinstance (value , MutableSequence ) and collections :
4258 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
59+ elif isinstance (value , MutableMapping ) and collections :
60+ value = (
61+ _clean_dict (dict (value ), strings = strings , collections = collections ) or None
62+ )
4563 elif isinstance (value , set ) and collections :
4664 value = _clean_set (value , strings = strings , collections = collections ) or None
4765 elif isinstance (value , str ) and strings :
@@ -51,5 +69,5 @@ def _clean_value(value, strings, collections):
5169 return value
5270
5371
54- def clean (d , strings = True , collections = True ):
72+ def clean (d : Any , strings : bool = True , collections : bool = True ) -> Any :
5573 return _clean_dict (d , strings = strings , collections = collections )
0 commit comments