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,59 @@ 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 (list ) # type: ignore[var-annotated]
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 ]), # type: ignore[assert-type]
36723680 defaultdict ,
3681+ tuple ,
36733682 )
36743683 check (
3675- assert_type (
3676- data .to_dict ("index" , into = target ),
3677- MutableMapping [Hashable , defaultdict [Hashable , list [ Any ] ]],
3684+ assert_type ( # type: ignore[assert-type]
3685+ data .to_dict ("index" , into = target ), # type: ignore[call-overload]
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 ]), # type: ignore[assert-type, call-overload]
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 ]]), # type: ignore[assert-type, call-overload]
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 )
3706+ target = OrderedDict
37013707
3702- check (
3703- assert_type (data .to_dict (into = target ), defaultdict [tuple [str , str ], list [int ]]),
3704- defaultdict ,
3705- tuple ,
3706- )
3708+ check (assert_type (data .to_dict (into = target ), OrderedDict ), OrderedDict , tuple ) # type: ignore[assert-type]
37073709 check (
37083710 assert_type (
37093711 data .to_dict ("index" , into = target ),
3710- MutableMapping [Hashable , defaultdict [ tuple [ str , str ], list [ int ] ]],
3712+ OrderedDict [Hashable , dict [ Hashable , Any ]],
37113713 ),
3712- defaultdict ,
3714+ OrderedDict ,
37133715 )
37143716 check (
3715- assert_type (
3716- data .to_dict ("tight" , into = target ), defaultdict [tuple [str , str ], list [int ]]
3717- ),
3718- defaultdict ,
3717+ assert_type (data .to_dict ("tight" , into = target ), MutableMapping [str , list ]),
3718+ OrderedDict ,
3719+ str ,
37193720 )
37203721 check (
3721- assert_type (
3722- data .to_dict ("records" , into = target ),
3723- list [defaultdict [tuple [str , str ], list [int ]]],
3724- ),
3722+ assert_type (data .to_dict ("records" , into = target ), list [OrderedDict ]), # type: ignore[assert-type]
37253723 list ,
3726- defaultdict ,
3724+ OrderedDict ,
37273725 )
37283726
37293727
@@ -4177,16 +4175,16 @@ def test_to_dict_index() -> None:
41774175 dict ,
41784176 )
41794177 check (
4180- assert_type (df .to_dict (orient = "split" , index = True ), dict [Hashable , Any ]), dict
4178+ assert_type (df .to_dict (orient = "split" , index = True ), dict [str , list ]), dict , str
41814179 )
41824180 check (
4183- assert_type (df .to_dict (orient = "tight" , index = True ), dict [Hashable , Any ]), dict
4181+ assert_type (df .to_dict (orient = "tight" , index = True ), dict [str , list ]), dict , str
41844182 )
41854183 check (
4186- assert_type (df .to_dict (orient = "tight" , index = False ), dict [Hashable , Any ]), dict
4184+ assert_type (df .to_dict (orient = "tight" , index = False ), dict [str , list ]), dict , str
41874185 )
41884186 check (
4189- assert_type (df .to_dict (orient = "split" , index = False ), dict [Hashable , Any ]), dict
4187+ assert_type (df .to_dict (orient = "split" , index = False ), dict [str , list ]), dict , str
41904188 )
41914189 if TYPE_CHECKING_INVALID_USAGE :
41924190 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