Skip to content

Commit ffa2396

Browse files
authored
TYP: dict, Mapping, etc. (#1537)
* dict * mapping * missing cases * relax input
1 parent 7981f2c commit ffa2396

File tree

13 files changed

+92
-62
lines changed

13 files changed

+92
-62
lines changed

pandas-stubs/_testing/__init__.pyi

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ def assert_almost_equal(
4040
atol: float = 1e-8,
4141
**kwargs: Any,
4242
) -> None: ...
43-
def assert_dict_equal(left: dict, right: dict, compare_keys: bool = True) -> None: ...
4443
def assert_index_equal(
4544
left: Index,
4645
right: Index,

pandas-stubs/_typing.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ Level: TypeAlias = Hashable
182182
Shape: TypeAlias = tuple[int, ...]
183183
Suffixes: TypeAlias = tuple[str | None, str | None] | list[str | None]
184184
Ordered: TypeAlias = bool | None
185-
JSONSerializable: TypeAlias = PythonScalar | list[Any] | dict
185+
JSONSerializable: TypeAlias = PythonScalar | list[Any] | dict[str, Any]
186186
Frequency: TypeAlias = str | BaseOffset
187187
PeriodFrequency: TypeAlias = (
188188
str

pandas-stubs/core/computation/eval.pyi

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
from collections.abc import Mapping
1+
from collections.abc import (
2+
Mapping,
3+
MutableSequence,
4+
)
25
from typing import (
36
Any,
47
Literal,
@@ -21,7 +24,7 @@ def eval(
2124
engine: Literal["python", "numexpr"] | None = ...,
2225
local_dict: dict[str, Any] | None = None,
2326
global_dict: dict[str, Any] | None = None,
24-
resolvers: list[Mapping] | None = ...,
27+
resolvers: MutableSequence[Mapping[Any, Any]] | None = ...,
2528
level: int = 0,
2629
target: object | None = None,
2730
inplace: bool = False,

pandas-stubs/core/frame.pyi

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ from collections.abc import (
1313
Iterator,
1414
Mapping,
1515
MutableMapping,
16+
MutableSequence,
1617
Sequence,
1718
)
1819
import datetime as dt
@@ -507,7 +508,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
507508
self,
508509
orient: Literal["index"],
509510
*,
510-
into: defaultdict,
511+
into: defaultdict[Any, Any],
511512
index: Literal[True] = True,
512513
) -> defaultdict[Hashable, dict[Hashable, Any]]: ...
513514
@overload
@@ -555,7 +556,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
555556
self,
556557
orient: Literal["split", "tight"],
557558
*,
558-
into: MutableMapping | type[MutableMapping],
559+
into: MutableMapping[Any, Any] | type[MutableMapping],
559560
index: bool = ...,
560561
) -> MutableMapping[str, list[Any]]: ...
561562
@overload
@@ -917,7 +918,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
917918
engine: Literal["python", "numexpr"] | None = ...,
918919
local_dict: dict[_str, Any] | None = ...,
919920
global_dict: dict[_str, Any] | None = ...,
920-
resolvers: list[Mapping] | None = ...,
921+
resolvers: MutableSequence[Mapping[Any, Any]] | None = ...,
921922
level: int = ...,
922923
target: object | None = ...,
923924
inplace: Literal[True],
@@ -932,7 +933,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
932933
engine: Literal["python", "numexpr"] | None = ...,
933934
local_dict: dict[_str, Any] | None = ...,
934935
global_dict: dict[_str, Any] | None = ...,
935-
resolvers: list[Mapping] | None = ...,
936+
resolvers: MutableSequence[Mapping[Any, Any]] | None = ...,
936937
level: int = ...,
937938
target: object | None = ...,
938939
) -> Self: ...
@@ -1032,7 +1033,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
10321033
@overload
10331034
def fillna(
10341035
self,
1035-
value: Scalar | NAType | dict | Series | DataFrame | None = ...,
1036+
value: Scalar | NAType | dict[Any, Any] | Series | DataFrame | None = ...,
10361037
*,
10371038
axis: Axis | None = ...,
10381039
limit: int = ...,
@@ -1042,7 +1043,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
10421043
@overload
10431044
def fillna(
10441045
self,
1045-
value: Scalar | NAType | dict | Series | DataFrame | None = ...,
1046+
value: Scalar | NAType | dict[Any, Any] | Series | DataFrame | None = ...,
10461047
*,
10471048
axis: Axis | None = ...,
10481049
limit: int = ...,
@@ -1725,7 +1726,10 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
17251726
validate: MergeValidate | None = None,
17261727
) -> Self: ...
17271728
def round(
1728-
self, decimals: int | dict | Series = ..., *args: Any, **kwargs: Any
1729+
self,
1730+
decimals: int | dict[Any, Any] | Series = ...,
1731+
*args: Any,
1732+
**kwargs: Any,
17291733
) -> Self: ...
17301734
def corr(
17311735
self,
@@ -2675,7 +2679,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
26752679
compression: CompressionOptions = ...,
26762680
index: _bool | None = ...,
26772681
indent: int | None = ...,
2678-
storage_options: dict | None = ...,
2682+
storage_options: dict[Any, Any] | None = ...,
26792683
mode: Literal["a"],
26802684
) -> None: ...
26812685
@overload
@@ -2693,7 +2697,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
26932697
compression: CompressionOptions = ...,
26942698
index: _bool | None = ...,
26952699
indent: int | None = ...,
2696-
storage_options: dict | None = ...,
2700+
storage_options: dict[Any, Any] | None = ...,
26972701
mode: Literal["a"],
26982702
) -> _str: ...
26992703
@overload
@@ -2711,7 +2715,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
27112715
compression: CompressionOptions = ...,
27122716
index: _bool | None = ...,
27132717
indent: int | None = ...,
2714-
storage_options: dict | None = ...,
2718+
storage_options: dict[Any, Any] | None = ...,
27152719
mode: Literal["w"] = ...,
27162720
) -> _str: ...
27172721
@overload
@@ -2729,7 +2733,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
27292733
compression: CompressionOptions = ...,
27302734
index: _bool | None = ...,
27312735
indent: int | None = ...,
2732-
storage_options: dict | None = ...,
2736+
storage_options: dict[Any, Any] | None = ...,
27332737
mode: Literal["w"] = ...,
27342738
) -> None: ...
27352739
@overload

