11from __future__ import annotations
22
3- from collections import defaultdict
3+ from collections import (
4+ OrderedDict ,
5+ defaultdict ,
6+ )
47from collections .abc import (
58 Callable ,
69 Hashable ,
@@ -3641,8 +3644,13 @@ def test_to_records() -> None:
36413644
36423645def test_to_dict_simple () -> None :
36433646 check (assert_type (DF .to_dict (), dict [Hashable , Any ]), dict )
3644- check (assert_type (DF .to_dict ("split" ), dict [Hashable , Any ]), dict )
36453647 check (assert_type (DF .to_dict ("records" ), list [dict [Hashable , Any ]]), list )
3648+ check (assert_type (DF .to_dict ("index" ), dict [Hashable , dict [Hashable , Any ]]), dict )
3649+ check (assert_type (DF .to_dict ("dict" ), dict [Hashable , Any ]), dict )
3650+ check (assert_type (DF .to_dict ("list" ), dict [Hashable , Any ]), dict )
3651+ check (assert_type (DF .to_dict ("series" ), dict [Hashable , Any ]), dict )
3652+ check (assert_type (DF .to_dict ("split" ), dict [str , list ]), dict , str )
3653+ check (assert_type (DF .to_dict ("tight" ), dict [str , list ]), dict , str )
36463654
36473655 if TYPE_CHECKING_INVALID_USAGE :
36483656
@@ -3661,69 +3669,58 @@ def test(mapping: Mapping) -> None: # pyright: ignore[reportUnusedFunction]
36613669 assert_type (DF .to_dict ("tight" , into = defaultdict ), Never )
36623670
36633671
3664- def test_to_dict_into_defaultdict_any () -> None :
3665- """Test DataFrame.to_dict with `into= defaultdict[Any, list]` """
3672+ def test_to_dict_into_defaultdict () -> None :
3673+ """Test DataFrame.to_dict with `into` is an instance of defaultdict[Any, list]"""
36663674
36673675 data = pd .DataFrame ({("str" , "rts" ): [[1 , 2 , 4 ], [2 , 3 ], [3 ]]})
3668- target : defaultdict [Hashable , list [ Any ] ] = defaultdict (list )
3676+ target : defaultdict [Any , list ] = defaultdict (list )
36693677
36703678 check (
3671- assert_type (data .to_dict (into = target ), defaultdict [Hashable , list [ Any ] ]),
3679+ assert_type (data .to_dict (into = target ), defaultdict [Any , list ]),
36723680 defaultdict ,
3681+ tuple ,
36733682 )
36743683 check (
3675- assert_type (
3684+ assert_type ( # type: ignore[assert-type]
36763685 data .to_dict ("index" , into = target ),
3677- MutableMapping [Hashable , defaultdict [Hashable , list [ Any ] ]],
3686+ defaultdict [Hashable , dict [Hashable , Any ]],
36783687 ),
36793688 defaultdict ,
36803689 )
36813690 check (
3682- assert_type (
3683- data .to_dict ("tight" , into = target ), defaultdict [Hashable , list [Any ]]
3684- ),
3691+ assert_type (data .to_dict ("tight" , into = target ), MutableMapping [str , list ]),
36853692 defaultdict ,
3693+ str ,
36863694 )
36873695 check (
3688- assert_type (
3689- data .to_dict ("records" , into = target ), list [defaultdict [Hashable , list [Any ]]]
3690- ),
3696+ assert_type (data .to_dict ("records" , into = target ), list [defaultdict [Any , list ]]),
36913697 list ,
3698+ defaultdict ,
36923699 )
36933700
36943701
3695- def test_to_dict_into_defaultdict_typed () -> None :
3696- """Test DataFrame.to_dict with `into=defaultdict[tuple[str, str], list[int]] `"""
3702+ def test_to_dict_into_ordered_dict () -> None :
3703+ """Test DataFrame.to_dict with `into=OrderedDict `"""
36973704
36983705 data = pd .DataFrame ({("str" , "rts" ): [[1 , 2 , 4 ], [2 , 3 ], [3 ]]})
3699- target : defaultdict [tuple [str , str ], list [int ]] = defaultdict (list )
3700- target [("str" , "rts" )].append (1 )
37013706
3702- check (
3703- assert_type (data .to_dict (into = target ), defaultdict [tuple [str , str ], list [int ]]),
3704- defaultdict ,
3705- tuple ,
3706- )
3707+ check (assert_type (data .to_dict (into = OrderedDict ), OrderedDict ), OrderedDict , tuple )
37073708 check (
37083709 assert_type (
3709- data .to_dict ("index" , into = target ),
3710- MutableMapping [Hashable , defaultdict [ tuple [ str , str ], list [ int ] ]],
3710+ data .to_dict ("index" , into = OrderedDict ),
3711+ OrderedDict [Hashable , dict [ Hashable , Any ]],
37113712 ),
3712- defaultdict ,
3713+ OrderedDict ,
37133714 )
37143715 check (
3715- assert_type (
3716- data .to_dict ("tight" , into = target ), defaultdict [tuple [str , str ], list [int ]]
3717- ),
3718- defaultdict ,
3716+ assert_type (data .to_dict ("tight" , into = OrderedDict ), MutableMapping [str , list ]),
3717+ OrderedDict ,
3718+ str ,
37193719 )
37203720 check (
3721- assert_type (
3722- data .to_dict ("records" , into = target ),
3723- list [defaultdict [tuple [str , str ], list [int ]]],
3724- ),
3721+ assert_type (data .to_dict ("records" , into = OrderedDict ), list [OrderedDict ]),
37253722 list ,
3726- defaultdict ,
3723+ OrderedDict ,
37273724 )
37283725
37293726
@@ -4177,16 +4174,16 @@ def test_to_dict_index() -> None:
41774174 dict ,
41784175 )
41794176 check (
4180- assert_type (df .to_dict (orient = "split" , index = True ), dict [Hashable , Any ]), dict
4177+ assert_type (df .to_dict (orient = "split" , index = True ), dict [str , list ]), dict , str
41814178 )
41824179 check (
4183- assert_type (df .to_dict (orient = "tight" , index = True ), dict [Hashable , Any ]), dict
4180+ assert_type (df .to_dict (orient = "tight" , index = True ), dict [str , list ]), dict , str
41844181 )
41854182 check (
4186- assert_type (df .to_dict (orient = "tight" , index = False ), dict [Hashable , Any ]), dict
4183+ assert_type (df .to_dict (orient = "tight" , index = False ), dict [str , list ]), dict , str
41874184 )
41884185 check (
4189- assert_type (df .to_dict (orient = "split" , index = False ), dict [Hashable , Any ]), dict
4186+ assert_type (df .to_dict (orient = "split" , index = False ), dict [str , list ]), dict , str
41904187 )
41914188 if TYPE_CHECKING_INVALID_USAGE :
41924189 check (assert_type (df .to_dict (orient = "records" , index = False ), list [dict [Hashable , Any ]]), list ) # type: ignore[assert-type, call-overload] # pyright: ignore[reportArgumentType,reportAssertTypeFailure,reportCallIssue]
0 commit comments