Skip to content

Commit d995243

Browse files
test, linter fixes
1 parent e788c25 commit d995243

File tree

10 files changed

+82
-89
lines changed

10 files changed

+82
-89
lines changed

pandas/_libs/tslibs/timedeltas.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ from cpython.datetime cimport (
2929
import_datetime()
3030

3131

32-
from pandas._libs cimport ops
3332
cimport pandas._libs.tslibs.util as util
33+
from pandas._libs cimport ops
3434
from pandas._libs.tslibs.base cimport ABCTimestamp
3535
from pandas._libs.tslibs.conversion cimport (
3636
cast_from_unit,

pandas/_libs/tslibs/timestamps.pyx

+4-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ from pandas._libs.tslibs.np_datetime cimport (
8686
pydatetime_to_dt64,
8787
)
8888

89-
from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime, OutOfBoundsTimedelta
89+
from pandas._libs.tslibs.np_datetime import (
90+
OutOfBoundsDatetime,
91+
OutOfBoundsTimedelta,
92+
)
9093

9194
from pandas._libs.tslibs.offsets cimport (
9295
BaseOffset,

pandas/tests/libs/test_ops.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ def fixture_int_min() -> int:
1818

1919
@pytest.fixture(name="overflow_msg")
2020
def fixture_overflow_msg() -> str:
21-
return "Python int too large to convert to C long"
21+
return "|".join(
22+
(
23+
"Python int too large to convert to C long",
24+
"int too big to convert",
25+
)
26+
)
2227

2328

2429
def test_raises_for_too_large_arg(int_max: int, overflow_msg: str):

pandas/tests/scalar/conftest.py

-18
This file was deleted.

pandas/tests/scalar/timedelta/test_arithmetic.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
timedelta,
77
)
88
import operator
9+
import re
910

1011
import numpy as np
1112
import pytest
1213

14+
from pandas._libs.tslibs import OutOfBoundsTimedelta
15+
1316
import pandas as pd
1417
from pandas import (
1518
NaT,
@@ -96,11 +99,14 @@ def test_td_add_datetimelike_scalar(self, op):
9699
result = op(td, NaT)
97100
assert result is NaT
98101

99-
def test_td_add_timestamp_overflow(self, timedelta_overflow):
100-
with pytest.raises(**timedelta_overflow):
102+
def test_td_add_timestamp_overflow(self):
103+
msg = msg = re.escape(
104+
"outside allowed range [-9223372036854775807ns, 9223372036854775807ns]"
105+
)
106+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
101107
Timestamp("1700-01-01") + Timedelta(13 * 19999, unit="D")
102108

103-
with pytest.raises(**timedelta_overflow):
109+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
104110
Timestamp("1700-01-01") + timedelta(days=13 * 19999)
105111

106112
@pytest.mark.parametrize("op", [operator.add, ops.radd])

pandas/tests/scalar/timedelta/test_constructors.py

+47-35
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import numpy as np
99
import pytest
1010

11+
from pandas._libs.tslibs import OutOfBoundsTimedelta
12+
1113
from pandas import (
1214
NA,
1315
NaT,
@@ -163,16 +165,19 @@ def test_construction():
163165

164166

165167
@pytest.mark.parametrize("unit", ("ps", "ns"))
166-
def test_from_np_td64_ignores_unit(unit: str, timedelta_overflow):
168+
def test_from_np_td64_ignores_unit(unit: str):
167169
"""
168170
Ignore the unit, as it may cause silently overflows leading to incorrect results,
169171
and in non-overflow cases is irrelevant GH#46827.
170172
"""
171173
td64 = np.timedelta64(NP_TD64_MAX_PER_UNIT["h"], "h")
174+
msg = re.escape(
175+
"outside allowed range [-9223372036854775807ns, 9223372036854775807ns]"
176+
)
172177

173178
assert Timedelta(td64, unit=unit) == Timedelta(td64)
174179

175-
with pytest.raises(**timedelta_overflow):
180+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
176181
Timedelta(td64 * 2, unit=unit)
177182

178183

@@ -448,13 +453,7 @@ def test_raises_if_ambiguous_units_passed(self, unit: str):
448453

449454
class TestOverflow:
450455
@pytest.mark.parametrize(("unit", "max_val"), TD_MAX_PER_UNIT.items())
451-
def test_int_plus_units_too_big(
452-
self,
453-
unit: str,
454-
max_val: int,
455-
request,
456-
timedelta_overflow,
457-
):
456+
def test_int_plus_units_too_big(self, unit: str, max_val: int, request):
458457
if unit == "w":
459458
mark = pytest.mark.xfail(
460459
reason="does not raise",
@@ -464,18 +463,15 @@ def test_int_plus_units_too_big(
464463
request.node.add_marker(mark)
465464

466465
too_big = max_val + 1
466+
msg = re.escape(
467+
"outside allowed range [-9223372036854775807ns, 9223372036854775807ns]"
468+
)
467469

468-
with pytest.raises(**timedelta_overflow):
470+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
469471
Timedelta(too_big, unit=unit)
470472

471473
@pytest.mark.parametrize(("unit", "min_val"), skip_ns(TD_MIN_PER_UNIT).items())
472-
def test_int_plus_units_too_small(
473-
self,
474-
unit: str,
475-
min_val: int,
476-
request,
477-
timedelta_overflow,
478-
):
474+
def test_int_plus_units_too_small(self, unit: str, min_val: int, request):
479475
if unit == "w":
480476
mark = pytest.mark.xfail(
481477
reason="does not raise",
@@ -485,55 +481,71 @@ def test_int_plus_units_too_small(
485481
request.node.add_marker(mark)
486482

487483
too_small = min_val - 1
484+
msg = re.escape(
485+
"outside allowed range [-9223372036854775807ns, 9223372036854775807ns]"
486+
)
488487

489-
with pytest.raises(**timedelta_overflow):
488+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
490489
Timedelta(too_small, unit=unit)
491490

492491
@pytest.mark.parametrize(("kwarg", "max_val"), TD_MAX_PER_KWARG.items())
493-
def test_kwarg_too_big(self, kwarg: str, max_val: int, timedelta_overflow):
492+
def test_kwarg_too_big(self, kwarg: str, max_val: int):
494493
too_big = max_val + 1
494+
msg = re.escape(
495+
"outside allowed range [-9223372036854775807ns, 9223372036854775807ns]"
496+
)
495497

496-
with pytest.raises(**timedelta_overflow):
498+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
497499
assert Timedelta(**{kwarg: too_big}) # type: ignore[arg-type]
498500

499501
@pytest.mark.parametrize(("kwarg", "min_val"), skip_ns(TD_MIN_PER_KWARG).items())
500-
def test_kwarg_too_small(self, kwarg: str, min_val: int, timedelta_overflow):
502+
def test_kwarg_too_small(self, kwarg: str, min_val: int):
501503
too_small = min_val - 1
504+
msg = re.escape(
505+
"outside allowed range [-9223372036854775807ns, 9223372036854775807ns]"
506+
)
502507

503-
with pytest.raises(**timedelta_overflow):
508+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
504509
Timedelta(**{kwarg: too_small}) # type: ignore[arg-type]
505510

506511
@pytest.mark.parametrize(("kwarg", "max_val"), skip_ns(TD_MAX_PER_KWARG).items())
507-
def test_from_timedelta_too_big(self, kwarg: str, max_val: int, timedelta_overflow):
512+
def test_from_timedelta_too_big(self, kwarg: str, max_val: int):
508513
too_big = timedelta(**{kwarg: max_val + 1})
514+
msg = re.escape(
515+
"outside allowed range [-9223372036854775807ns, 9223372036854775807ns]"
516+
)
509517

510-
with pytest.raises(**timedelta_overflow):
518+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
511519
Timedelta(too_big)
512520

513521
@pytest.mark.parametrize(("kwarg", "min_val"), skip_ns(TD_MIN_PER_KWARG).items())
514-
def test_from_timedelta_too_small(
515-
self,
516-
kwarg: str,
517-
min_val: int,
518-
timedelta_overflow,
519-
):
522+
def test_from_timedelta_too_small(self, kwarg: str, min_val: int):
520523
too_small = timedelta(**{kwarg: min_val - 1})
524+
msg = re.escape(
525+
"outside allowed range [-9223372036854775807ns, 9223372036854775807ns]"
526+
)
521527

522-
with pytest.raises(**timedelta_overflow):
528+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
523529
Timedelta(too_small)
524530

525531
@pytest.mark.parametrize(("unit", "max_val"), skip_ns(NP_TD64_MAX_PER_UNIT).items())
526-
def test_from_np_td64_too_big(self, unit: str, max_val: int, timedelta_overflow):
532+
def test_from_np_td64_too_big(self, unit: str, max_val: int):
527533
too_big = np.timedelta64(max_val + 1, unit)
534+
msg = re.escape(
535+
"outside allowed range [-9223372036854775807ns, 9223372036854775807ns]"
536+
)
528537

529-
with pytest.raises(**timedelta_overflow):
538+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
530539
Timedelta(too_big)
531540

532541
@pytest.mark.parametrize(("unit", "min_val"), skip_ns(NP_TD64_MIN_PER_UNIT).items())
533-
def test_from_np_td64_too_small(self, unit: str, min_val: int, timedelta_overflow):
542+
def test_from_np_td64_too_small(self, unit: str, min_val: int):
534543
too_small = np.timedelta64(min_val - 1, unit)
544+
msg = re.escape(
545+
"outside allowed range [-9223372036854775807ns, 9223372036854775807ns]"
546+
)
535547

536-
with pytest.raises(**timedelta_overflow):
548+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
537549
Timedelta(too_small)
538550

539551
def test_too_small_by_1ns_returns_nat(self):

pandas/tests/scalar/timedelta/test_timedelta.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
""" test the scalar Timedelta """
22
from datetime import timedelta
3+
import re
34

45
from hypothesis import (
56
given,
@@ -11,6 +12,7 @@
1112
from pandas._libs import lib
1213
from pandas._libs.tslibs import (
1314
NaT,
15+
OutOfBoundsTimedelta,
1416
iNaT,
1517
)
1618

@@ -646,7 +648,7 @@ def test_timedelta_hash_equality(self):
646648
ns_td = Timedelta(1, "ns")
647649
assert hash(ns_td) != hash(ns_td.to_pytimedelta())
648650

649-
def test_implementation_limits(self, timedelta_overflow):
651+
def test_implementation_limits(self):
650652
min_td = Timedelta(Timedelta.min)
651653
max_td = Timedelta(Timedelta.max)
652654

@@ -658,20 +660,23 @@ def test_implementation_limits(self, timedelta_overflow):
658660
# Beyond lower limit, a NAT before the Overflow
659661
assert (min_td - Timedelta(1, "ns")) is NaT
660662

661-
with pytest.raises(**timedelta_overflow):
663+
msg = re.escape(
664+
"outside allowed range [-9223372036854775807ns, 9223372036854775807ns]"
665+
)
666+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
662667
min_td - Timedelta(2, "ns")
663668

664-
with pytest.raises(**timedelta_overflow):
669+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
665670
max_td + Timedelta(1, "ns")
666671

667672
# Same tests using the internal nanosecond values
668673
td = Timedelta(min_td.value - 1, "ns")
669674
assert td is NaT
670675

671-
with pytest.raises(**timedelta_overflow):
676+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
672677
Timedelta(min_td.value - 2, "ns")
673678

674-
with pytest.raises(**timedelta_overflow):
679+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
675680
Timedelta(max_td.value + 1, "ns")
676681

677682
def test_total_seconds_precision(self):

pandas/tests/scalar/timestamp/test_arithmetic.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import pytest
99

1010
from pandas._libs.tslibs import (
11-
OutOfBoundsDatetime,
11+
OutOfBoundsTimedelta,
1212
Timedelta,
1313
Timestamp,
1414
offsets,
@@ -72,13 +72,13 @@ def test_overflow_offset_raises(self):
7272
with pytest.raises(OverflowError, match=lmsg):
7373
stamp - offset_overflow
7474

75-
def test_sub_returns_stdlib_timedelta_to_avoid_overflow(self, timedelta_overflow):
75+
def test_sub_returns_stdlib_timedelta_to_avoid_overflow(self):
7676
# https://github.com/pandas-dev/pandas/issues/31774
7777
msg = "Result is too large for pandas.Timedelta"
7878
a = Timestamp("2101-01-01 00:00:00")
7979
b = Timestamp("1688-01-01 00:00:00")
8080

81-
with pytest.raises(timedelta_overflow["expected_exception"], match=msg):
81+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
8282
a - b
8383

8484
# but we're OK for Timestamp and datetime.datetime

pandas/tests/tools/test_to_timedelta.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,7 @@ def test_to_timedelta_invalid_unit(self, arg):
109109

110110
def test_to_timedelta_time(self):
111111
# time not supported ATM
112-
msg = (
113-
"Value must be Timedelta, string, integer, float, timedelta or convertible"
114-
)
112+
msg = "Invalid type for timedelta scalar: <class 'datetime.time'>"
115113
with pytest.raises(ValueError, match=msg):
116114
to_timedelta(time(second=1))
117115
assert to_timedelta(time(second=1), errors="coerce") is pd.NaT
@@ -264,10 +262,7 @@ def test_to_timedelta_zerodim(self, fixed_now_ts):
264262
dt64 = fixed_now_ts.to_datetime64()
265263
arg = np.array(dt64)
266264

267-
msg = (
268-
"Value must be Timedelta, string, integer, float, timedelta "
269-
"or convertible, not datetime64"
270-
)
265+
msg = "Invalid type for timedelta scalar: <class 'numpy.datetime64'>"
271266
with pytest.raises(ValueError, match=msg):
272267
to_timedelta(arg)
273268

pandas/tests/tslibs/test_timedeltas.py

-15
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,6 @@ def test_huge_nanoseconds_overflow():
6161
assert delta_to_nanoseconds(Timedelta(nanoseconds=1e10)) == 1e10
6262

6363

64-
@pytest.mark.parametrize(
65-
"kwargs", [{"Seconds": 1}, {"seconds": 1, "Nanoseconds": 1}, {"Foo": 2}]
66-
)
67-
def test_kwarg_assertion(kwargs):
68-
err_message = (
69-
"cannot construct a Timedelta from the passed arguments, "
70-
"allowed keywords are "
71-
"[weeks, days, hours, minutes, seconds, "
72-
"milliseconds, microseconds, nanoseconds]"
73-
)
74-
75-
with pytest.raises(ValueError, match=re.escape(err_message)):
76-
Timedelta(**kwargs)
77-
78-
7964
class TestArrayToTimedelta64:
8065
def test_array_to_timedelta64_string_with_unit_2d_raises(self):
8166
# check the 'unit is not None and errors != "coerce"' path

0 commit comments

Comments
 (0)