Skip to content

Commit e7d8661

Browse files
authored
TYP: to_dict use MutableMapping (#796)
* TYP: to_dict use MutableMapping * remove deprecation warning * mypy
1 parent bddb20b commit e7d8661

File tree

5 files changed

+106
-110
lines changed

5 files changed

+106
-110
lines changed

pandas-stubs/core/frame.pyi

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ from collections.abc import (
44
Iterable,
55
Iterator,
66
Mapping,
7+
MutableMapping,
78
Sequence,
89
)
910
import datetime
@@ -270,61 +271,67 @@ class DataFrame(NDFrame, OpsMixin):
270271
na_value: Scalar = ...,
271272
) -> np.ndarray: ...
272273
@overload
273-
def to_dict(
274+
def to_dict( # type: ignore[misc]
274275
self,
275276
orient: Literal["records"],
276-
into: Mapping | type[Mapping],
277+
*,
278+
into: MutableMapping | type[MutableMapping],
277279
index: Literal[True] = ...,
278-
) -> list[Mapping[Hashable, Any]]: ...
280+
) -> list[MutableMapping[Hashable, Any]]: ...
279281
@overload
280282
def to_dict(
281283
self,
282284
orient: Literal["records"],
283-
into: None = ...,
285+
*,
286+
into: type[dict] = ...,
284287
index: Literal[True] = ...,
285288
) -> list[dict[Hashable, Any]]: ...
286289
@overload
287-
def to_dict(
290+
def to_dict( # type: ignore[misc]
288291
self,
289292
orient: Literal["dict", "list", "series", "index"],
290-
into: Mapping | type[Mapping],
293+
*,
294+
into: MutableMapping | type[MutableMapping],
291295
index: Literal[True] = ...,
292-
) -> Mapping[Hashable, Any]: ...
296+
) -> MutableMapping[Hashable, Any]: ...
293297
@overload
294-
def to_dict(
298+
def to_dict( # type: ignore[misc]
295299
self,
296300
orient: Literal["split", "tight"],
297-
into: Mapping | type[Mapping],
301+
*,
302+
into: MutableMapping | type[MutableMapping],
298303
index: bool = ...,
299-
) -> Mapping[Hashable, Any]: ...
304+
) -> MutableMapping[Hashable, Any]: ...
300305
@overload
301-
def to_dict(
306+
def to_dict( # type: ignore[misc]
302307
self,
303308
orient: Literal["dict", "list", "series", "index"] = ...,
304309
*,
305-
into: Mapping | type[Mapping],
310+
into: MutableMapping | type[MutableMapping],
306311
index: Literal[True] = ...,
307-
) -> Mapping[Hashable, Any]: ...
312+
) -> MutableMapping[Hashable, Any]: ...
308313
@overload
309-
def to_dict(
314+
def to_dict( # type: ignore[misc]
310315
self,
311316
orient: Literal["split", "tight"] = ...,
312317
*,
313-
into: Mapping | type[Mapping],
318+
into: MutableMapping | type[MutableMapping],
314319
index: bool = ...,
315-
) -> Mapping[Hashable, Any]: ...
320+
) -> MutableMapping[Hashable, Any]: ...
316321
@overload
317322
def to_dict(
318323
self,
319324
orient: Literal["dict", "list", "series", "index"] = ...,
320-
into: None = ...,
325+
*,
326+
into: type[dict] = ...,
321327
index: Literal[True] = ...,
322328
) -> dict[Hashable, Any]: ...
323329
@overload
324330
def to_dict(
325331
self,
326332
orient: Literal["split", "tight"] = ...,
327-
into: None = ...,
333+
*,
334+
into: type[dict] = ...,
328335
index: bool = ...,
329336
) -> dict[Hashable, Any]: ...
330337
def to_gbq(

pandas-stubs/core/series.pyi

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ from collections.abc import (
44
Iterable,
55
Iterator,
66
Mapping,
7+
MutableMapping,
78
Sequence,
89
)
910
from datetime import (
@@ -535,9 +536,11 @@ class Series(IndexOpsMixin[S1], NDFrame):
535536
def items(self) -> Iterable[tuple[Hashable, S1]]: ...
536537
def keys(self) -> list: ...
537538
@overload
538-
def to_dict(self) -> dict[Any, S1]: ...
539+
def to_dict(self, *, into: type[dict] = ...) -> dict[Any, S1]: ...
539540
@overload
540-
def to_dict(self, into: type[Mapping] | Mapping) -> Mapping[Hashable, S1]: ...
541+
def to_dict(
542+
self, *, into: type[MutableMapping] | MutableMapping
543+
) -> MutableMapping[Hashable, S1]: ...
541544
def to_frame(self, name: object | None = ...) -> DataFrame: ...
542545
@overload
543546
def groupby(

pyproject.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@ types-pytz = ">= 2022.1.1"
3535
numpy = { version = ">=1.26.0", python = "<3.13" }
3636

3737
[tool.poetry.dev-dependencies]
38-
mypy = "1.5.1"
38+
mypy = "1.6.0"
3939
pandas = "2.1.1"
4040
pyarrow = ">=10.0.1"
4141
pytest = ">=7.1.2"
42-
# pyright 1.1.329 has bug fixed in 1.1.330
43-
pyright = "1.1.328"
42+
pyright = ">=1.1.331"
4443
poethepoet = ">=0.16.5"
4544
loguru = ">=0.6.0"
4645
typing-extensions = ">=4.4.0"

tests/test_frame.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
Iterable,
77
Iterator,
88
Mapping,
9+
MutableMapping,
910
)
1011
import csv
1112
import datetime
@@ -2318,11 +2319,11 @@ def func() -> MyDataFrame[int]:
23182319
func()
23192320

23202321

2321-
def test_to_xarray():
2322+
def test_to_xarray() -> None:
23222323
check(assert_type(DF.to_xarray(), xr.Dataset), xr.Dataset)
23232324

23242325

2325-
def test_to_records():
2326+
def test_to_records() -> None:
23262327
check(assert_type(DF.to_records(False, "int8"), np.recarray), np.recarray)
23272328
check(
23282329
assert_type(DF.to_records(False, index_dtypes=np.int8), np.recarray),
@@ -2336,23 +2337,33 @@ def test_to_records():
23362337
)
23372338

23382339

2339-
def test_to_dict():
2340+
def test_to_dict() -> None:
23402341
check(assert_type(DF.to_dict(), dict[Hashable, Any]), dict)
23412342
check(assert_type(DF.to_dict("split"), dict[Hashable, Any]), dict)
23422343

2343-
target: Mapping = defaultdict(list)
2344-
check(assert_type(DF.to_dict(into=target), Mapping[Hashable, Any]), defaultdict)
2344+
target: MutableMapping = defaultdict(list)
2345+
check(
2346+
assert_type(DF.to_dict(into=target), MutableMapping[Hashable, Any]), defaultdict
2347+
)
23452348
target = defaultdict(list)
23462349
check(
2347-
assert_type(DF.to_dict("tight", into=target), Mapping[Hashable, Any]),
2350+
assert_type(DF.to_dict("tight", into=target), MutableMapping[Hashable, Any]),
23482351
defaultdict,
23492352
)
23502353
target = defaultdict(list)
23512354
check(assert_type(DF.to_dict("records"), list[dict[Hashable, Any]]), list)
23522355
check(
2353-
assert_type(DF.to_dict("records", into=target), list[Mapping[Hashable, Any]]),
2356+
assert_type(
2357+
DF.to_dict("records", into=target), list[MutableMapping[Hashable, Any]]
2358+
),
23542359
list,
23552360
)
2361+
if TYPE_CHECKING_INVALID_USAGE:
2362+
2363+
def test(mapping: Mapping) -> None: # pyright: ignore[reportUnusedFunction]
2364+
DF.to_dict( # type: ignore[call-overload]
2365+
into=mapping # pyright: ignore[reportGeneralTypeIssues]
2366+
)
23562367

23572368

23582369
def test_neg() -> None:

tests/test_io.py

Lines changed: 56 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ def test_hdf():
360360
check(assert_type(read_hdf(path), Union[DataFrame, Series]), DataFrame)
361361

362362

363-
def test_hdfstore():
363+
def test_hdfstore() -> None:
364364
with ensure_clean() as path:
365365
store = HDFStore(path, model="w")
366366
check(assert_type(store, HDFStore), HDFStore)
@@ -372,91 +372,67 @@ def test_hdfstore():
372372
assert_type(store.select("df", start=0, stop=1), Union[DataFrame, Series]),
373373
DataFrame,
374374
)
375-
with pytest_warns_bounded(
376-
DeprecationWarning,
377-
match="`alltrue` is deprecated as of NumPy 1.25.0",
378-
lower="1.24.99",
379-
version_str=np.__version__,
380-
):
381-
check(
382-
assert_type(
383-
store.select("df", where="index>=1"), Union[DataFrame, Series]
384-
),
385-
DataFrame,
386-
)
387-
check(
388-
assert_type(
389-
store.select("df", where=Term("index>=1")),
390-
Union[DataFrame, Series],
391-
),
392-
DataFrame,
393-
)
394-
check(
395-
assert_type(
396-
store.select("df", where=[Term("index>=1")]),
397-
Union[DataFrame, Series],
398-
),
399-
DataFrame,
400-
)
401-
check(assert_type(store.get("df"), Union[DataFrame, Series]), DataFrame)
402-
for key in store:
403-
check(assert_type(key, str), str)
404-
check(assert_type(store.close(), None), type(None))
375+
check(
376+
assert_type(store.select("df", where="index>=1"), Union[DataFrame, Series]),
377+
DataFrame,
378+
)
379+
check(
380+
assert_type(
381+
store.select("df", where=Term("index>=1")),
382+
Union[DataFrame, Series],
383+
),
384+
DataFrame,
385+
)
386+
check(
387+
assert_type(
388+
store.select("df", where=[Term("index>=1")]),
389+
Union[DataFrame, Series],
390+
),
391+
DataFrame,
392+
)
393+
check(assert_type(store.get("df"), Union[DataFrame, Series]), DataFrame)
394+
for key in store:
395+
check(assert_type(key, str), str)
396+
check(assert_type(store.close(), None), type(None))
405397

406-
store = HDFStore(path, model="r")
407-
check(
408-
assert_type(read_hdf(store, "df"), Union[DataFrame, Series]),
409-
DataFrame,
410-
)
411-
store.close()
398+
store = HDFStore(path, model="r")
399+
check(
400+
assert_type(read_hdf(store, "df"), Union[DataFrame, Series]),
401+
DataFrame,
402+
)
403+
store.close()
412404

413405

414-
def test_read_hdf_iterator():
415-
with pytest_warns_bounded(
416-
DeprecationWarning,
417-
match="`alltrue` is deprecated as of NumPy 1.25.0",
418-
lower="1.24.99",
419-
version_str=np.__version__,
420-
):
421-
with ensure_clean() as path:
422-
with pytest_warns_bounded(
423-
FutureWarning,
424-
r".*all arguments of to_hdf except for the argument 'path_or_buf' will be keyword-only",
425-
lower="2.1.99",
426-
):
427-
check(
428-
assert_type(DF.to_hdf(path, "df", format="table"), None), type(None)
429-
)
430-
ti = read_hdf(path, chunksize=1)
431-
check(assert_type(ti, TableIterator), TableIterator)
432-
ti.close()
406+
def test_read_hdf_iterator() -> None:
407+
with ensure_clean() as path:
408+
with pytest_warns_bounded(
409+
FutureWarning,
410+
r".*all arguments of to_hdf except for the argument 'path_or_buf' will be keyword-only",
411+
lower="2.1.99",
412+
):
413+
check(assert_type(DF.to_hdf(path, "df", format="table"), None), type(None))
414+
ti = read_hdf(path, chunksize=1)
415+
check(assert_type(ti, TableIterator), TableIterator)
416+
ti.close()
433417

434-
ti = read_hdf(path, "df", iterator=True)
435-
check(assert_type(ti, TableIterator), TableIterator)
436-
for _ in ti:
437-
pass
438-
ti.close()
418+
ti = read_hdf(path, "df", iterator=True)
419+
check(assert_type(ti, TableIterator), TableIterator)
420+
for _ in ti:
421+
pass
422+
ti.close()
439423

440424

441-
def test_hdf_context_manager():
442-
with pytest_warns_bounded(
443-
DeprecationWarning,
444-
match="`alltrue` is deprecated as of NumPy 1.25.0",
445-
lower="1.24.99",
446-
version_str=np.__version__,
447-
):
448-
with ensure_clean() as path:
449-
with pytest_warns_bounded(
450-
FutureWarning,
451-
r".*all arguments of to_hdf except for the argument 'path_or_buf' will be keyword-only",
452-
lower="2.1.99",
453-
):
454-
check(
455-
assert_type(DF.to_hdf(path, "df", format="table"), None), type(None)
456-
)
457-
with HDFStore(path, mode="r") as store:
458-
check(assert_type(store.is_open, bool), bool)
459-
check(assert_type(store.get("df"), Union[DataFrame, Series]), DataFrame)
425+
def test_hdf_context_manager() -> None:
426+
with ensure_clean() as path:
427+
with pytest_warns_bounded(
428+
FutureWarning,
429+
r".*all arguments of to_hdf except for the argument 'path_or_buf' will be keyword-only",
430+
lower="2.1.99",
431+
):
432+
check(assert_type(DF.to_hdf(path, "df", format="table"), None), type(None))
433+
with HDFStore(path, mode="r") as store:
434+
check(assert_type(store.is_open, bool), bool)
435+
check(assert_type(store.get("df"), Union[DataFrame, Series]), DataFrame)
460436

461437

462438
def test_hdf_series():

0 commit comments

Comments
 (0)