Skip to content

Commit 933d405

Browse files
authored
BUG: #1786 resume supporting numpy 2.5 (#1802)
* fix: partially support numpy 2-5 * fix: typing * fix: typing * fix: rebase * fix: pyrefly * fix: tests * fix: installed stubs * fix: https://github.com/pandas-dev/pandas-stubs/actions/runs/28395061571 * remove _dist discrepency * force numpy 2.5 for python >= 3.12 for local stubs; without this change, for python 3.12 and 3.13, numpy 2.4 will be installed * conditional warnings * pyrefly issue * python/mypy#21733 * conditional warnings revisited * deduplicate + black * regressions in pyrefly * #1802 (review) * revert a non-functional change
1 parent d799479 commit 933d405

11 files changed

Lines changed: 276 additions & 113 deletions

File tree

‎pyproject.toml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ classifiers = [
2121
"Topic :: Scientific/Engineering",
2222
"Typing :: Stubs Only",
2323
]
24-
dependencies = ["numpy>=1.23.5,<2.5"] # TODO: pandas-dev/pandas-stubs#1786 remove <2.5
24+
dependencies = ["numpy>=1.23.5"]
2525

2626
[project.urls]
2727
Homepage = "https://pandas.pydata.org"
@@ -33,6 +33,7 @@ Documentation = "https://pandas.pydata.org/pandas-docs/stable"
3333
dev = [
3434
"mypy>=2.2.0",
3535
"pandas==3.0.3",
36+
"numpy>=2.5.1;python_version>='3.12'",
3637
"pyarrow>=10.0.1",
3738
"pytest>=8.4.2",
3839
"pyright>=1.1.410",
@@ -69,7 +70,7 @@ dev = [
6970
"pytz>=2025.2",
7071
"types-pytz>=2022.1.1",
7172
"pyiceberg>=0.10.0",
72-
"numpy-typing-compat>=20260602",
73+
"optype[numpy]"
7374
]
7475

7576
[build-system]

‎scripts/test/run.py‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,18 @@ def build_dist() -> None:
5050

5151
def install_dist() -> None:
5252
path = sorted(Path("dist/").glob("pandas_stubs-*.whl"))[-1]
53-
cmd = [sys.executable, "-m", "pip", "install", "--force-reinstall", str(path)]
53+
cmd = [
54+
sys.executable,
55+
"-m",
56+
"pip",
57+
"install",
58+
"--upgrade",
59+
"--force-reinstall",
60+
str(path),
61+
"numpy-typing-compat",
62+
]
5463
subprocess.run(cmd, check=True)
64+
subprocess.run([sys.executable, "-m", "pip", "check"], check=True)
5565

5666

5767
def rename_src() -> None:

‎tests/__init__.py‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
WINDOWS = sys.platform in {"win32", "cygwin"}
4141
MAC = sys.platform == "darwin"
4242
PD_LTE_31 = Version(pd.__version__) < Version("3.0.99")
43+
NP_GTE_25 = Version(np.__version__) >= Version("2.5.0")
4344

4445

4546
def check(
@@ -189,7 +190,16 @@ def pytest_warns_bounded(
189190
current = Version(pd.__version__)
190191
else:
191192
current = Version(version_str)
192-
if lb < current < ub:
193+
return pytest_warns_conditioned(warning, match, lb < current < ub, upper_exception)
194+
195+
196+
def pytest_warns_conditioned(
197+
warning: type[Warning],
198+
match: str,
199+
condition: bool,
200+
upper_exception: type[Exception] | None = None,
201+
) -> AbstractContextManager[Any]:
202+
if condition:
193203
return pytest.warns(warning, match=match)
194204
if upper_exception is None:
195205
return nullcontext()

‎tests/arrays/test_datetime_array.py‎

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
datetime,
99
time,
1010
)
11+
import sys
1112
from typing import (
1213
TYPE_CHECKING,
1314
Any,
@@ -78,7 +79,11 @@ def test_construction_sequence_pandas(
7879
pd.array([pd.Timestamp(2026, 1, 5, tzinfo=ZoneInfo("Africa/Ouagadougou"))]),
7980
DatetimeArray,
8081
)
81-
assert_type(pd.array([np.datetime64("2026-01-05 23:27:59")]), DatetimeArray)
82+
if sys.version_info >= (3, 12):
83+
# TODO: python/mypy#21733 the mypy bug has manifested in numpy >= 2.5
84+
assert_type(pd.array([np.datetime64("2026-01-05 23:27:59")]), DatetimeArray) # type: ignore[assert-type]
85+
else:
86+
assert_type(pd.array([np.datetime64("2026-01-05 23:27:59")]), DatetimeArray)
8287

8388
assert_type(
8489
pd.array([datetime(2100, 1, 5, 1), datetime(1, 1, 6, 2)]), DatetimeArray
@@ -89,10 +94,16 @@ def test_construction_sequence_pandas(
8994
assert_type(
9095
pd.array([datetime(2052, 1, 5), pd.Timestamp(2, 1, 6)]), DatetimeArray
9196
)
92-
assert_type(
93-
pd.array([np.datetime64("2131-01-05 01:25"), np.datetime64("1748-01-06")]),
94-
DatetimeArray,
95-
)
97+
if sys.version_info >= (3, 12):
98+
# TODO: python/mypy#21733 the mypy bug has manifested in numpy >= 2.5
99+
assert_type(pd.array([np.datetime64("2131-01-05 23:27:59"), np.datetime64("1748-01-06")]), DatetimeArray) # type: ignore[assert-type]
100+
else:
101+
assert_type(
102+
pd.array(
103+
[np.datetime64("2131-01-05 23:27:59"), np.datetime64("1748-01-06")]
104+
),
105+
DatetimeArray,
106+
)
96107
assert_type(
97108
pd.array([np.datetime64("2130-01-01 01:25"), datetime(1749, 1, 6)]),
98109
DatetimeArray,
@@ -104,7 +115,11 @@ def test_construction_sequence_pandas(
104115

105116
assert_type(pd.array([datetime(2061, 1, 5, 1), None]), DatetimeArray)
106117
assert_type(pd.array([pd.Timestamp(1902, 1, 5, 3), None]), DatetimeArray)
107-
assert_type(pd.array([np.datetime64("2111-01-05"), None]), DatetimeArray)
118+
if sys.version_info >= (3, 12):
119+
# TODO: python/mypy#21733 the mypy bug has manifested in numpy >= 2.5
120+
assert_type(pd.array([np.datetime64("2111-01-05"), None]), DatetimeArray) # type: ignore[assert-type]
121+
else:
122+
assert_type(pd.array([np.datetime64("2111-01-05"), None]), DatetimeArray)
108123

109124
assert_type(pd.array([datetime(1921, 1, 5, 1), pd.NaT]), DatetimeArray)
110125
assert_type(pd.array([pd.Timestamp(1872, 1, 5, 3), pd.NaT]), DatetimeArray)
@@ -114,14 +129,25 @@ def test_construction_sequence_pandas(
114129
assert_type(
115130
pd.array([pd.Timestamp(2102, 1, 5, 3), None, pd.NaT]), DatetimeArray
116131
)
117-
assert_type(
118-
pd.array([np.datetime64("2114-01-05"), None, pd.NaT]), DatetimeArray
119-
)
132+
if sys.version_info >= (3, 12):
133+
# TODO: python/mypy#21733 the mypy bug has manifested in numpy >= 2.5
134+
assert_type(pd.array([np.datetime64("2114-01-05"), None, pd.NaT]), DatetimeArray) # type: ignore[assert-type]
135+
else:
136+
assert_type(
137+
pd.array([np.datetime64("2114-01-05"), None, pd.NaT]), DatetimeArray
138+
)
120139

121140
assert_type(pd.array((datetime(2026, 1, 5),)), DatetimeArray)
122141
assert_type(pd.array(UserList([pd.Timestamp(2026, 1, 5)])), DatetimeArray)
123-
assert_type(pd.array((np.datetime64("1959-01-05"),)), DatetimeArray)
124-
assert_type(pd.array(UserList([np.datetime64("1701-01-05")])), DatetimeArray)
142+
if sys.version_info >= (3, 12):
143+
# TODO: python/mypy#21733 the mypy bug has manifested in numpy >= 2.5
144+
assert_type(pd.array((np.datetime64("1959-01-05"),)), DatetimeArray) # type: ignore[assert-type]
145+
assert_type(pd.array(UserList([np.datetime64("1701-01-05")])), DatetimeArray) # type: ignore[assert-type]
146+
else:
147+
assert_type(pd.array((np.datetime64("1959-01-05"),)), DatetimeArray)
148+
assert_type(
149+
pd.array(UserList([np.datetime64("1701-01-05")])), DatetimeArray
150+
)
125151

126152

127153
@pytest.mark.parametrize(
@@ -310,7 +336,11 @@ def test_constructor() -> None:
310336
check(assert_type(pd.array([np_dt]), DatetimeArray), DatetimeArray)
311337
check(assert_type(pd.array([np_dt, None]), DatetimeArray), DatetimeArray)
312338
dt_nat = cast(list[np.datetime64 | NaTType], [np_dt, pd.NaT])
313-
check(assert_type(pd.array(dt_nat), DatetimeArray), DatetimeArray)
339+
if sys.version_info >= (3, 12):
340+
# TODO: python/mypy#21733 the mypy bug has manifested in numpy >= 2.5
341+
check(assert_type(pd.array(dt_nat), DatetimeArray), DatetimeArray) # type: ignore[assert-type]
342+
else:
343+
check(assert_type(pd.array(dt_nat), DatetimeArray), DatetimeArray)
314344

315345
np_arr = np.array([dt], np.datetime64)
316346
check(assert_type(pd.array(np_arr), DatetimeArray), DatetimeArray)

‎tests/arrays/test_integer.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ def test_construction_sequence(
4646

4747
if TYPE_CHECKING:
4848
assert_type(pd.array([-2, 3]), IntegerArray)
49-
assert_type(pd.array([1 << 32, np.int8(1) << 6]), IntegerArray)
49+
# TODO: pandas-dev/pandas-stubs#1786 investigate and report to pyrefly
50+
assert_type( # pyrefly: ignore[assert-type]
51+
pd.array([1 << 32, np.int8(1) << 6]), IntegerArray
52+
)
5053

5154
assert_type(pd.array([2, np.int8(3)]), IntegerArray)
5255

‎tests/arrays/test_timedeltas.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_construction() -> None:
3535
"""Test pd.array method for TimedeltaArray."""
3636

3737
td = timedelta(2025, 11, 10)
38-
np_dt = np.timedelta64(td)
38+
np_dt = np.timedelta64(td, "D")
3939
check(assert_type(pd.array([td]), TimedeltaArray), TimedeltaArray)
4040
check(
4141
assert_type(pd.array([td, pd.Timedelta(td), np_dt]), TimedeltaArray),

0 commit comments

Comments
 (0)