pandas-stubs/core/groupby/generic.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ _TT = TypeVar("_TT", bound=Literal[True, False])
219219
class DFCallable1(Protocol[P]): # ty: ignore[invalid-argument-type]
220220
def __call__(
221221
self, df: DataFrame, /, *args: P.args, **kwargs: P.kwargs
222-
) -> Scalar | list[Any] | dict: ...
222+
) -> Scalar | list[Any] | dict[Hashable, Any]: ...
223223

224224
class DFCallable2(Protocol[P]): # ty: ignore[invalid-argument-type]
225225
def __call__(

pandas-stubs/core/reshape/pivot.pyi

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ from pandas._typing import (
3535
_PivotAggCallable: TypeAlias = Callable[[Series], ScalarT]
3636

3737
_PivotAggFunc: TypeAlias = (
38-
_PivotAggCallable
38+
_PivotAggCallable[ScalarT]
3939
| np.ufunc
4040
| Literal["mean", "sum", "count", "min", "max", "median", "std", "var"]
4141
)
@@ -67,11 +67,17 @@ _Values: TypeAlias = SequenceNotStr[Any] | _ExtendedAnyArrayLike
6767
@overload
6868
def pivot_table(
6969
data: DataFrame,
70-
values: _PivotTableValuesTypes = None,
71-
index: _PivotTableIndexTypes = None,
72-
columns: _PivotTableColumnsTypes = None,
70+
values: _PivotTableValuesTypes[
71+
Hashable # ty: ignore[invalid-type-arguments]
72+
] = None,
73+
index: _PivotTableIndexTypes[Hashable] = None, # ty: ignore[invalid-type-arguments]
74+
columns: _PivotTableColumnsTypes[
75+
Hashable # ty: ignore[invalid-type-arguments]
76+
] = None,
7377
aggfunc: (
74-
_PivotAggFunc | Sequence[_PivotAggFunc] | Mapping[Hashable, _PivotAggFunc]
78+
_PivotAggFunc[Scalar]
79+
| Sequence[_PivotAggFunc[Scalar]]
80+
| Mapping[Any, _PivotAggFunc[Scalar]]
7581
) = "mean",
7682
fill_value: Scalar | None = None,
7783
margins: bool = False,
@@ -85,12 +91,20 @@ def pivot_table(
8591
@overload
8692
def pivot_table(
8793
data: DataFrame,
88-
values: _PivotTableValuesTypes = None,
94+
values: _PivotTableValuesTypes[
95+
Hashable # ty: ignore[invalid-type-arguments]
96+
] = None,
8997
*,
9098
index: Grouper,
91-
columns: _PivotTableColumnsTypes | np_ndarray | Index[Any] = None,
99+
columns: (
100+
_PivotTableColumnsTypes[Hashable] # ty: ignore[invalid-type-arguments]
101+
| np_ndarray
102+
| Index[Any]
103+
) = None,
92104
aggfunc: (
93-
_PivotAggFunc | Sequence[_PivotAggFunc] | Mapping[Hashable, _PivotAggFunc]
105+
_PivotAggFunc[Scalar]
106+
| Sequence[_PivotAggFunc[Scalar]]
107+
| Mapping[Any, _PivotAggFunc[Scalar]]
94108
) = "mean",
95109
fill_value: Scalar | None = None,
96110
margins: bool = False,
@@ -102,12 +116,20 @@ def pivot_table(
102116
@overload
103117
def pivot_table(
104118
data: DataFrame,
105-
values: _PivotTableValuesTypes = None,
106-
index: _PivotTableIndexTypes | np_ndarray | Index[Any] = None,
119+
values: _PivotTableValuesTypes[
120+
Hashable # ty: ignore[invalid-type-arguments]
121+
] = None,
122+
index: (
123+
_PivotTableIndexTypes[Hashable] # ty: ignore[invalid-type-arguments]
124+
| np_ndarray
125+
| Index[Any]
126+
) = None,
107127
*,
108128
columns: Grouper,
109129
aggfunc: (
110-
_PivotAggFunc | Sequence[_PivotAggFunc] | Mapping[Hashable, _PivotAggFunc]
130+
_PivotAggFunc[Scalar]
131+
| Sequence[_PivotAggFunc[Scalar]]
132+
| Mapping[Any, _PivotAggFunc[Scalar]]
111133
) = "mean",
112134
fill_value: Scalar | None = None,
113135
margins: bool = False,

pandas-stubs/core/series.pyi

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
776776
def to_dict(self, *, into: type[dict] = ...) -> dict[Any, S1]: ...
777777
@overload
778778
def to_dict(
779-
self, *, into: type[MutableMapping] | MutableMapping
779+
self, *, into: type[MutableMapping] | MutableMapping[Any, Any]
780780
) -> MutableMapping[Hashable, S1]: ...
781781
def to_frame(self, name: object | None = ...) -> DataFrame: ...
782782
@overload
@@ -1104,7 +1104,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
11041104
def unstack(
11051105
self,
11061106
level: IndexLabel = -1,
1107-
fill_value: int | _str | dict | None = None,
1107+
fill_value: int | _str | dict[Any, Any] | None = None,
11081108
sort: _bool = True,
11091109
) -> DataFrame: ...
11101110
@overload
@@ -1170,7 +1170,13 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
11701170
def apply(
11711171
self,
11721172
func: Callable[
1173-
..., Scalar | Sequence[Any] | AbstractSet[Any] | Mapping | NAType | None
1173+
...,
1174+
Scalar
1175+
| Sequence[Any]
1176+
| AbstractSet[Any]
1177+
| Mapping[Any, Any]
1178+
| NAType
1179+
| None,
11741180
],
11751181
convertDType: _bool = ...,
11761182
args: tuple[Any, ...] = ...,
@@ -1258,7 +1264,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
12581264
@overload
12591265
def fillna(
12601266
self,
1261-
value: Scalar | NAType | dict | Series[S1] | DataFrame | None = ...,
1267+
value: Scalar | NAType | dict[Any, Any] | Series[S1] | DataFrame | None = ...,
12621268
*,
12631269
axis: AxisIndex = ...,
12641270
limit: int | None = ...,
@@ -1267,7 +1273,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
12671273
@overload
12681274
def fillna(
12691275
self,
1270-
value: Scalar | NAType | dict | Series[S1] | DataFrame | None = ...,
1276+
value: Scalar | NAType | dict[Any, Any] | Series[S1] | DataFrame | None = ...,
12711277
*,
12721278
axis: AxisIndex = ...,
12731279
limit: int | None = ...,
@@ -4637,7 +4643,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
46374643
def rename_axis(
46384644
self,
46394645
*,
4640-
index: Scalar | ListLike | Callable[..., Any] | dict | None = ...,
4646+
index: Scalar | ListLike | Callable[..., Any] | dict[Any, Any] | None = ...,
46414647
copy: _bool = ...,
46424648
inplace: Literal[True],
46434649
) -> None: ...
@@ -4646,7 +4652,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
46464652
def rename_axis(
46474653
self,
46484654
*,
4649-
index: Scalar | ListLike | Callable[..., Any] | dict | None = ...,
4655+
index: Scalar | ListLike | Callable[..., Any] | dict[Any, Any] | None = ...,
46504656
copy: _bool = ...,
46514657
inplace: Literal[False] = False,
46524658
) -> Self: ...

pandas-stubs/core/tools/timedeltas.pyi

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from collections.abc import Sequence
21
from datetime import timedelta
32
from typing import overload
43

@@ -28,14 +27,7 @@ def to_timedelta(
2827
) -> Series[Timedelta]: ...
2928
@overload
3029
def to_timedelta(
31-
arg: (
32-
SequenceNotStr
33-
| Sequence[float | timedelta]
34-
| tuple[str | float | timedelta, ...]
35-
| range
36-
| ArrayLike
37-
| Index
38-
),
30+
arg: SequenceNotStr[str | float | timedelta] | range | ArrayLike | Index,
3931
unit: TimeDeltaUnitChoices | None = ...,
4032
errors: RaiseCoerce = ...,
4133
) -> TimedeltaIndex: ...

pandas-stubs/io/clipboards.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def read_clipboard(
3636
names: ListLikeHashable | None = ...,
3737
index_col: int | str | Sequence[str | int] | Literal[False] | None = ...,
3838
usecols: UsecolsArgType = ...,
39-
dtype: DtypeArg | defaultdict | None = ...,
39+
dtype: DtypeArg | defaultdict[Any, Any] | None = ...,
4040
engine: CSVEngine | None = ...,
4141
converters: dict[int | str, Callable[[str], Any]] = ...,
4242
true_values: list[str] = ...,
@@ -95,7 +95,7 @@ def read_clipboard(
9595
names: ListLikeHashable | None = ...,
9696
index_col: int | str | Sequence[str | int] | Literal[False] | None = ...,
9797
usecols: UsecolsArgType = ...,
98-
dtype: DtypeArg | defaultdict | None = ...,
98+
dtype: DtypeArg | defaultdict[Any, Any] | None = ...,
9999
engine: CSVEngine | None = ...,
100100
converters: dict[int | str, Callable[[str], Any]] = ...,
101101
true_values: list[str] = ...,
@@ -154,7 +154,7 @@ def read_clipboard(
154154
names: ListLikeHashable | None = ...,
155155
index_col: int | str | Sequence[str | int] | Literal[False] | None = ...,
156156
usecols: UsecolsArgType = ...,
157-
dtype: DtypeArg | defaultdict | None = ...,
157+
dtype: DtypeArg | defaultdict[Any, Any] | None = ...,
158158
engine: CSVEngine | None = ...,
159159
converters: dict[int | str, Callable[[str], Any]] = ...,
160160
true_values: list[str] = ...,

pandas-stubs/io/json/_normalize.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
from typing import Any
2+
13
from pandas import DataFrame
24

35
from pandas._typing import IgnoreRaise
46

57
def json_normalize(
6-
data: dict | list[dict],
8+
data: dict[str, Any] | list[dict[str, Any]],
79
record_path: str | list[str] | None = None,
810
meta: str | list[str | list[str]] | None = None,
911
meta_prefix: str | None = None,

0 commit comments

Comments
 (0)