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 )
0 commit comments