Skip to content

Commit 94f0d80

Browse files
committed
fix: work in progress
1 parent f340905 commit 94f0d80

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

pandas-stubs/core/frame.pyi

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ from typing import (
1919
Generic,
2020
Literal,
2121
NoReturn,
22+
TypeVar,
2223
final,
2324
overload,
2425
)
@@ -165,6 +166,8 @@ from pandas._typing import (
165166
from pandas.io.formats.style import Styler
166167
from pandas.plotting import PlotAccessor
167168

169+
_T_MUTABLE_MAPPING = TypeVar("_T_MUTABLE_MAPPING", bound=MutableMapping, covariant=True)
170+
168171
class _iLocIndexerFrame(_iLocIndexer, Generic[_T]):
169172
@overload
170173
def __getitem__(self, idx: tuple[int, int]) -> Scalar: ...
@@ -396,9 +399,9 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
396399
self,
397400
orient: Literal["records"],
398401
*,
399-
into: MutableMapping | type[MutableMapping],
402+
into: _T_MUTABLE_MAPPING | type[_T_MUTABLE_MAPPING],
400403
index: Literal[True] = ...,
401-
) -> list[MutableMapping[Hashable, Any]]: ...
404+
) -> list[_T_MUTABLE_MAPPING]: ...
402405
@overload
403406
def to_dict(
404407
self,
@@ -412,33 +415,33 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
412415
self,
413416
orient: Literal["dict", "list", "series", "index"],
414417
*,
415-
into: MutableMapping | type[MutableMapping],
418+
into: _T_MUTABLE_MAPPING | type[_T_MUTABLE_MAPPING],
416419
index: Literal[True] = ...,
417-
) -> MutableMapping[Hashable, Any]: ...
420+
) -> _T_MUTABLE_MAPPING: ...
418421
@overload
419422
def to_dict(
420423
self,
421424
orient: Literal["split", "tight"],
422425
*,
423-
into: MutableMapping | type[MutableMapping],
426+
into: _T_MUTABLE_MAPPING | type[_T_MUTABLE_MAPPING],
424427
index: bool = ...,
425-
) -> MutableMapping[Hashable, Any]: ...
428+
) -> _T_MUTABLE_MAPPING: ...
426429
@overload
427430
def to_dict(
428431
self,
429432
orient: Literal["dict", "list", "series", "index"] = ...,
430433
*,
431-
into: MutableMapping | type[MutableMapping],
434+
into: _T_MUTABLE_MAPPING | type[_T_MUTABLE_MAPPING],
432435
index: Literal[True] = ...,
433-
) -> MutableMapping[Hashable, Any]: ...
436+
) -> _T_MUTABLE_MAPPING: ...
434437
@overload
435438
def to_dict(
436439
self,
437440
orient: Literal["split", "tight"] = ...,
438441
*,
439-
into: MutableMapping | type[MutableMapping],
442+
into: _T_MUTABLE_MAPPING | type[_T_MUTABLE_MAPPING],
440443
index: bool = ...,
441-
) -> MutableMapping[Hashable, Any]: ...
444+
) -> _T_MUTABLE_MAPPING: ...
442445
@overload
443446
def to_dict(
444447
self,

tests/test_frame.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3643,20 +3643,20 @@ def test_to_dict() -> None:
36433643
check(assert_type(DF.to_dict("split"), dict[Hashable, Any]), dict)
36443644

36453645
target: MutableMapping = defaultdict(list)
3646-
check(
3647-
assert_type(DF.to_dict(into=target), MutableMapping[Hashable, Any]), defaultdict
3648-
)
3646+
check(assert_type(DF.to_dict(into=target), defaultdict[Any, list]), defaultdict)
3647+
# check(
3648+
# assert_type(DF.to_dict("index", into=target), MutableMapping[Hashable, MutableMapping[Hashable, Any]]), defaultdict
3649+
# )
3650+
36493651
target = defaultdict(list)
36503652
check(
3651-
assert_type(DF.to_dict("tight", into=target), MutableMapping[Hashable, Any]),
3653+
assert_type(DF.to_dict("tight", into=target), defaultdict[Any, list]),
36523654
defaultdict,
36533655
)
36543656
target = defaultdict(list)
36553657
check(assert_type(DF.to_dict("records"), list[dict[Hashable, Any]]), list)
36563658
check(
3657-
assert_type(
3658-
DF.to_dict("records", into=target), list[MutableMapping[Hashable, Any]]
3659-
),
3659+
assert_type(DF.to_dict("records", into=target), list[defaultdict[Any, list]]),
36603660
list,
36613661
)
36623662
if TYPE_CHECKING_INVALID_USAGE:

0 commit comments

Comments
 (0)