11"""Test to_dict functionality for all multidict types."""
2+ from typing import Any , Type
23
34import pytest
45
6+ from multidict import CIMultiDict , CIMultiDictProxy , MultiDict , MultiDictProxy
7+
58
69class BaseToDictTests :
710 """Base tests for to_dict() method, inherited by all multidict type tests."""
811
9- def test_to_dict_simple (self , cls ) :
12+ def test_to_dict_simple (self , cls : Any ) -> None :
1013 """Test basic conversion with unique keys."""
1114 d = cls ([("a" , 1 ), ("b" , 2 )])
1215 result = d .to_dict ()
1316 assert result == {"a" : [1 ], "b" : [2 ]}
1417
15- def test_to_dict_multi_values (self , cls ) :
18+ def test_to_dict_multi_values (self , cls : Any ) -> None :
1619 """Test grouping multiple values under the same key."""
1720 d = cls ([("a" , 1 ), ("b" , 2 ), ("a" , 3 )])
1821 result = d .to_dict ()
1922 assert result == {"a" : [1 , 3 ], "b" : [2 ]}
2023
21- def test_to_dict_empty (self , cls ) :
24+ def test_to_dict_empty (self , cls : Any ) -> None :
2225 """Test conversion of an empty multidict."""
2326 d = cls ()
2427 result = d .to_dict ()
2528 assert result == {}
2629
27- def test_to_dict_returns_new_dict (self , cls ) :
30+ def test_to_dict_returns_new_dict (self , cls : Any ) -> None :
2831 """Test that each call returns a new dictionary instance."""
2932 d = cls ([("a" , 1 )])
3033 result1 = d .to_dict ()
3134 result2 = d .to_dict ()
3235 assert result1 == result2
3336 assert result1 is not result2
3437
35- def test_to_dict_list_is_fresh (self , cls ) :
38+ def test_to_dict_list_is_fresh (self , cls : Any ) -> None :
3639 """Test that value lists are independent between calls."""
3740 d = cls ([("a" , 1 )])
3841 result1 = d .to_dict ()
3942 result2 = d .to_dict ()
4043 assert result1 ["a" ] is not result2 ["a" ]
4144
42- def test_to_dict_order_preservation (self , cls ) :
45+ def test_to_dict_order_preservation (self , cls : Any ) -> None :
4346 """Test that value lists maintain insertion order."""
4447 d = cls ([("x" , 3 ), ("x" , 1 ), ("x" , 2 )])
4548 result = d .to_dict ()
4649 assert result ["x" ] == [3 , 1 , 2 ]
4750
48- def test_to_dict_large_data (self , cls ) :
51+ def test_to_dict_large_data (self , cls : Any ) -> None :
4952 """Test to_dict with a large number of entries for performance."""
5053 items = [(f"key{ i % 100 } " , i ) for i in range (10000 )]
5154 d = cls (items )
5255 result = d .to_dict ()
5356 assert len (result ) == 100
5457 assert all (len (v ) == 100 for v in result .values ())
5558
56- def test_to_dict_mixed_value_types (self , cls ) :
59+ def test_to_dict_mixed_value_types (self , cls : Any ) -> None :
5760 """Test to_dict with mixed value types (str, int) to verify generic _V."""
5861 d = cls ([("a" , 1 ), ("a" , "two" ), ("b" , 3.14 )])
5962 result = d .to_dict ()
@@ -65,18 +68,18 @@ class TestMultiDictToDict(BaseToDictTests):
6568 """Tests for MultiDict.to_dict()."""
6669
6770 @pytest .fixture
68- def cls (self , multidict_module ) :
69- return multidict_module .MultiDict
71+ def cls (self , multidict_module : Any ) -> Type [ MultiDict [ Any ]] :
72+ return multidict_module .MultiDict # type: ignore[no-any-return]
7073
7174
7275class TestCIMultiDictToDict (BaseToDictTests ):
7376 """Tests for CIMultiDict.to_dict()."""
7477
7578 @pytest .fixture
76- def cls (self , multidict_module ) :
77- return multidict_module .CIMultiDict
79+ def cls (self , multidict_module : Any ) -> Type [ CIMultiDict [ Any ]] :
80+ return multidict_module .CIMultiDict # type: ignore[no-any-return]
7881
79- def test_to_dict_case_insensitive_grouping (self , cls ) :
82+ def test_to_dict_case_insensitive_grouping (self , cls : Any ) -> None :
8083 """Test that case variants are grouped under the same key."""
8184 d = cls ([("A" , 1 ), ("a" , 2 ), ("B" , 3 )])
8285 result = d .to_dict ()
@@ -93,14 +96,13 @@ class TestMultiDictProxyToDict(BaseToDictTests):
9396 """Tests for MultiDictProxy.to_dict()."""
9497
9598 @pytest .fixture
96- def cls (self , multidict_module ) :
97- def make_proxy (* args , ** kwargs ) :
99+ def cls (self , multidict_module : Any ) -> Any :
100+ def make_proxy (* args : Any , ** kwargs : Any ) -> MultiDictProxy [ Any ] :
98101 md = multidict_module .MultiDict (* args , ** kwargs )
99- return multidict_module .MultiDictProxy (md )
100-
102+ return multidict_module .MultiDictProxy (md ) # type: ignore[no-any-return]
101103 return make_proxy
102104
103- def test_to_dict_proxy_mutation_isolation (self , cls , multidict_module ) :
105+ def test_to_dict_proxy_mutation_isolation (self , cls : Any , multidict_module : Any ) -> None :
104106 """Test that modifying returned dict does not affect the proxy."""
105107 md = multidict_module .MultiDict ([("a" , 1 )])
106108 proxy = multidict_module .MultiDictProxy (md )
@@ -113,14 +115,13 @@ class TestCIMultiDictProxyToDict(BaseToDictTests):
113115 """Tests for CIMultiDictProxy.to_dict()."""
114116
115117 @pytest .fixture
116- def cls (self , multidict_module ) :
117- def make_proxy (* args , ** kwargs ) :
118+ def cls (self , multidict_module : Any ) -> Any :
119+ def make_proxy (* args : Any , ** kwargs : Any ) -> CIMultiDictProxy [ Any ] :
118120 md = multidict_module .CIMultiDict (* args , ** kwargs )
119- return multidict_module .CIMultiDictProxy (md )
120-
121+ return multidict_module .CIMultiDictProxy (md ) # type: ignore[no-any-return]
121122 return make_proxy
122123
123- def test_to_dict_case_insensitive_grouping (self , cls ) :
124+ def test_to_dict_case_insensitive_grouping (self , cls : Any ) -> None :
124125 """Test that case variants are grouped under the same key."""
125126 d = cls ([("A" , 1 ), ("a" , 2 ), ("B" , 3 )])
126127 result = d .to_dict ()
@@ -132,7 +133,7 @@ def test_to_dict_case_insensitive_grouping(self, cls):
132133 assert result [key_a ] == [1 , 2 ]
133134 assert result [key_b ] == [3 ]
134135
135- def test_to_dict_proxy_mutation_isolation (self , cls , multidict_module ) :
136+ def test_to_dict_proxy_mutation_isolation (self , cls : Any , multidict_module : Any ) -> None :
136137 """Test that modifying returned dict does not affect the proxy."""
137138 md = multidict_module .CIMultiDict ([("a" , 1 )])
138139 proxy = multidict_module .CIMultiDictProxy (md )
0 commit comments