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,59 @@ 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 )
3706
+ target = OrderedDict
3701
3707
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]
3707
3709
check (
3708
3710
assert_type (
3709
3711
data .to_dict ("index" , into = target ),
3710
- MutableMapping [Hashable , defaultdict [ tuple [ str , str ], list [ int ] ]],
3712
+ OrderedDict [Hashable , dict [ Hashable , Any ]],
3711
3713
),
3712
- defaultdict ,
3714
+ OrderedDict ,
3713
3715
)
3714
3716
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 ,
3719
3720
)
3720
3721
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]
3725
3723
list ,
3726
- defaultdict ,
3724
+ OrderedDict ,
3727
3725
)
3728
3726
3729
3727
@@ -4177,16 +4175,16 @@ def test_to_dict_index() -> None:
4177
4175
dict ,
4178
4176
)
4179
4177
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
4181
4179
)
4182
4180
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
4184
4182
)
4185
4183
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
4187
4185
)
4188
4186
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
4190
4188
)
4191
4189
if TYPE_CHECKING_INVALID_USAGE :
4192
4190
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