1
1
from __future__ import annotations
2
2
3
- from collections import defaultdict
3
+ from collections import (
4
+ OrderedDict ,
5
+ defaultdict ,
6
+ )
4
7
from collections .abc import (
5
8
Callable ,
6
9
Hashable ,
@@ -3641,8 +3644,13 @@ def test_to_records() -> None:
3641
3644
3642
3645
def test_to_dict_simple () -> None :
3643
3646
check (assert_type (DF .to_dict (), dict [Hashable , Any ]), dict )
3644
- check (assert_type (DF .to_dict ("split" ), dict [Hashable , Any ]), dict )
3645
3647
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 )
3646
3654
3647
3655
if TYPE_CHECKING_INVALID_USAGE :
3648
3656
@@ -3661,69 +3669,58 @@ def test(mapping: Mapping) -> None: # pyright: ignore[reportUnusedFunction]
3661
3669
assert_type (DF .to_dict ("tight" , into = defaultdict ), Never )
3662
3670
3663
3671
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]"""
3666
3674
3667
3675
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 )
3669
3677
3670
3678
check (
3671
- assert_type (data .to_dict (into = target ), defaultdict [Hashable , list [ Any ] ]),
3679
+ assert_type (data .to_dict (into = target ), defaultdict [Any , list ]),
3672
3680
defaultdict ,
3681
+ tuple ,
3673
3682
)
3674
3683
check (
3675
- assert_type (
3684
+ assert_type ( # type: ignore[assert-type]
3676
3685
data .to_dict ("index" , into = target ),
3677
- MutableMapping [Hashable , defaultdict [Hashable , list [ Any ] ]],
3686
+ defaultdict [Hashable , dict [Hashable , Any ]],
3678
3687
),
3679
3688
defaultdict ,
3680
3689
)
3681
3690
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 ]),
3685
3692
defaultdict ,
3693
+ str ,
3686
3694
)
3687
3695
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 ]]),
3691
3697
list ,
3698
+ defaultdict ,
3692
3699
)
3693
3700
3694
3701
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 `"""
3697
3704
3698
3705
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 )
3701
3706
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 )
3707
3708
check (
3708
3709
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 ]],
3711
3712
),
3712
- defaultdict ,
3713
+ OrderedDict ,
3713
3714
)
3714
3715
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 ,
3719
3719
)
3720
3720
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 ]),
3725
3722
list ,
3726
- defaultdict ,
3723
+ OrderedDict ,
3727
3724
)
3728
3725
3729
3726
@@ -4177,16 +4174,16 @@ def test_to_dict_index() -> None:
4177
4174
dict ,
4178
4175
)
4179
4176
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
4181
4178
)
4182
4179
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
4184
4181
)
4185
4182
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
4187
4184
)
4188
4185
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
4190
4187
)
4191
4188
if TYPE_CHECKING_INVALID_USAGE :
4192
4189
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