11"""Test to_dict functionality for all multidict types."""
22
3- from typing import Any , Callable , Type , Iterable
3+ from typing import Type , Iterable
44from multidict import MultiMapping
55
66
77from typing import Protocol
88
99
10- from typing import Protocol , Type
1110from multidict import MultiDict , CIMultiDict , MultiDictProxy , CIMultiDictProxy
1211
12+
1313class MultidictModule (Protocol ):
1414 MultiDict : Type [MultiDict [object ]]
1515 CIMultiDict : Type [CIMultiDict [object ]]
1616 MultiDictProxy : Type [MultiDictProxy [object ]]
1717 CIMultiDictProxy : Type [CIMultiDictProxy [object ]]
1818
19+
1920class DictFactory (Protocol ):
20- def __call__ (self , arg : Iterable [tuple [str , object ]] | None = None ) -> MultiMapping [object ]: ...
21+ def __call__ (
22+ self , arg : Iterable [tuple [str , object ]] | None = None
23+ ) -> MultiMapping [object ]: ...
24+
2125
2226import pytest
2327
@@ -45,9 +49,7 @@ def test_to_dict_empty(self, cls: DictFactory) -> None:
4549 result = d .to_dict ()
4650 assert result == {}
4751
48- def test_to_dict_returns_new_dict (
49- self , cls : DictFactory
50- ) -> None :
52+ def test_to_dict_returns_new_dict (self , cls : DictFactory ) -> None :
5153 """Test that each call returns a new dictionary instance."""
5254 d = cls ([("a" , 1 )])
5355 result1 = d .to_dict ()
@@ -62,9 +64,7 @@ def test_to_dict_list_is_fresh(self, cls: DictFactory) -> None:
6264 result2 = d .to_dict ()
6365 assert result1 ["a" ] is not result2 ["a" ]
6466
65- def test_to_dict_order_preservation (
66- self , cls : DictFactory
67- ) -> None :
67+ def test_to_dict_order_preservation (self , cls : DictFactory ) -> None :
6868 """Test that value lists maintain insertion order."""
6969 d = cls ([("x" , 3 ), ("x" , 1 ), ("x" , 2 )])
7070 result = d .to_dict ()
@@ -78,9 +78,7 @@ def test_to_dict_large_data(self, cls: DictFactory) -> None:
7878 assert len (result ) == 100
7979 assert all (len (v ) == 100 for v in result .values ())
8080
81- def test_to_dict_mixed_value_types (
82- self , cls : DictFactory
83- ) -> None :
81+ def test_to_dict_mixed_value_types (self , cls : DictFactory ) -> None :
8482 """Test to_dict with mixed value types (str, int) to verify generic _V."""
8583 d = cls ([("a" , 1 ), ("a" , "two" ), ("b" , 3.14 )])
8684 result = d .to_dict ()
@@ -103,9 +101,7 @@ class TestCIMultiDictToDict(BaseToDictTests):
103101 def cls (self , multidict_module : MultidictModule ) -> Type [CIMultiDict [object ]]:
104102 return multidict_module .CIMultiDict
105103
106- def test_to_dict_case_insensitive_grouping (
107- self , cls : DictFactory
108- ) -> None :
104+ def test_to_dict_case_insensitive_grouping (self , cls : DictFactory ) -> None :
109105 """Test that case variants are grouped under the same key."""
110106 d = cls ([("A" , 1 ), ("a" , 2 ), ("B" , 3 )])
111107 result = d .to_dict ()
@@ -123,8 +119,12 @@ class TestMultiDictProxyToDict(BaseToDictTests):
123119
124120 @pytest .fixture
125121 def cls (self , multidict_module : MultidictModule ) -> DictFactory :
126- def make_proxy (arg : Iterable [tuple [str , object ]] | None = None ) -> MultiMapping [object ]:
127- md : MultiDict [object ] = multidict_module .MultiDict (arg ) if arg else multidict_module .MultiDict ()
122+ def make_proxy (
123+ arg : Iterable [tuple [str , object ]] | None = None ,
124+ ) -> MultiMapping [object ]:
125+ md : MultiDict [object ] = (
126+ multidict_module .MultiDict (arg ) if arg else multidict_module .MultiDict ()
127+ )
128128 return multidict_module .MultiDictProxy (md )
129129
130130 return make_proxy
@@ -145,15 +145,19 @@ class TestCIMultiDictProxyToDict(BaseToDictTests):
145145
146146 @pytest .fixture
147147 def cls (self , multidict_module : MultidictModule ) -> DictFactory :
148- def make_proxy (arg : Iterable [tuple [str , object ]] | None = None ) -> MultiMapping [object ]:
149- md : CIMultiDict [object ] = multidict_module .CIMultiDict (arg ) if arg else multidict_module .CIMultiDict ()
148+ def make_proxy (
149+ arg : Iterable [tuple [str , object ]] | None = None ,
150+ ) -> MultiMapping [object ]:
151+ md : CIMultiDict [object ] = (
152+ multidict_module .CIMultiDict (arg )
153+ if arg
154+ else multidict_module .CIMultiDict ()
155+ )
150156 return multidict_module .CIMultiDictProxy (md )
151157
152158 return make_proxy
153159
154- def test_to_dict_case_insensitive_grouping (
155- self , cls : DictFactory
156- ) -> None :
160+ def test_to_dict_case_insensitive_grouping (self , cls : DictFactory ) -> None :
157161 """Test that case variants are grouped under the same key."""
158162 d = cls ([("A" , 1 ), ("a" , 2 ), ("B" , 3 )])
159163 result = d .to_dict ()
0 commit comments