From d6592685321c6add64ff4bfd12c6984e5efa3d70 Mon Sep 17 00:00:00 2001 From: cmp0xff Date: Tue, 11 Nov 2025 13:48:09 +0100 Subject: [PATCH 01/10] fix(nightly): pandas-dev/pandas-stubs/#1462 pandas-dev/pandas#57064 pandas-dev/pandas#58690 pandas-dev/pandas#61942 pandas-dev/pandas#61952 --- tests/indexes/test_datetime_index.py | 29 ++-- tests/indexes/test_indexes.py | 47 +++--- tests/indexes/test_rangeindex.py | 47 ++---- tests/scalars/test_scalars.py | 228 +++++++++++++-------------- tests/series/test_series.py | 17 +- tests/test_frame.py | 80 ++++++---- tests/test_groupby.py | 3 +- tests/test_interval.py | 4 +- tests/test_resampler.py | 4 +- tests/test_string_accessors.py | 75 +++------ tests/test_timefuncs.py | 29 ++-- 11 files changed, 253 insertions(+), 310 deletions(-) diff --git a/tests/indexes/test_datetime_index.py b/tests/indexes/test_datetime_index.py index 3c87bb1bd..d91442944 100644 --- a/tests/indexes/test_datetime_index.py +++ b/tests/indexes/test_datetime_index.py @@ -8,7 +8,8 @@ from tests import ( check, - np_1darray, + np_1darray_bool, + np_1darray_intp, ) @@ -34,10 +35,10 @@ def test_index_relops() -> None: check(assert_type(data[idx > x], pd.DatetimeIndex), pd.DatetimeIndex) ind = pd.Index([1, 2, 3]) - check(assert_type(ind <= 2, np_1darray[np.bool]), np_1darray[np.bool]) - check(assert_type(ind < 2, np_1darray[np.bool]), np_1darray[np.bool]) - check(assert_type(ind >= 2, np_1darray[np.bool]), np_1darray[np.bool]) - check(assert_type(ind > 2, np_1darray[np.bool]), np_1darray[np.bool]) + check(assert_type(ind <= 2, np_1darray_bool), np_1darray_bool) + check(assert_type(ind < 2, np_1darray_bool), np_1darray_bool) + check(assert_type(ind >= 2, np_1darray_bool), np_1darray_bool) + check(assert_type(ind > 2, np_1darray_bool), np_1darray_bool) def test_datetime_index_constructor() -> None: @@ -92,14 +93,8 @@ def test_datetimeindex_shift() -> None: def test_datetimeindex_indexer_at_time() -> None: dti = pd.date_range("2023-01-01", "2023-02-01") - check( - assert_type(dti.indexer_at_time("10:00"), np_1darray[np.intp]), - np_1darray[np.intp], - ) - check( - assert_type(dti.indexer_at_time(time(10)), np_1darray[np.intp]), - np_1darray[np.intp], - ) + check(assert_type(dti.indexer_at_time("10:00"), np_1darray_intp), np_1darray_intp) + check(assert_type(dti.indexer_at_time(time(10)), np_1darray_intp), np_1darray_intp) def test_datetimeindex_indexer_between_time() -> None: @@ -109,13 +104,13 @@ def test_datetimeindex_indexer_between_time() -> None: dti.indexer_between_time( "10:00", time(11), include_start=False, include_end=True ), - np_1darray[np.intp], + np_1darray_intp, ), - np_1darray[np.intp], + np_1darray_intp, ) check( - assert_type(dti.indexer_between_time(time(10), "11:00"), np_1darray[np.intp]), - np_1darray[np.intp], + assert_type(dti.indexer_between_time(time(10), "11:00"), np_1darray_intp), + np_1darray_intp, ) diff --git a/tests/indexes/test_indexes.py b/tests/indexes/test_indexes.py index 8903c5666..8647f6dde 100644 --- a/tests/indexes/test_indexes.py +++ b/tests/indexes/test_indexes.py @@ -28,6 +28,9 @@ TYPE_CHECKING_INVALID_USAGE, check, np_1darray, + np_1darray_bool, + np_1darray_int64, + np_1darray_intp, np_ndarray_dt, pytest_warns_bounded, ) @@ -48,13 +51,13 @@ def test_index_duplicated() -> None: df = pd.DataFrame({"x": [1, 2, 3, 4]}, index=pd.Index([1, 2, 3, 2])) ind = df.index duplicated = ind.duplicated("first") - check(assert_type(duplicated, np_1darray[np.bool]), np_1darray[np.bool]) + check(assert_type(duplicated, np_1darray_bool), np_1darray_bool) def test_index_isin() -> None: ind = pd.Index([1, 2, 3, 4, 5]) isin = ind.isin([2, 4]) - check(assert_type(isin, np_1darray[np.bool]), np_1darray[np.bool]) + check(assert_type(isin, np_1darray_bool), np_1darray_bool) def test_index_astype() -> None: @@ -248,11 +251,8 @@ def test_types_to_numpy() -> None: check(assert_type(idx.to_numpy(na_value=0), np_1darray), np_1darray) r_idx = pd.RangeIndex(2) - check(assert_type(r_idx.to_numpy(), np_1darray[np.int64]), np_1darray[np.int64]) - check( - assert_type(r_idx.to_numpy(na_value=0), np_1darray[np.int64]), - np_1darray[np.int64], - ) + check(assert_type(r_idx.to_numpy(), np_1darray_int64), np_1darray_int64) + check(assert_type(r_idx.to_numpy(na_value=0), np_1darray_int64), np_1darray_int64) check( assert_type(r_idx.to_numpy(dtype="int", copy=True), np_1darray), np_1darray, @@ -1191,38 +1191,32 @@ def test_datetime_operators_builtin() -> None: def test_get_loc() -> None: unique_index = pd.Index(list("abc")) check( - assert_type(unique_index.get_loc("b"), int | slice | np_1darray[np.bool]), + assert_type(unique_index.get_loc("b"), int | slice | np_1darray_bool), int, ) monotonic_index = pd.Index(list("abbc")) check( - assert_type(monotonic_index.get_loc("b"), int | slice | np_1darray[np.bool]), + assert_type(monotonic_index.get_loc("b"), int | slice | np_1darray_bool), slice, ) non_monotonic_index = pd.Index(list("abcb")) check( - assert_type( - non_monotonic_index.get_loc("b"), int | slice | np_1darray[np.bool] - ), - np_1darray[np.bool], + assert_type(non_monotonic_index.get_loc("b"), int | slice | np_1darray_bool), + np_1darray_bool, ) i1, i2, i3 = pd.Interval(0, 1), pd.Interval(1, 2), pd.Interval(0, 2) unique_interval_index = pd.IntervalIndex([i1, i2]) check( - assert_type( - unique_interval_index.get_loc(i1), int | slice | np_1darray[np.bool] - ), + assert_type(unique_interval_index.get_loc(i1), int | slice | np_1darray_bool), np.int64, ) overlap_interval_index = pd.IntervalIndex([i1, i2, i3]) check( - assert_type( - overlap_interval_index.get_loc(1), int | slice | np_1darray[np.bool] - ), - np_1darray[np.bool], + assert_type(overlap_interval_index.get_loc(1), int | slice | np_1darray_bool), + np_1darray_bool, ) @@ -1367,13 +1361,13 @@ def test_index_naming() -> None: def test_index_searchsorted() -> None: idx = pd.Index([1, 2, 3]) check(assert_type(idx.searchsorted(1), np.intp), np.intp) - check(assert_type(idx.searchsorted([1]), "np_1darray[np.intp]"), np_1darray) - check(assert_type(idx.searchsorted(range(1, 2)), "np_1darray[np.intp]"), np_1darray) + check(assert_type(idx.searchsorted([1]), np_1darray_intp), np_1darray_intp) + check(assert_type(idx.searchsorted(range(1, 2)), np_1darray_intp), np_1darray_intp) check( - assert_type(idx.searchsorted(pd.Series([1])), "np_1darray[np.intp]"), np_1darray + assert_type(idx.searchsorted(pd.Series([1])), np_1darray_intp), np_1darray_intp ) check( - assert_type(idx.searchsorted(np.array([1])), "np_1darray[np.intp]"), np_1darray + assert_type(idx.searchsorted(np.array([1])), np_1darray_intp), np_1darray_intp ) check(assert_type(idx.searchsorted(1, side="left"), np.intp), np.intp) check(assert_type(idx.searchsorted(1, sorter=[1, 0, 2]), np.intp), np.intp) @@ -1397,10 +1391,7 @@ def test_period_index_asof_locs() -> None: idx = pd.PeriodIndex(["2000", "2001"], freq="D") where = pd.DatetimeIndex(["2023-05-30 00:12:00", "2023-06-01 00:00:00"]) mask = np.ones(2, dtype=bool) - check( - assert_type(idx.asof_locs(where, mask), np_1darray[np.intp]), - np_1darray[np.intp], - ) + check(assert_type(idx.asof_locs(where, mask), np_1darray_intp), np_1darray_intp) def test_array_property() -> None: diff --git a/tests/indexes/test_rangeindex.py b/tests/indexes/test_rangeindex.py index 1b30e83ae..8cdb1b822 100644 --- a/tests/indexes/test_rangeindex.py +++ b/tests/indexes/test_rangeindex.py @@ -1,6 +1,5 @@ from __future__ import annotations -import numpy as np import pandas as pd from typing_extensions import ( assert_type, @@ -8,72 +7,48 @@ from tests import ( check, - np_1darray, + np_1darray_intp, ) def test_rangeindex_floordiv() -> None: ri = pd.RangeIndex(3) - check( - assert_type(ri // 2, "pd.Index[int]"), - pd.Index, - ) + check(assert_type(ri // 2, "pd.Index[int]"), pd.Index) def test_rangeindex_min_max() -> None: ri = pd.RangeIndex(3) - check( - assert_type(ri.min(), int), - int, - ) - check( - assert_type(ri.max(axis=0), int), - int, - ) + check(assert_type(ri.min(), int), int) + check(assert_type(ri.max(axis=0), int), int) def test_rangeindex_equals() -> None: ri = pd.RangeIndex(3) - check( - assert_type(ri.equals(ri), bool), - bool, - ) + check(assert_type(ri.equals(ri), bool), bool) def test_rangeindex_tolist() -> None: ri = pd.RangeIndex.from_range(range(3)) - check( - assert_type(ri.tolist(), list[int]), - list[int], - ) + check(assert_type(ri.tolist(), list[int]), list[int]) def test_rangeindex_get_indexer() -> None: ri = pd.RangeIndex.from_range(range(3)) - check( - assert_type(ri.get_indexer(ri), np_1darray[np.intp]), - np_1darray[np.intp], - ) + check(assert_type(ri.get_indexer(ri), np_1darray_intp), np_1darray_intp) def test_rangeindex_argsort() -> None: ri = pd.RangeIndex.from_range(range(3)) - check( - assert_type(ri.argsort(), np_1darray[np.intp]), - np_1darray[np.intp], - ) + check(assert_type(ri.argsort(), np_1darray_intp), np_1darray_intp) def test_rangeindex_join() -> None: ri = pd.RangeIndex.from_range(range(3)) - check( - assert_type(ri.join(ri), pd.Index), - pd.Index, - ) + check(assert_type(ri.join(ri), pd.Index), pd.Index) check( assert_type( ri.join(ri, return_indexers=True), - tuple[pd.Index, np_1darray[np.intp] | None, np_1darray[np.intp] | None], + tuple[pd.Index, np_1darray_intp | None, np_1darray_intp | None], ), - tuple[pd.Index, np_1darray[np.intp] | None, np_1darray[np.intp] | None], + tuple[pd.Index, np_1darray_intp | None, np_1darray_intp | None], ) diff --git a/tests/scalars/test_scalars.py b/tests/scalars/test_scalars.py index fd954051a..36c7f59fc 100644 --- a/tests/scalars/test_scalars.py +++ b/tests/scalars/test_scalars.py @@ -25,7 +25,7 @@ PD_LTE_23, TYPE_CHECKING_INVALID_USAGE, check, - np_1darray, + np_1darray_bool, np_2darray, np_ndarray, np_ndarray_bool, @@ -40,7 +40,7 @@ ) if not PD_LTE_23: - from pandas.errors import Pandas4Warning # type: ignore[attr-defined] # pyright: ignore # isort: skip + from pandas.errors import Pandas4Warning # type: ignore[attr-defined] # pyright: ignore[reportAttributeAccessIssue,reportRedeclaration] # isort: skip else: Pandas4Warning: TypeAlias = FutureWarning # type: ignore[no-redef] @@ -318,55 +318,55 @@ def test_interval_cmp() -> None: interval_index_int = pd.IntervalIndex([interval_i]) check( - assert_type(interval_index_int >= interval_i, np_1darray[np.bool]), - np_1darray[np.bool], + assert_type(interval_index_int >= interval_i, np_1darray_bool), + np_1darray_bool, ) check( - assert_type(interval_index_int < interval_i, np_1darray[np.bool]), - np_1darray[np.bool], + assert_type(interval_index_int < interval_i, np_1darray_bool), + np_1darray_bool, ) check( - assert_type(interval_index_int <= interval_i, np_1darray[np.bool]), - np_1darray[np.bool], + assert_type(interval_index_int <= interval_i, np_1darray_bool), + np_1darray_bool, ) check( - assert_type(interval_index_int > interval_i, np_1darray[np.bool]), - np_1darray[np.bool], + assert_type(interval_index_int > interval_i, np_1darray_bool), + np_1darray_bool, ) check( - assert_type(interval_i >= interval_index_int, np_1darray[np.bool]), - np_1darray[np.bool], + assert_type(interval_i >= interval_index_int, np_1darray_bool), + np_1darray_bool, ) check( - assert_type(interval_i < interval_index_int, np_1darray[np.bool]), - np_1darray[np.bool], + assert_type(interval_i < interval_index_int, np_1darray_bool), + np_1darray_bool, ) check( - assert_type(interval_i <= interval_index_int, np_1darray[np.bool]), - np_1darray[np.bool], + assert_type(interval_i <= interval_index_int, np_1darray_bool), + np_1darray_bool, ) check( - assert_type(interval_i > interval_index_int, np_1darray[np.bool]), - np_1darray[np.bool], + assert_type(interval_i > interval_index_int, np_1darray_bool), + np_1darray_bool, ) check( - assert_type(interval_index_int == interval_i, np_1darray[np.bool]), - np_1darray[np.bool], + assert_type(interval_index_int == interval_i, np_1darray_bool), + np_1darray_bool, ) check( - assert_type(interval_index_int != interval_i, np_1darray[np.bool]), - np_1darray[np.bool], + assert_type(interval_index_int != interval_i, np_1darray_bool), + np_1darray_bool, ) check( - assert_type(interval_i == interval_index_int, np_1darray[np.bool]), - np_1darray[np.bool], + assert_type(interval_i == interval_index_int, np_1darray_bool), + np_1darray_bool, ) check( - assert_type(interval_i != interval_index_int, np_1darray[np.bool]), - np_1darray[np.bool], + assert_type(interval_i != interval_index_int, np_1darray_bool), + np_1darray_bool, ) @@ -817,29 +817,29 @@ def test_timedelta_cmp_index() -> None: td_idx = pd.to_timedelta([1, 2, 3], unit="D") # TimedeltaIndex # >, <= - gt1 = check(assert_type(td > td_idx, np_1darray[np.bool]), np_1darray[np.bool]) - le1 = check(assert_type(td <= td_idx, np_1darray[np.bool]), np_1darray[np.bool]) + gt1 = check(assert_type(td > td_idx, np_1darray_bool), np_1darray_bool) + le1 = check(assert_type(td <= td_idx, np_1darray_bool), np_1darray_bool) assert (gt1 != le1).all() - gt2 = check(assert_type(td_idx > td, np_1darray[np.bool]), np_1darray[np.bool]) - le2 = check(assert_type(td_idx <= td, np_1darray[np.bool]), np_1darray[np.bool]) + gt2 = check(assert_type(td_idx > td, np_1darray_bool), np_1darray_bool) + le2 = check(assert_type(td_idx <= td, np_1darray_bool), np_1darray_bool) assert (gt2 != le2).all() # <, >= - lt1 = check(assert_type(td < td_idx, np_1darray[np.bool]), np_1darray[np.bool]) - ge1 = check(assert_type(td >= td_idx, np_1darray[np.bool]), np_1darray[np.bool]) + lt1 = check(assert_type(td < td_idx, np_1darray_bool), np_1darray_bool) + ge1 = check(assert_type(td >= td_idx, np_1darray_bool), np_1darray_bool) assert (lt1 != ge1).all() - lt2 = check(assert_type(td_idx < td, np_1darray[np.bool]), np_1darray[np.bool]) - ge2 = check(assert_type(td_idx >= td, np_1darray[np.bool]), np_1darray[np.bool]) + lt2 = check(assert_type(td_idx < td, np_1darray_bool), np_1darray_bool) + ge2 = check(assert_type(td_idx >= td, np_1darray_bool), np_1darray_bool) assert (lt2 != ge2).all() # ==, != - eq1 = check(assert_type(td == td_idx, np_1darray[np.bool]), np_1darray[np.bool]) - ne1 = check(assert_type(td != td_idx, np_1darray[np.bool]), np_1darray[np.bool]) + eq1 = check(assert_type(td == td_idx, np_1darray_bool), np_1darray_bool) + ne1 = check(assert_type(td != td_idx, np_1darray_bool), np_1darray_bool) assert (eq1 != ne1).all() # ==, != (td on the rhs, use == and != of lhs) - eq_rhs1 = check(assert_type(td_idx == td, np_1darray[np.bool]), np_1darray[np.bool]) - ne_rhs1 = check(assert_type(td_idx != td, np_1darray[np.bool]), np_1darray[np.bool]) + eq_rhs1 = check(assert_type(td_idx == td, np_1darray_bool), np_1darray_bool) + ne_rhs1 = check(assert_type(td_idx != td, np_1darray_bool), np_1darray_bool) assert (eq_rhs1 != ne_rhs1).all() @@ -862,11 +862,11 @@ def test_timedelta_cmp_array() -> None: gt_2d2 = check(assert_type(arr_2d > td, np_2darray[np.bool]), np_2darray[np.bool]) le_2d2 = check(assert_type(arr_2d <= td, np_2darray[np.bool]), np_2darray[np.bool]) assert (gt_2d2 != le_2d2).all() - gt_1d1 = check(assert_type(td > arr_1d, np_1darray[np.bool]), np_1darray[np.bool]) - le_1d1 = check(assert_type(td <= arr_1d, np_1darray[np.bool]), np_1darray[np.bool]) + gt_1d1 = check(assert_type(td > arr_1d, np_1darray_bool), np_1darray_bool) + le_1d1 = check(assert_type(td <= arr_1d, np_1darray_bool), np_1darray_bool) assert (gt_1d1 != le_1d1).all() - gt_1d2 = check(assert_type(arr_1d > td, np_1darray[np.bool]), np_1darray[np.bool]) - le_1d2 = check(assert_type(arr_1d <= td, np_1darray[np.bool]), np_1darray[np.bool]) + gt_1d2 = check(assert_type(arr_1d > td, np_1darray_bool), np_1darray_bool) + le_1d2 = check(assert_type(arr_1d <= td, np_1darray_bool), np_1darray_bool) assert (gt_1d2 != le_1d2).all() # <, >= @@ -882,11 +882,11 @@ def test_timedelta_cmp_array() -> None: lt_2d2 = check(assert_type(arr_2d < td, np_2darray[np.bool]), np_2darray[np.bool]) ge_2d2 = check(assert_type(arr_2d >= td, np_2darray[np.bool]), np_2darray[np.bool]) assert (lt_2d2 != ge_2d2).all() - lt_1d1 = check(assert_type(td < arr_1d, np_1darray[np.bool]), np_1darray[np.bool]) - ge_1d1 = check(assert_type(td >= arr_1d, np_1darray[np.bool]), np_1darray[np.bool]) + lt_1d1 = check(assert_type(td < arr_1d, np_1darray_bool), np_1darray_bool) + ge_1d1 = check(assert_type(td >= arr_1d, np_1darray_bool), np_1darray_bool) assert (lt_1d1 != ge_1d1).all() - lt_1d2 = check(assert_type(arr_1d < td, np_1darray[np.bool]), np_1darray[np.bool]) - ge_1d2 = check(assert_type(arr_1d >= td, np_1darray[np.bool]), np_1darray[np.bool]) + lt_1d2 = check(assert_type(arr_1d < td, np_1darray_bool), np_1darray_bool) + ge_1d2 = check(assert_type(arr_1d >= td, np_1darray_bool), np_1darray_bool) assert (lt_1d2 != ge_1d2).all() # ==, != @@ -896,8 +896,8 @@ def test_timedelta_cmp_array() -> None: eq_2d1 = check(assert_type(td == arr_2d, np_2darray[np.bool]), np_2darray[np.bool]) ne_2d1 = check(assert_type(td != arr_2d, np_2darray[np.bool]), np_2darray[np.bool]) assert (eq_2d1 != ne_2d1).all() - eq_1d1 = check(assert_type(td == arr_1d, np_1darray[np.bool]), np_1darray[np.bool]) - ne_1d1 = check(assert_type(td != arr_1d, np_1darray[np.bool]), np_1darray[np.bool]) + eq_1d1 = check(assert_type(td == arr_1d, np_1darray_bool), np_1darray_bool) + ne_1d1 = check(assert_type(td != arr_1d, np_1darray_bool), np_1darray_bool) assert (eq_1d1 != ne_1d1).all() # ==, != (td on the rhs, use == and != of lhs) @@ -907,8 +907,8 @@ def test_timedelta_cmp_array() -> None: eq_rhs_2d1 = check(assert_type(arr_2d == td, Any), np_2darray[np.bool]) ne_rhs_2d1 = check(assert_type(arr_2d != td, Any), np_2darray[np.bool]) assert (eq_rhs_2d1 != ne_rhs_2d1).all() - eq_rhs_1d1 = check(assert_type(arr_1d == td, Any), np_1darray[np.bool]) - ne_rhs_1d1 = check(assert_type(arr_1d != td, Any), np_1darray[np.bool]) + eq_rhs_1d1 = check(assert_type(arr_1d == td, Any), np_1darray_bool) + ne_rhs_1d1 = check(assert_type(arr_1d != td, Any), np_1darray_bool) assert (eq_rhs_1d1 != ne_rhs_1d1).all() @@ -1186,56 +1186,48 @@ def test_timestamp_cmp_index() -> None: un_idx = pd.DataFrame({"a": [1]}, index=dt_idx).index # >, <= - gt_dt1 = check(assert_type(ts > dt_idx, np_1darray[np.bool]), np_1darray[np.bool]) - le_dt1 = check(assert_type(ts <= dt_idx, np_1darray[np.bool]), np_1darray[np.bool]) + gt_dt1 = check(assert_type(ts > dt_idx, np_1darray_bool), np_1darray_bool) + le_dt1 = check(assert_type(ts <= dt_idx, np_1darray_bool), np_1darray_bool) assert (gt_dt1 != le_dt1).all() - gt_dt2 = check(assert_type(dt_idx > ts, np_1darray[np.bool]), np_1darray[np.bool]) - le_dt2 = check(assert_type(dt_idx <= ts, np_1darray[np.bool]), np_1darray[np.bool]) + gt_dt2 = check(assert_type(dt_idx > ts, np_1darray_bool), np_1darray_bool) + le_dt2 = check(assert_type(dt_idx <= ts, np_1darray_bool), np_1darray_bool) assert (gt_dt2 != le_dt2).all() - gt_un1 = check(assert_type(ts > un_idx, np_1darray[np.bool]), np_1darray[np.bool]) - le_un1 = check(assert_type(ts <= un_idx, np_1darray[np.bool]), np_1darray[np.bool]) + gt_un1 = check(assert_type(ts > un_idx, np_1darray_bool), np_1darray_bool) + le_un1 = check(assert_type(ts <= un_idx, np_1darray_bool), np_1darray_bool) assert (gt_un1 != le_un1).all() - gt_un2 = check(assert_type(un_idx > ts, np_1darray[np.bool]), np_1darray[np.bool]) - le_un2 = check(assert_type(un_idx <= ts, np_1darray[np.bool]), np_1darray[np.bool]) + gt_un2 = check(assert_type(un_idx > ts, np_1darray_bool), np_1darray_bool) + le_un2 = check(assert_type(un_idx <= ts, np_1darray_bool), np_1darray_bool) assert (gt_un2 != le_un2).all() # <, >= - lt_dt1 = check(assert_type(ts < dt_idx, np_1darray[np.bool]), np_1darray[np.bool]) - ge_dt1 = check(assert_type(ts >= dt_idx, np_1darray[np.bool]), np_1darray[np.bool]) + lt_dt1 = check(assert_type(ts < dt_idx, np_1darray_bool), np_1darray_bool) + ge_dt1 = check(assert_type(ts >= dt_idx, np_1darray_bool), np_1darray_bool) assert (lt_dt1 != ge_dt1).all() - lt_dt2 = check(assert_type(dt_idx < ts, np_1darray[np.bool]), np_1darray[np.bool]) - ge_dt2 = check(assert_type(dt_idx >= ts, np_1darray[np.bool]), np_1darray[np.bool]) + lt_dt2 = check(assert_type(dt_idx < ts, np_1darray_bool), np_1darray_bool) + ge_dt2 = check(assert_type(dt_idx >= ts, np_1darray_bool), np_1darray_bool) assert (lt_dt2 != ge_dt2).all() - lt_un1 = check(assert_type(ts < un_idx, np_1darray[np.bool]), np_1darray[np.bool]) - ge_un1 = check(assert_type(ts >= un_idx, np_1darray[np.bool]), np_1darray[np.bool]) + lt_un1 = check(assert_type(ts < un_idx, np_1darray_bool), np_1darray_bool) + ge_un1 = check(assert_type(ts >= un_idx, np_1darray_bool), np_1darray_bool) assert (lt_un1 != ge_un1).all() - lt_un2 = check(assert_type(un_idx < ts, np_1darray[np.bool]), np_1darray[np.bool]) - ge_un2 = check(assert_type(un_idx >= ts, np_1darray[np.bool]), np_1darray[np.bool]) + lt_un2 = check(assert_type(un_idx < ts, np_1darray_bool), np_1darray_bool) + ge_un2 = check(assert_type(un_idx >= ts, np_1darray_bool), np_1darray_bool) assert (lt_un2 != ge_un2).all() # ==, != - eq_dt1 = check(assert_type(ts == dt_idx, np_1darray[np.bool]), np_1darray[np.bool]) - ne_dt1 = check(assert_type(ts != dt_idx, np_1darray[np.bool]), np_1darray[np.bool]) + eq_dt1 = check(assert_type(ts == dt_idx, np_1darray_bool), np_1darray_bool) + ne_dt1 = check(assert_type(ts != dt_idx, np_1darray_bool), np_1darray_bool) assert (eq_dt1 != ne_dt1).all() # there is a mypy bug where ts.__eq__(Index) gets revealed as Any and not np_1darray - eq_un1 = check(assert_type(ts == un_idx, np_1darray[np.bool]), np_1darray[np.bool]) # type: ignore[assert-type] - ne_un1 = check(assert_type(ts != un_idx, np_1darray[np.bool]), np_1darray[np.bool]) # type: ignore[assert-type] + eq_un1 = check(assert_type(ts == un_idx, np_1darray_bool), np_1darray_bool) # type: ignore[assert-type] + ne_un1 = check(assert_type(ts != un_idx, np_1darray_bool), np_1darray_bool) # type: ignore[assert-type] assert (eq_un1 != ne_un1).all() # ==, != (ts on the rhs, use == and != of lhs) - eq_rhs_dt1 = check( - assert_type(dt_idx == ts, np_1darray[np.bool]), np_1darray[np.bool] - ) - ne_rhs_dt1 = check( - assert_type(dt_idx != ts, np_1darray[np.bool]), np_1darray[np.bool] - ) + eq_rhs_dt1 = check(assert_type(dt_idx == ts, np_1darray_bool), np_1darray_bool) + ne_rhs_dt1 = check(assert_type(dt_idx != ts, np_1darray_bool), np_1darray_bool) assert (eq_rhs_dt1 != ne_rhs_dt1).all() - eq_rhs_un1 = check( - assert_type(un_idx == ts, np_1darray[np.bool]), np_1darray[np.bool] - ) - ne_rhs_un1 = check( - assert_type(un_idx != ts, np_1darray[np.bool]), np_1darray[np.bool] - ) + eq_rhs_un1 = check(assert_type(un_idx == ts, np_1darray_bool), np_1darray_bool) + ne_rhs_un1 = check(assert_type(un_idx != ts, np_1darray_bool), np_1darray_bool) assert (eq_rhs_un1 != ne_rhs_un1).all() @@ -1258,11 +1250,11 @@ def test_timestamp_cmp_array() -> None: gt_2d2 = check(assert_type(arr_2d > ts, np_2darray[np.bool]), np_2darray[np.bool]) le_2d2 = check(assert_type(arr_2d <= ts, np_2darray[np.bool]), np_2darray[np.bool]) assert (gt_2d2 != le_2d2).all() - gt_1d1 = check(assert_type(ts > arr_1d, np_1darray[np.bool]), np_1darray[np.bool]) - le_1d1 = check(assert_type(ts <= arr_1d, np_1darray[np.bool]), np_1darray[np.bool]) + gt_1d1 = check(assert_type(ts > arr_1d, np_1darray_bool), np_1darray_bool) + le_1d1 = check(assert_type(ts <= arr_1d, np_1darray_bool), np_1darray_bool) assert (gt_1d1 != le_1d1).all() - gt_1d2 = check(assert_type(arr_1d > ts, np_1darray[np.bool]), np_1darray[np.bool]) - le_1d2 = check(assert_type(arr_1d <= ts, np_1darray[np.bool]), np_1darray[np.bool]) + gt_1d2 = check(assert_type(arr_1d > ts, np_1darray_bool), np_1darray_bool) + le_1d2 = check(assert_type(arr_1d <= ts, np_1darray_bool), np_1darray_bool) assert (gt_1d2 != le_1d2).all() # <, >= @@ -1278,11 +1270,11 @@ def test_timestamp_cmp_array() -> None: lt_2d2 = check(assert_type(arr_2d < ts, np_2darray[np.bool]), np_2darray[np.bool]) ge_2d2 = check(assert_type(arr_2d >= ts, np_2darray[np.bool]), np_2darray[np.bool]) assert (lt_2d2 != ge_2d2).all() - lt_1d1 = check(assert_type(ts < arr_1d, np_1darray[np.bool]), np_1darray[np.bool]) - ge_1d1 = check(assert_type(ts >= arr_1d, np_1darray[np.bool]), np_1darray[np.bool]) + lt_1d1 = check(assert_type(ts < arr_1d, np_1darray_bool), np_1darray_bool) + ge_1d1 = check(assert_type(ts >= arr_1d, np_1darray_bool), np_1darray_bool) assert (lt_1d1 != ge_1d1).all() - lt_1d2 = check(assert_type(arr_1d < ts, np_1darray[np.bool]), np_1darray[np.bool]) - ge_1d2 = check(assert_type(arr_1d >= ts, np_1darray[np.bool]), np_1darray[np.bool]) + lt_1d2 = check(assert_type(arr_1d < ts, np_1darray_bool), np_1darray_bool) + ge_1d2 = check(assert_type(arr_1d >= ts, np_1darray_bool), np_1darray_bool) assert (lt_1d2 != ge_1d2).all() # ==, != @@ -1292,8 +1284,8 @@ def test_timestamp_cmp_array() -> None: eq_2d1 = check(assert_type(ts == arr_2d, np_2darray[np.bool]), np_2darray[np.bool]) ne_2d1 = check(assert_type(ts != arr_2d, np_2darray[np.bool]), np_2darray[np.bool]) assert (eq_2d1 != ne_2d1).all() - eq_1d1 = check(assert_type(ts == arr_1d, np_1darray[np.bool]), np_1darray[np.bool]) - ne_1d1 = check(assert_type(ts != arr_1d, np_1darray[np.bool]), np_1darray[np.bool]) + eq_1d1 = check(assert_type(ts == arr_1d, np_1darray_bool), np_1darray_bool) + ne_1d1 = check(assert_type(ts != arr_1d, np_1darray_bool), np_1darray_bool) assert (eq_1d1 != ne_1d1).all() # ==, != (td on the rhs, use == and != of lhs) @@ -1303,8 +1295,8 @@ def test_timestamp_cmp_array() -> None: eq_rhs_2d1 = check(assert_type(arr_2d == ts, Any), np_2darray[np.bool]) ne_rhs_2d1 = check(assert_type(arr_2d != ts, Any), np_2darray[np.bool]) assert (eq_rhs_2d1 != ne_rhs_2d1).all() - eq_rhs_1d1 = check(assert_type(arr_1d == ts, Any), np_1darray[np.bool]) - ne_rhs_1d1 = check(assert_type(arr_1d != ts, Any), np_1darray[np.bool]) + eq_rhs_1d1 = check(assert_type(arr_1d == ts, Any), np_1darray_bool) + ne_rhs_1d1 = check(assert_type(arr_1d != ts, Any), np_1darray_bool) assert (eq_rhs_1d1 != ne_rhs_1d1).all() @@ -1817,29 +1809,29 @@ def test_period_cmp_index() -> None: p_idx = pd.period_range("2012-1-1", periods=10, freq="D") # >, <= - gt1 = check(assert_type(p > p_idx, np_1darray[np.bool]), np_1darray[np.bool]) - le1 = check(assert_type(p <= p_idx, np_1darray[np.bool]), np_1darray[np.bool]) + gt1 = check(assert_type(p > p_idx, np_1darray_bool), np_1darray_bool) + le1 = check(assert_type(p <= p_idx, np_1darray_bool), np_1darray_bool) assert (gt1 != le1).all() - gt2 = check(assert_type(p_idx > p, np_1darray[np.bool]), np_1darray[np.bool]) - le2 = check(assert_type(p_idx <= p, np_1darray[np.bool]), np_1darray[np.bool]) + gt2 = check(assert_type(p_idx > p, np_1darray_bool), np_1darray_bool) + le2 = check(assert_type(p_idx <= p, np_1darray_bool), np_1darray_bool) assert (gt2 != le2).all() # <, >= - lt1 = check(assert_type(p < p_idx, np_1darray[np.bool]), np_1darray[np.bool]) - ge1 = check(assert_type(p >= p_idx, np_1darray[np.bool]), np_1darray[np.bool]) + lt1 = check(assert_type(p < p_idx, np_1darray_bool), np_1darray_bool) + ge1 = check(assert_type(p >= p_idx, np_1darray_bool), np_1darray_bool) assert (lt1 != ge1).all() - lt2 = check(assert_type(p_idx < p, np_1darray[np.bool]), np_1darray[np.bool]) - ge2 = check(assert_type(p_idx >= p, np_1darray[np.bool]), np_1darray[np.bool]) + lt2 = check(assert_type(p_idx < p, np_1darray_bool), np_1darray_bool) + ge2 = check(assert_type(p_idx >= p, np_1darray_bool), np_1darray_bool) assert (lt2 != ge2).all() # ==, != - eq1 = check(assert_type(p == p_idx, np_1darray[np.bool]), np_1darray[np.bool]) - ne1 = check(assert_type(p != p_idx, np_1darray[np.bool]), np_1darray[np.bool]) + eq1 = check(assert_type(p == p_idx, np_1darray_bool), np_1darray_bool) + ne1 = check(assert_type(p != p_idx, np_1darray_bool), np_1darray_bool) assert (eq1 != ne1).all() # ==, != (p on the rhs, use == and != of lhs) - eq_rhs1 = check(assert_type(p_idx == p, np_1darray[np.bool]), np_1darray[np.bool]) - ne_rhs1 = check(assert_type(p_idx != p, np_1darray[np.bool]), np_1darray[np.bool]) + eq_rhs1 = check(assert_type(p_idx == p, np_1darray_bool), np_1darray_bool) + ne_rhs1 = check(assert_type(p_idx != p, np_1darray_bool), np_1darray_bool) assert (eq_rhs1 != ne_rhs1).all() @@ -1856,8 +1848,8 @@ def test_period_cmp_array() -> None: gt_2d1 = check(assert_type(p > arr_2d, np_2darray[np.bool]), np_2darray[np.bool]) le_2d1 = check(assert_type(p <= arr_2d, np_2darray[np.bool]), np_2darray[np.bool]) assert (gt_2d1 != le_2d1).all() - gt_1d1 = check(assert_type(p > arr_1d, np_1darray[np.bool]), np_1darray[np.bool]) - le_1d1 = check(assert_type(p <= arr_1d, np_1darray[np.bool]), np_1darray[np.bool]) + gt_1d1 = check(assert_type(p > arr_1d, np_1darray_bool), np_1darray_bool) + le_1d1 = check(assert_type(p <= arr_1d, np_1darray_bool), np_1darray_bool) assert (gt_1d1 != le_1d1).all() # p on the rhs, type depends on np.ndarray > and <= methods gt_nd2 = check(assert_type(arr_nd > p, np_ndarray_bool), np_ndarray_bool, np.bool) @@ -1866,8 +1858,8 @@ def test_period_cmp_array() -> None: gt_2d2 = check(assert_type(arr_2d > p, np_ndarray_bool), np_2darray[np.bool]) le_2d2 = check(assert_type(arr_2d <= p, np_ndarray_bool), np_2darray[np.bool]) assert (gt_2d2 != le_2d2).all() - gt_1d2 = check(assert_type(arr_1d > p, np_ndarray_bool), np_1darray[np.bool]) - le_1d2 = check(assert_type(arr_1d <= p, np_ndarray_bool), np_1darray[np.bool]) + gt_1d2 = check(assert_type(arr_1d > p, np_ndarray_bool), np_1darray_bool) + le_1d2 = check(assert_type(arr_1d <= p, np_ndarray_bool), np_1darray_bool) assert (gt_1d2 != le_1d2).all() # <, >= @@ -1877,8 +1869,8 @@ def test_period_cmp_array() -> None: lt_2d1 = check(assert_type(p < arr_2d, np_2darray[np.bool]), np_2darray[np.bool]) ge_2d1 = check(assert_type(p >= arr_2d, np_2darray[np.bool]), np_2darray[np.bool]) assert (lt_2d1 != ge_2d1).all() - lt_1d1 = check(assert_type(p < arr_1d, np_1darray[np.bool]), np_1darray[np.bool]) - ge_1d1 = check(assert_type(p >= arr_1d, np_1darray[np.bool]), np_1darray[np.bool]) + lt_1d1 = check(assert_type(p < arr_1d, np_1darray_bool), np_1darray_bool) + ge_1d1 = check(assert_type(p >= arr_1d, np_1darray_bool), np_1darray_bool) assert (lt_1d1 != ge_1d1).all() # p on the rhs, type depends on np.ndarray < and >= methods lt_nd2 = check(assert_type(arr_nd < p, np_ndarray_bool), np_ndarray_bool, np.bool) @@ -1887,8 +1879,8 @@ def test_period_cmp_array() -> None: lt_2d2 = check(assert_type(arr_2d < p, np_ndarray_bool), np_2darray[np.bool]) ge_2d2 = check(assert_type(arr_2d >= p, np_ndarray_bool), np_2darray[np.bool]) assert (lt_2d2 != ge_2d2).all() - lt_1d2 = check(assert_type(arr_1d < p, np_ndarray_bool), np_1darray[np.bool]) - ge_1d2 = check(assert_type(arr_1d >= p, np_ndarray_bool), np_1darray[np.bool]) + lt_1d2 = check(assert_type(arr_1d < p, np_ndarray_bool), np_1darray_bool) + ge_1d2 = check(assert_type(arr_1d >= p, np_ndarray_bool), np_1darray_bool) assert (lt_1d2 != ge_1d2).all() # ==, != @@ -1898,8 +1890,8 @@ def test_period_cmp_array() -> None: eq_2d1 = check(assert_type(p == arr_2d, np_2darray[np.bool]), np_2darray[np.bool]) ne_2d1 = check(assert_type(p != arr_2d, np_2darray[np.bool]), np_2darray[np.bool]) assert (eq_2d1 != ne_2d1).all() - eq_1d1 = check(assert_type(p == arr_1d, np_1darray[np.bool]), np_1darray[np.bool]) - ne_1d1 = check(assert_type(p != arr_1d, np_1darray[np.bool]), np_1darray[np.bool]) + eq_1d1 = check(assert_type(p == arr_1d, np_1darray_bool), np_1darray_bool) + ne_1d1 = check(assert_type(p != arr_1d, np_1darray_bool), np_1darray_bool) assert (eq_1d1 != ne_1d1).all() # ==, != (td on the rhs, use == and != of lhs) @@ -1909,8 +1901,8 @@ def test_period_cmp_array() -> None: eq_rhs_2d1 = check(assert_type(arr_2d == p, Any), np_2darray[np.bool]) ne_rhs_2d1 = check(assert_type(arr_2d != p, Any), np_2darray[np.bool]) assert (eq_rhs_2d1 != ne_rhs_2d1).all() - eq_rhs_1d1 = check(assert_type(arr_1d == p, Any), np_1darray[np.bool]) - ne_rhs_1d1 = check(assert_type(arr_1d != p, Any), np_1darray[np.bool]) + eq_rhs_1d1 = check(assert_type(arr_1d == p, Any), np_1darray_bool) + ne_rhs_1d1 = check(assert_type(arr_1d != p, Any), np_1darray_bool) assert (eq_rhs_1d1 != ne_rhs_1d1).all() diff --git a/tests/series/test_series.py b/tests/series/test_series.py index 40c3ec10a..ee56795a6 100644 --- a/tests/series/test_series.py +++ b/tests/series/test_series.py @@ -34,7 +34,6 @@ ) from pandas.api.typing import NAType from pandas.core.arrays.datetimes import DatetimeArray -from pandas.core.arrays.string_ import StringArray from pandas.core.arrays.timedeltas import TimedeltaArray from pandas.core.window import ExponentialMovingWindow from pandas.core.window.expanding import Expanding @@ -94,7 +93,7 @@ ) if not PD_LTE_23: - from pandas.errors import Pandas4Warning # type: ignore[attr-defined] # pyright: ignore # isort: skip + from pandas.errors import Pandas4Warning # type: ignore[attr-defined] # pyright: ignore[reportAttributeAccessIssue,reportRedeclaration] # isort: skip else: Pandas4Warning: TypeAlias = FutureWarning # type: ignore[no-redef] @@ -1415,23 +1414,17 @@ def test_types_rename_axis() -> None: def test_types_values() -> None: check( assert_type( - pd.Series([1, 2, 3]).values, - np_1darray | ExtensionArray | pd.Categorical, + pd.Series([1, 2, 3]).values, np_1darray | ExtensionArray | pd.Categorical ), np_1darray, np.integer, ) - valresult_type: type[np_1darray | ExtensionArray | pd.Categorical] - if PD_LTE_23: - valresult_type = np_1darray - else: - valresult_type = StringArray check( assert_type( - pd.Series(list("aabc")).values, - np_1darray | ExtensionArray | pd.Categorical, + pd.Series(list("aabc")).values, np_1darray | ExtensionArray | pd.Categorical ), - valresult_type, + # TODO: ArrowStringArray after pandas-dev/pandas-stubs#1469 + np_1darray if PD_LTE_23 else ExtensionArray, str, ) check( diff --git a/tests/test_frame.py b/tests/test_frame.py index eb2960b0f..7863b26ac 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -79,7 +79,7 @@ _PandasNamedTuple: TypeAlias = tuple if not PD_LTE_23: - from pandas.errors import Pandas4Warning # type: ignore[attr-defined] # pyright: ignore # isort: skip + from pandas.errors import Pandas4Warning # type: ignore[attr-defined] # pyright: ignore[reportAttributeAccessIssue,reportRedeclaration] # isort: skip else: Pandas4Warning: TypeAlias = FutureWarning # type: ignore[no-redef] @@ -590,10 +590,15 @@ def test_types_set_index() -> None: check(assert_type(df.set_index("col1"), pd.DataFrame), pd.DataFrame) check(assert_type(df.set_index("col1", drop=False), pd.DataFrame), pd.DataFrame) check(assert_type(df.set_index("col1", append=True), pd.DataFrame), pd.DataFrame) - check( - assert_type(df.set_index("col1", verify_integrity=True), pd.DataFrame), - pd.DataFrame, - ) + with pytest_warns_bounded( + Pandas4Warning, + "The 'verify_integrity' keyword in DataFrame.set_index is deprecated and will be removed in a future version. Directly check the result.index.is_unique instead.", + lower="2.99", + ): + check( + assert_type(df.set_index("col1", verify_integrity=True), pd.DataFrame), + pd.DataFrame, + ) check(assert_type(df.set_index(["col1", "col2"]), pd.DataFrame), pd.DataFrame) check(assert_type(df.set_index("col1", inplace=True), None), type(None)) # GH 140 @@ -838,7 +843,7 @@ def test_dataframe_clip() -> None: df.clip(lower=pd.Series([1, 2]), upper=pd.Series([4, 5]), axis=None) # type: ignore[call-overload] # pyright: ignore[reportCallIssue, reportArgumentType] df.copy().clip(lower=pd.Series([1, 2]), upper=None, axis=None, inplace=True) # type: ignore[call-overload] # pyright: ignore[reportCallIssue, reportArgumentType] df.copy().clip(lower=None, upper=pd.Series([1, 2]), axis=None, inplace=True) # type: ignore[call-overload] # pyright: ignore[reportCallIssue, reportArgumentType] - df.copy().clip(lower=pd.Series([4, 5]), upper=pd.Series([1, 2]), axis=None, inplace=True) # type: ignore[call-overload] # pyright: ignore[reportCallIssue, reportArgumentType] + df.copy().clip(lower=pd.Series([4, 5]), upper=pd.Series([1, 2]), axis=None, inplace=True) # type: ignore[call-overload]# pyright: ignore[reportCallIssue, reportArgumentType] check( assert_type(df.clip(lower=None, upper=None, axis=None), pd.DataFrame), @@ -967,10 +972,7 @@ def test_dataframe_clip() -> None: assert_type(df.clip(upper=None, axis="index", inplace=True), pd.DataFrame), pd.DataFrame, ) - check( - assert_type(df.clip(upper=15, axis="index", inplace=True), None), - type(None), - ) + check(assert_type(df.clip(upper=15, axis="index", inplace=True), None), type(None)) check( assert_type(df.clip(upper=pd.Series([1, 2]), axis="index", inplace=True), None), type(None), @@ -3752,10 +3754,7 @@ def test_to_records() -> None: np.recarray, ) dtypes = {"col1": np.int8, "col2": np.int16} - check( - assert_type(DF.to_records(False, dtypes), np.recarray), - np.recarray, - ) + check(assert_type(DF.to_records(False, dtypes), np.recarray), np.recarray) def test_to_dict_simple() -> None: @@ -3771,18 +3770,31 @@ def test_to_dict_simple() -> None: # orient param accepting "tight" added in 1.4.0 https://pandas.pydata.org/docs/whatsnew/v1.4.0.html check(assert_type(data.to_dict("tight"), dict[str, list]), dict, str) - if TYPE_CHECKING_INVALID_USAGE: - - def test(mapping: Mapping) -> None: # pyright: ignore[reportUnusedFunction] - data.to_dict(into=mapping) # type: ignore[call-overload] # pyright: ignore[reportArgumentType,reportCallIssue] + def _0(mapping: Mapping) -> None: # pyright: ignore[reportUnusedFunction] + data.to_dict(into=mapping) # type: ignore[call-overload] # pyright: ignore[reportArgumentType,reportCallIssue] + def _1() -> None: # pyright: ignore[reportUnusedFunction] assert_type(data.to_dict(into=defaultdict), Never) + + def _2() -> None: # pyright: ignore[reportUnusedFunction] assert_type(data.to_dict("records", into=defaultdict), Never) + + def _3() -> None: # pyright: ignore[reportUnusedFunction] assert_type(data.to_dict("index", into=defaultdict), Never) + + def _4() -> None: # pyright: ignore[reportUnusedFunction] assert_type(data.to_dict("dict", into=defaultdict), Never) + + def _5() -> None: # pyright: ignore[reportUnusedFunction] assert_type(data.to_dict("list", into=defaultdict), Never) + + def _6() -> None: # pyright: ignore[reportUnusedFunction] assert_type(data.to_dict("series", into=defaultdict), Never) + + def _7() -> None: # pyright: ignore[reportUnusedFunction] assert_type(data.to_dict("split", into=defaultdict), Never) + + def _8() -> None: # pyright: ignore[reportUnusedFunction] assert_type(data.to_dict("tight", into=defaultdict), Never) @@ -4330,10 +4342,10 @@ def test_to_dict_index() -> None: assert_type(df.to_dict(orient="split", index=False), dict[str, list]), dict, str ) if TYPE_CHECKING_INVALID_USAGE: - 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] - check(assert_type(df.to_dict(orient="dict", index=False), dict[Hashable, Any]), dict) # type: ignore[assert-type, call-overload] # pyright: ignore[reportArgumentType,reportAssertTypeFailure,reportCallIssue] - check(assert_type(df.to_dict(orient="series", index=False), dict[Hashable, Any]), dict) # type: ignore[assert-type, call-overload] # pyright: ignore[reportArgumentType,reportAssertTypeFailure,reportCallIssue] - check(assert_type(df.to_dict(orient="index", index=False), dict[Hashable, Any]), dict) # type: ignore[assert-type, call-overload] # pyright: ignore[reportArgumentType,reportAssertTypeFailure,reportCallIssue] + _0 = df.to_dict(orient="records", index=False) # type: ignore[call-overload] # pyright: ignore[reportArgumentType,reportCallIssue] + _1 = df.to_dict(orient="dict", index=False) # type: ignore[call-overload] # pyright: ignore[reportArgumentType,reportCallIssue] + _2 = df.to_dict(orient="series", index=False) # type: ignore[call-overload] # pyright: ignore[reportArgumentType,reportCallIssue] + _3 = df.to_dict(orient="index", index=False) # type: ignore[call-overload] # pyright: ignore[reportArgumentType,reportCallIssue] def test_suffix_prefix_index() -> None: @@ -4396,13 +4408,23 @@ def test_select_dtypes() -> None: ), pd.DataFrame, ) - if TYPE_CHECKING_INVALID_USAGE: - # include and exclude shall not be both empty + + # include and exclude shall not be both empty + def _0() -> None: # pyright: ignore[reportUnusedFunction] + assert_never(df.select_dtypes([], [])) - assert_never(df.select_dtypes()) - # str like dtypes are not allowed + + if TYPE_CHECKING_INVALID_USAGE: + _1 = df.select_dtypes() # type: ignore[call-overload] # pyright: ignore[reportCallIssue] + + # str like dtypes are not allowed + def _2() -> None: # pyright: ignore[reportUnusedFunction] assert_never(df.select_dtypes(str)) + + def _3() -> None: # pyright: ignore[reportUnusedFunction] assert_never(df.select_dtypes(exclude=str)) + + def _4() -> None: # pyright: ignore[reportUnusedFunction] assert_never(df.select_dtypes(None, str)) @@ -4437,7 +4459,9 @@ def test_interpolate_inplace() -> None: def test_getitem_generator() -> None: # GH 685 - check(assert_type(DF[(f"col{i+1}" for i in range(2))], pd.DataFrame), pd.DataFrame) + check( + assert_type(DF[(f"col{i + 1}" for i in range(2))], pd.DataFrame), pd.DataFrame + ) def test_getitem_dict_keys() -> None: @@ -4589,7 +4613,6 @@ def test_hashable_args() -> None: test = ["test"] with ensure_clean() as path: - df.to_stata(path, version=117, convert_strl=test) df.to_stata(path, version=117, convert_strl=["test"]) @@ -4714,7 +4737,6 @@ def test_unstack() -> None: def test_from_records() -> None: - # test with np.ndarray arr = np.array([[1, "a"], [2, "b"]], dtype=object).reshape(2, 2) check(assert_type(pd.DataFrame.from_records(arr), pd.DataFrame), pd.DataFrame) diff --git a/tests/test_groupby.py b/tests/test_groupby.py index a6f623ab3..20ec6119f 100644 --- a/tests/test_groupby.py +++ b/tests/test_groupby.py @@ -438,8 +438,7 @@ def f(val: Series) -> float: Series, float, ) - # This fails typing checks, and should work in 3.0, but is a bug in main - # https://github.com/pandas-dev/pandas/issues/58690 + # TODO: This fails typing checks, and should work in 3.0, but is a bug in main pandas-dev/pandas#58690 # check( # assert_type( # GB_S.apply(lambda x: x.resample("ME").interpolate(inplace=True)), None diff --git a/tests/test_interval.py b/tests/test_interval.py index 40a8583bd..a8e9c92b5 100644 --- a/tests/test_interval.py +++ b/tests/test_interval.py @@ -7,7 +7,7 @@ from tests import ( TYPE_CHECKING_INVALID_USAGE, check, - np_1darray, + np_1darray_bool, ) @@ -127,4 +127,4 @@ def test_interval_array_contains() -> None: ser = pd.Series(obj, index=df.index) arr = ser.array check(assert_type(arr.contains(df["A"]), "pd.Series[bool]"), pd.Series, np.bool_) - check(assert_type(arr.contains(3), np_1darray[np.bool]), np_1darray[np.bool]) + check(assert_type(arr.contains(3), np_1darray_bool), np_1darray_bool) diff --git a/tests/test_resampler.py b/tests/test_resampler.py index ad9f751d1..20ae58478 100644 --- a/tests/test_resampler.py +++ b/tests/test_resampler.py @@ -147,8 +147,8 @@ def test_interpolate() -> None: def test_interpolate_inplace() -> None: + # TODO: ValueError: Cannot interpolate inplace on a resampled object. Disable in Pandas 3.0 pandas-dev/pandas#57064 if PD_LTE_23: - # Bug in main see https://github.com/pandas-dev/pandas/issues/58690 check( assert_type(DF.resample("ME").interpolate(inplace=True), None), type(None) ) @@ -362,8 +362,8 @@ def test_interpolate_series() -> None: def test_interpolate_inplace_series() -> None: + # TODO: ValueError: Cannot interpolate inplace on a resampled object. Disable in Pandas 3.0 pandas-dev/pandas#57064 if PD_LTE_23: - # Bug in main see https://github.com/pandas-dev/pandas/issues/58690 check(assert_type(S.resample("ME").interpolate(inplace=True), None), type(None)) diff --git a/tests/test_string_accessors.py b/tests/test_string_accessors.py index 7ab9e769f..33033ee40 100644 --- a/tests/test_string_accessors.py +++ b/tests/test_string_accessors.py @@ -7,10 +7,9 @@ from typing_extensions import assert_type from tests import ( - PD_LTE_23, TYPE_CHECKING_INVALID_USAGE, check, - np_1darray, + np_1darray_bool, ) DATA = ["applep", "bananap", "Cherryp", "DATEp", "eGGpLANTp", "123p", "23.45p"] @@ -41,19 +40,11 @@ def test_string_accessors_boolean_series() -> None: _check( assert_type(s.str.contains("a"), "pd.Series[bool]"), ) - if PD_LTE_23: - # Bug in pandas 3.0 dev https://github.com/pandas-dev/pandas/issues/61942 - _check( - assert_type( - s.str.contains(re.compile(r"a"), regex=True), "pd.Series[bool]" - ), - ) + _check(assert_type(s.str.contains(re.compile(r"a"), regex=True), "pd.Series[bool]")) _check(assert_type(s.str.endswith("e"), "pd.Series[bool]")) _check(assert_type(s.str.endswith(("e", "f")), "pd.Series[bool]")) _check(assert_type(s.str.fullmatch("apple"), "pd.Series[bool]")) - if PD_LTE_23: - # Bug in 3.0 dev: https://github.com/pandas-dev/pandas/issues/61952 - _check(assert_type(s.str.fullmatch(re.compile(r"apple")), "pd.Series[bool]")) + _check(assert_type(s.str.fullmatch(re.compile(r"apple")), "pd.Series[bool]")) _check(assert_type(s.str.isalnum(), "pd.Series[bool]")) _check(assert_type(s.str.isalpha(), "pd.Series[bool]")) _check(assert_type(s.str.isdecimal(), "pd.Series[bool]")) @@ -64,49 +55,33 @@ def test_string_accessors_boolean_series() -> None: _check(assert_type(s.str.istitle(), "pd.Series[bool]")) _check(assert_type(s.str.isupper(), "pd.Series[bool]")) _check(assert_type(s.str.match("pp"), "pd.Series[bool]")) - if PD_LTE_23: - # Bug in 3.0 dev: https://github.com/pandas-dev/pandas/issues/61952 - _check(assert_type(s.str.match(re.compile(r"pp")), "pd.Series[bool]")) + _check(assert_type(s.str.match(re.compile(r"pp")), "pd.Series[bool]")) def test_string_accessors_boolean_index() -> None: idx = pd.Index(DATA) - _check = functools.partial(check, klass=np_1darray[np.bool]) - _check(assert_type(idx.str.startswith("a"), np_1darray[np.bool])) - _check( - assert_type(idx.str.startswith(("a", "b")), np_1darray[np.bool]), - ) + _check = functools.partial(check, klass=np_1darray_bool) + _check(assert_type(idx.str.startswith("a"), np_1darray_bool)) _check( - assert_type(idx.str.contains("a"), np_1darray[np.bool]), - ) - if PD_LTE_23: - # Bug in pandas 3.0 dev https://github.com/pandas-dev/pandas/issues/61942 - _check( - assert_type( - idx.str.contains(re.compile(r"a"), regex=True), np_1darray[np.bool] - ), - ) - _check(assert_type(idx.str.endswith("e"), np_1darray[np.bool])) - _check(assert_type(idx.str.endswith(("e", "f")), np_1darray[np.bool])) - _check(assert_type(idx.str.fullmatch("apple"), np_1darray[np.bool])) - if PD_LTE_23: - # Bug in 3.0 dev: https://github.com/pandas-dev/pandas/issues/61952 - _check( - assert_type(idx.str.fullmatch(re.compile(r"apple")), np_1darray[np.bool]) - ) - _check(assert_type(idx.str.isalnum(), np_1darray[np.bool])) - _check(assert_type(idx.str.isalpha(), np_1darray[np.bool])) - _check(assert_type(idx.str.isdecimal(), np_1darray[np.bool])) - _check(assert_type(idx.str.isdigit(), np_1darray[np.bool])) - _check(assert_type(idx.str.isnumeric(), np_1darray[np.bool])) - _check(assert_type(idx.str.islower(), np_1darray[np.bool])) - _check(assert_type(idx.str.isspace(), np_1darray[np.bool])) - _check(assert_type(idx.str.istitle(), np_1darray[np.bool])) - _check(assert_type(idx.str.isupper(), np_1darray[np.bool])) - _check(assert_type(idx.str.match("pp"), np_1darray[np.bool])) - if PD_LTE_23: - # Bug in 3.0 dev: https://github.com/pandas-dev/pandas/issues/61952 - _check(assert_type(idx.str.match(re.compile(r"pp")), np_1darray[np.bool])) + assert_type(idx.str.startswith(("a", "b")), np_1darray_bool), + ) + _check(assert_type(idx.str.contains("a"), np_1darray_bool)) + _check(assert_type(idx.str.contains(re.compile(r"a"), regex=True), np_1darray_bool)) + _check(assert_type(idx.str.endswith("e"), np_1darray_bool)) + _check(assert_type(idx.str.endswith(("e", "f")), np_1darray_bool)) + _check(assert_type(idx.str.fullmatch("apple"), np_1darray_bool)) + _check(assert_type(idx.str.fullmatch(re.compile(r"apple")), np_1darray_bool)) + _check(assert_type(idx.str.isalnum(), np_1darray_bool)) + _check(assert_type(idx.str.isalpha(), np_1darray_bool)) + _check(assert_type(idx.str.isdecimal(), np_1darray_bool)) + _check(assert_type(idx.str.isdigit(), np_1darray_bool)) + _check(assert_type(idx.str.isnumeric(), np_1darray_bool)) + _check(assert_type(idx.str.islower(), np_1darray_bool)) + _check(assert_type(idx.str.isspace(), np_1darray_bool)) + _check(assert_type(idx.str.istitle(), np_1darray_bool)) + _check(assert_type(idx.str.isupper(), np_1darray_bool)) + _check(assert_type(idx.str.match("pp"), np_1darray_bool)) + _check(assert_type(idx.str.match(re.compile(r"pp")), np_1darray_bool)) def test_string_accessors_integer_series() -> None: diff --git a/tests/test_timefuncs.py b/tests/test_timefuncs.py index e495209f7..1fa8bd286 100644 --- a/tests/test_timefuncs.py +++ b/tests/test_timefuncs.py @@ -29,6 +29,7 @@ TYPE_CHECKING_INVALID_USAGE, check, np_1darray, + np_1darray_bool, np_1darray_dt, np_1darray_int64, np_1darray_td, @@ -48,7 +49,7 @@ ) if not PD_LTE_23: - from pandas.errors import Pandas4Warning # type: ignore[attr-defined] # pyright: ignore # isort: skip + from pandas.errors import Pandas4Warning # type: ignore[attr-defined] # pyright: ignore[reportAttributeAccessIssue,reportRedeclaration] # isort: skip else: Pandas4Warning: TypeAlias = FutureWarning # type: ignore[no-redef] @@ -273,12 +274,12 @@ def test_comparisons_datetimeindex() -> None: # GH 74 dti = pd.date_range("2000-01-01", "2000-01-10") ts = pd.Timestamp("2000-01-05") - check(assert_type((dti < ts), np_1darray[np.bool]), np_1darray[np.bool]) - check(assert_type((dti > ts), np_1darray[np.bool]), np_1darray[np.bool]) - check(assert_type((dti >= ts), np_1darray[np.bool]), np_1darray[np.bool]) - check(assert_type((dti <= ts), np_1darray[np.bool]), np_1darray[np.bool]) - check(assert_type((dti == ts), np_1darray[np.bool]), np_1darray[np.bool]) - check(assert_type((dti != ts), np_1darray[np.bool]), np_1darray[np.bool]) + check(assert_type((dti < ts), np_1darray_bool), np_1darray_bool) + check(assert_type((dti > ts), np_1darray_bool), np_1darray_bool) + check(assert_type((dti >= ts), np_1darray_bool), np_1darray_bool) + check(assert_type((dti <= ts), np_1darray_bool), np_1darray_bool) + check(assert_type((dti == ts), np_1darray_bool), np_1darray_bool) + check(assert_type((dti != ts), np_1darray_bool), np_1darray_bool) def test_to_datetime_nat() -> None: @@ -626,13 +627,13 @@ def test_datetimeindex_accessors() -> None: check(assert_type(i0.dayofyear, "pd.Index[int]"), pd.Index, np.int32) check(assert_type(i0.day_of_year, "pd.Index[int]"), pd.Index, np.int32) check(assert_type(i0.quarter, "pd.Index[int]"), pd.Index, np.int32) - check(assert_type(i0.is_month_start, np_1darray[np.bool]), np_1darray[np.bool]) - check(assert_type(i0.is_month_end, np_1darray[np.bool]), np_1darray[np.bool]) - check(assert_type(i0.is_quarter_start, np_1darray[np.bool]), np_1darray[np.bool]) - check(assert_type(i0.is_quarter_end, np_1darray[np.bool]), np_1darray[np.bool]) - check(assert_type(i0.is_year_start, np_1darray[np.bool]), np_1darray[np.bool]) - check(assert_type(i0.is_year_end, np_1darray[np.bool]), np_1darray[np.bool]) - check(assert_type(i0.is_leap_year, np_1darray[np.bool]), np_1darray[np.bool]) + check(assert_type(i0.is_month_start, np_1darray_bool), np_1darray_bool) + check(assert_type(i0.is_month_end, np_1darray_bool), np_1darray_bool) + check(assert_type(i0.is_quarter_start, np_1darray_bool), np_1darray_bool) + check(assert_type(i0.is_quarter_end, np_1darray_bool), np_1darray_bool) + check(assert_type(i0.is_year_start, np_1darray_bool), np_1darray_bool) + check(assert_type(i0.is_year_end, np_1darray_bool), np_1darray_bool) + check(assert_type(i0.is_leap_year, np_1darray_bool), np_1darray_bool) check(assert_type(i0.daysinmonth, "pd.Index[int]"), pd.Index, np.int32) check(assert_type(i0.days_in_month, "pd.Index[int]"), pd.Index, np.int32) check(assert_type(i0.tz, dt.tzinfo | None), type(None)) From 3a69444115a38e47523cc78abd33aad62697cf65 Mon Sep 17 00:00:00 2001 From: cmp0xff Date: Tue, 11 Nov 2025 17:23:51 +0100 Subject: [PATCH 02/10] BaseStringArray https://github.com/pandas-dev/pandas-stubs/pull/1479#discussion_r2514689943 --- tests/series/test_series.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/series/test_series.py b/tests/series/test_series.py index d3ac911a0..42abed3b7 100644 --- a/tests/series/test_series.py +++ b/tests/series/test_series.py @@ -1432,7 +1432,7 @@ def test_types_values() -> None: assert_type( pd.Series(list("aabc")).values, np_1darray | ExtensionArray | pd.Categorical ), - # TODO: ArrowStringArray after pandas-dev/pandas-stubs#1469 + # TODO: BaseStringArray after pandas-dev/pandas-stubs#1469 np_1darray if PD_LTE_23 else ExtensionArray, str, ) From bf8ecb4e2dc27999259f29f7cff8917746508976 Mon Sep 17 00:00:00 2001 From: cmp0xff Date: Tue, 11 Nov 2025 17:26:50 +0100 Subject: [PATCH 03/10] remain in a if block https://github.com/pandas-dev/pandas-stubs/pull/1479#discussion_r2514700521 --- tests/test_frame.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/test_frame.py b/tests/test_frame.py index 32e0b1e8f..d752aef1a 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -3770,8 +3770,10 @@ def test_to_dict_simple() -> None: # orient param accepting "tight" added in 1.4.0 https://pandas.pydata.org/docs/whatsnew/v1.4.0.html check(assert_type(data.to_dict("tight"), dict[str, list]), dict, str) - def _0(mapping: Mapping) -> None: # pyright: ignore[reportUnusedFunction] - data.to_dict(into=mapping) # type: ignore[call-overload] # pyright: ignore[reportArgumentType,reportCallIssue] + if TYPE_CHECKING_INVALID_USAGE: + + def test(mapping: Mapping) -> None: # pyright: ignore[reportUnusedFunction] + data.to_dict(into=mapping) # type: ignore[call-overload] # pyright: ignore[reportArgumentType,reportCallIssue] def _1() -> None: # pyright: ignore[reportUnusedFunction] assert_type(data.to_dict(into=defaultdict), Never) @@ -4411,7 +4413,6 @@ def test_select_dtypes() -> None: # include and exclude shall not be both empty def _0() -> None: # pyright: ignore[reportUnusedFunction] - assert_never(df.select_dtypes([], [])) if TYPE_CHECKING_INVALID_USAGE: From 036d326882a2fe0306c848c1c2d0f64989b3f2b6 Mon Sep 17 00:00:00 2001 From: cmp0xff Date: Tue, 11 Nov 2025 17:44:34 +0100 Subject: [PATCH 04/10] forbid inplace interpolate https://github.com/pandas-dev/pandas-stubs/pull/1479#discussion_r2514741321 --- pandas-stubs/core/resample.pyi | 13 ------------- pandas-stubs/core/series.pyi | 13 ------------- tests/test_groupby.py | 15 +-------------- tests/test_resampler.py | 13 ++++--------- 4 files changed, 5 insertions(+), 49 deletions(-) diff --git a/pandas-stubs/core/resample.pyi b/pandas-stubs/core/resample.pyi index 54c748a67..9f52c69ef 100644 --- a/pandas-stubs/core/resample.pyi +++ b/pandas-stubs/core/resample.pyi @@ -92,19 +92,6 @@ class Resampler(BaseGroupBy[NDFrameT]): def nearest(self, limit: int | None = ...) -> NDFrameT: ... @final def bfill(self, limit: int | None = ...) -> NDFrameT: ... - @overload - def interpolate( - self, - method: InterpolateOptions = ..., - *, - axis: Axis = ..., - limit: int | None = ..., - inplace: Literal[True], - limit_direction: Literal["forward", "backward", "both"] = ..., - limit_area: Literal["inside", "outside"] | None = ..., - **kwargs: Any, - ) -> None: ... - @overload def interpolate( self, method: InterpolateOptions = ..., diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index 96f73a950..0760c9a78 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -1525,19 +1525,6 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame): limit: int | None = ..., limit_area: Literal["inside", "outside"] | None = ..., ) -> Series[S1]: ... - @overload - def interpolate( - self, - method: InterpolateOptions = ..., - *, - axis: AxisIndex | None = 0, - limit: int | None = ..., - inplace: Literal[True], - limit_direction: Literal["forward", "backward", "both"] | None = ..., - limit_area: Literal["inside", "outside"] | None = ..., - **kwargs: Any, - ) -> None: ... - @overload def interpolate( self, method: InterpolateOptions = ..., diff --git a/tests/test_groupby.py b/tests/test_groupby.py index 20ec6119f..c278b0f44 100644 --- a/tests/test_groupby.py +++ b/tests/test_groupby.py @@ -259,22 +259,12 @@ def df2scalar(val: DataFrame) -> float: ), DataFrame, ) - check( - assert_type(GB_DF.resample("ME").interpolate(inplace=True), None), - type(None), - ) else: def resample_interpolate(x: DataFrame) -> DataFrame: return x.resample("ME").interpolate() - check( - assert_type( - GB_DF.apply(resample_interpolate), - DataFrame, - ), - DataFrame, - ) + check(assert_type(GB_DF.apply(resample_interpolate), DataFrame), DataFrame) def resample_interpolate_linear(x: DataFrame) -> DataFrame: return x.resample("ME").interpolate(method="linear") @@ -427,9 +417,6 @@ def f(val: Series) -> float: Series, float, ) - check( - assert_type(GB_S.resample("ME").interpolate(inplace=True), None), type(None) - ) else: check( assert_type( diff --git a/tests/test_resampler.py b/tests/test_resampler.py index 20ae58478..22861d3f8 100644 --- a/tests/test_resampler.py +++ b/tests/test_resampler.py @@ -18,7 +18,6 @@ from typing_extensions import assert_type from tests import ( - PD_LTE_23, TYPE_CHECKING_INVALID_USAGE, check, pytest_warns_bounded, @@ -147,11 +146,8 @@ def test_interpolate() -> None: def test_interpolate_inplace() -> None: - # TODO: ValueError: Cannot interpolate inplace on a resampled object. Disable in Pandas 3.0 pandas-dev/pandas#57064 - if PD_LTE_23: - check( - assert_type(DF.resample("ME").interpolate(inplace=True), None), type(None) - ) + if TYPE_CHECKING_INVALID_USAGE: + DF.resample("ME").interpolate(inplace=True) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] def test_pipe() -> None: @@ -362,9 +358,8 @@ def test_interpolate_series() -> None: def test_interpolate_inplace_series() -> None: - # TODO: ValueError: Cannot interpolate inplace on a resampled object. Disable in Pandas 3.0 pandas-dev/pandas#57064 - if PD_LTE_23: - check(assert_type(S.resample("ME").interpolate(inplace=True), None), type(None)) + if TYPE_CHECKING_INVALID_USAGE: + S.resample("ME").interpolate(inplace=True) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] def test_pipe_series() -> None: From d7cd661ae0666c27365dd718f5a6c01a2fc96a1b Mon Sep 17 00:00:00 2001 From: cmp0xff Date: Tue, 11 Nov 2025 19:00:45 +0100 Subject: [PATCH 05/10] fix: interpolate inplace --- pandas-stubs/core/resample.pyi | 1 - pandas-stubs/core/series.pyi | 13 +++++++++++++ tests/series/test_series.py | 11 +++++++++++ tests/test_frame.py | 2 +- tests/test_groupby.py | 7 ------- tests/test_resampler.py | 10 ---------- 6 files changed, 25 insertions(+), 19 deletions(-) diff --git a/pandas-stubs/core/resample.pyi b/pandas-stubs/core/resample.pyi index 9f52c69ef..58887d2cb 100644 --- a/pandas-stubs/core/resample.pyi +++ b/pandas-stubs/core/resample.pyi @@ -98,7 +98,6 @@ class Resampler(BaseGroupBy[NDFrameT]): *, axis: Axis = ..., limit: int | None = ..., - inplace: Literal[False] = False, limit_direction: Literal["forward", "backward", "both"] = ..., limit_area: Literal["inside", "outside"] | None = ..., **kwargs: Any, diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index 0760c9a78..96f73a950 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -1525,6 +1525,19 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame): limit: int | None = ..., limit_area: Literal["inside", "outside"] | None = ..., ) -> Series[S1]: ... + @overload + def interpolate( + self, + method: InterpolateOptions = ..., + *, + axis: AxisIndex | None = 0, + limit: int | None = ..., + inplace: Literal[True], + limit_direction: Literal["forward", "backward", "both"] | None = ..., + limit_area: Literal["inside", "outside"] | None = ..., + **kwargs: Any, + ) -> None: ... + @overload def interpolate( self, method: InterpolateOptions = ..., diff --git a/tests/series/test_series.py b/tests/series/test_series.py index 42abed3b7..ab332de7b 100644 --- a/tests/series/test_series.py +++ b/tests/series/test_series.py @@ -3213,6 +3213,17 @@ def test_to_json_mode() -> None: _result3 = s.to_json(orient="records", lines=False, mode="a") # type: ignore[call-overload] # pyright: ignore[reportArgumentType,reportCallIssue] +def test_interpolate() -> None: + s = pd.Series(range(3)) + check(assert_type(s.interpolate(method="linear"), "pd.Series[int]"), pd.Series, int) + check( + assert_type(s.interpolate(method="linear", inplace=False), "pd.Series[int]"), + pd.Series, + int, + ) + check(assert_type(s.interpolate(method="linear", inplace=True), None), type(None)) + + def test_groupby_diff() -> None: # GH 658 s = pd.Series([1.0, 2.0, 3.0, np.nan]) diff --git a/tests/test_frame.py b/tests/test_frame.py index d752aef1a..9c03fcd66 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -4447,7 +4447,7 @@ def test_to_json_mode() -> None: _result3 = df.to_json(orient="records", lines=False, mode="a") # type: ignore[call-overload] # pyright: ignore[reportArgumentType,reportCallIssue] -def test_interpolate_inplace() -> None: +def test_interpolate() -> None: # GH 691 df = pd.DataFrame({"a": range(3)}) check(assert_type(df.interpolate(method="linear"), pd.DataFrame), pd.DataFrame) diff --git a/tests/test_groupby.py b/tests/test_groupby.py index c278b0f44..a1145efd0 100644 --- a/tests/test_groupby.py +++ b/tests/test_groupby.py @@ -425,13 +425,6 @@ def f(val: Series) -> float: Series, float, ) - # TODO: This fails typing checks, and should work in 3.0, but is a bug in main pandas-dev/pandas#58690 - # check( - # assert_type( - # GB_S.apply(lambda x: x.resample("ME").interpolate(inplace=True)), None - # ), - # type(None), - # ) # pipe def g(val: Resampler[Series]) -> float: diff --git a/tests/test_resampler.py b/tests/test_resampler.py index 22861d3f8..61d1ac8d2 100644 --- a/tests/test_resampler.py +++ b/tests/test_resampler.py @@ -145,11 +145,6 @@ def test_interpolate() -> None: ) -def test_interpolate_inplace() -> None: - if TYPE_CHECKING_INVALID_USAGE: - DF.resample("ME").interpolate(inplace=True) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] - - def test_pipe() -> None: def f(val: "DatetimeIndexResampler[DataFrame]") -> DataFrame: assert isinstance(val, DatetimeIndexResampler) @@ -357,11 +352,6 @@ def test_interpolate_series() -> None: check(assert_type(S.resample("ME").interpolate(method="time"), Series), Series) -def test_interpolate_inplace_series() -> None: - if TYPE_CHECKING_INVALID_USAGE: - S.resample("ME").interpolate(inplace=True) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] - - def test_pipe_series() -> None: def f(val: "DatetimeIndexResampler[Series]") -> Series: assert isinstance(val, DatetimeIndexResampler) From bc6ed2d7d4a5b8c88dc3ee59df3e68163538729e Mon Sep 17 00:00:00 2001 From: cmp0xff Date: Tue, 11 Nov 2025 19:08:03 +0100 Subject: [PATCH 06/10] fix: pytest --- tests/series/test_series.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/series/test_series.py b/tests/series/test_series.py index ab332de7b..5152ff528 100644 --- a/tests/series/test_series.py +++ b/tests/series/test_series.py @@ -3215,11 +3215,15 @@ def test_to_json_mode() -> None: def test_interpolate() -> None: s = pd.Series(range(3)) - check(assert_type(s.interpolate(method="linear"), "pd.Series[int]"), pd.Series, int) + check( + assert_type(s.interpolate(method="linear"), "pd.Series[int]"), + pd.Series, + np.integer, + ) check( assert_type(s.interpolate(method="linear", inplace=False), "pd.Series[int]"), pd.Series, - int, + np.integer, ) check(assert_type(s.interpolate(method="linear", inplace=True), None), type(None)) From 62ceaee958d09b2750d0cb451f7b40a94dbc6527 Mon Sep 17 00:00:00 2001 From: cmp0xff Date: Tue, 11 Nov 2025 21:33:52 +0100 Subject: [PATCH 07/10] https://github.com/pandas-dev/pandas-stubs/pull/1479/files/bc6ed2d7d4a5b8c88dc3ee59df3e68163538729e#r2515210061 --- tests/test_groupby.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/tests/test_groupby.py b/tests/test_groupby.py index a1145efd0..b0287217b 100644 --- a/tests/test_groupby.py +++ b/tests/test_groupby.py @@ -259,25 +259,24 @@ def df2scalar(val: DataFrame) -> float: ), DataFrame, ) - else: - def resample_interpolate(x: DataFrame) -> DataFrame: - return x.resample("ME").interpolate() + def resample_interpolate(x: DataFrame) -> DataFrame: + return x.resample("ME").interpolate() - check(assert_type(GB_DF.apply(resample_interpolate), DataFrame), DataFrame) + check(assert_type(GB_DF.apply(resample_interpolate), DataFrame), DataFrame) - def resample_interpolate_linear(x: DataFrame) -> DataFrame: - return x.resample("ME").interpolate(method="linear") + def resample_interpolate_linear(x: DataFrame) -> DataFrame: + return x.resample("ME").interpolate(method="linear") - check( - assert_type( - GB_DF.apply( - resample_interpolate_linear, - ), - DataFrame, + check( + assert_type( + GB_DF.apply( + resample_interpolate_linear, ), DataFrame, - ) + ), + DataFrame, + ) # pipe def g(val: Resampler[DataFrame]) -> DataFrame: From b783f02caf8258260bb38e943e8c54274059846f Mon Sep 17 00:00:00 2001 From: cmp0xff Date: Tue, 11 Nov 2025 21:51:02 +0100 Subject: [PATCH 08/10] https://github.com/pandas-dev/pandas-stubs/pull/1479/files/d6592685321c6add64ff4bfd12c6984e5efa3d70#r2515222772 --- pandas-stubs/core/resample.pyi | 1 + tests/test_resampler.py | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/pandas-stubs/core/resample.pyi b/pandas-stubs/core/resample.pyi index 58887d2cb..9f52c69ef 100644 --- a/pandas-stubs/core/resample.pyi +++ b/pandas-stubs/core/resample.pyi @@ -98,6 +98,7 @@ class Resampler(BaseGroupBy[NDFrameT]): *, axis: Axis = ..., limit: int | None = ..., + inplace: Literal[False] = False, limit_direction: Literal["forward", "backward", "both"] = ..., limit_area: Literal["inside", "outside"] | None = ..., **kwargs: Any, diff --git a/tests/test_resampler.py b/tests/test_resampler.py index 61d1ac8d2..da6dae62a 100644 --- a/tests/test_resampler.py +++ b/tests/test_resampler.py @@ -2,6 +2,7 @@ Hashable, Iterator, ) +from typing import TypeAlias import numpy as np import pandas as pd @@ -18,11 +19,17 @@ from typing_extensions import assert_type from tests import ( + PD_LTE_23, TYPE_CHECKING_INVALID_USAGE, check, pytest_warns_bounded, ) +if not PD_LTE_23: + from pandas.errors import Pandas4Warning # type: ignore[attr-defined] # pyright: ignore[reportAttributeAccessIssue,reportRedeclaration] # isort: skip +else: + Pandas4Warning: TypeAlias = FutureWarning # type: ignore[no-redef] + DR = date_range("1999-1-1", periods=365, freq="D") DF_ = DataFrame(np.random.standard_normal((365, 1)), index=DR) S = DF_.iloc[:, 0] @@ -145,6 +152,21 @@ def test_interpolate() -> None: ) +# TODO: remove the whole test function when the warning and ValueError in pandas-dev/pandas#62847 are removed +def test_interpolate_inplace() -> None: + with pytest_warns_bounded( + Pandas4Warning, + "The 'inplace' keyword in DatetimeIndexResampler.interpolate is deprecated and will be removed in a future version. resample(...).interpolate is never inplace.", + lower="2.99", + ): + check( + assert_type(DF.resample("ME").interpolate(inplace=False), DataFrame), + DataFrame, + ) + if TYPE_CHECKING_INVALID_USAGE: + DF.resample("ME").interpolate(inplace=True) # type: ignore[call-arg] # pyright: ignore[reportArgumentType] + + def test_pipe() -> None: def f(val: "DatetimeIndexResampler[DataFrame]") -> DataFrame: assert isinstance(val, DatetimeIndexResampler) From c9b22a7af7fc9ca5a8f81199c3b1d5e89bcc6c71 Mon Sep 17 00:00:00 2001 From: cmp0xff Date: Tue, 11 Nov 2025 21:56:44 +0100 Subject: [PATCH 09/10] mypy --- tests/test_resampler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_resampler.py b/tests/test_resampler.py index da6dae62a..cf9ee4024 100644 --- a/tests/test_resampler.py +++ b/tests/test_resampler.py @@ -164,7 +164,7 @@ def test_interpolate_inplace() -> None: DataFrame, ) if TYPE_CHECKING_INVALID_USAGE: - DF.resample("ME").interpolate(inplace=True) # type: ignore[call-arg] # pyright: ignore[reportArgumentType] + DF.resample("ME").interpolate(inplace=True) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] def test_pipe() -> None: From a7a7b5bb5f002198f767f09cfb272a8a8d2471cf Mon Sep 17 00:00:00 2001 From: cmp0xff Date: Tue, 11 Nov 2025 22:47:35 +0100 Subject: [PATCH 10/10] regex escape https://github.com/pandas-dev/pandas-stubs/pull/1479#issuecomment-3518842508 --- tests/test_resampler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_resampler.py b/tests/test_resampler.py index cf9ee4024..3a5986cdf 100644 --- a/tests/test_resampler.py +++ b/tests/test_resampler.py @@ -156,7 +156,7 @@ def test_interpolate() -> None: def test_interpolate_inplace() -> None: with pytest_warns_bounded( Pandas4Warning, - "The 'inplace' keyword in DatetimeIndexResampler.interpolate is deprecated and will be removed in a future version. resample(...).interpolate is never inplace.", + r"The 'inplace' keyword in DatetimeIndexResampler.interpolate is deprecated and will be removed in a future version. resample\(...\).interpolate is never inplace.", lower="2.99", ): check(