Skip to content

Commit b80523a

Browse files
committed
broaden if check; specify error message
1 parent af3f19e commit b80523a

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

pandas/tests/tseries/holiday/test_holiday.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ def test_list_of_list_of_offsets_raises():
280280
day=USThanksgivingDay.day,
281281
offset=[USThanksgivingDay.offset, DateOffset(1)],
282282
)
283-
with pytest.raises(ValueError, match="Nested lists are not supported for offset"):
283+
msg = "Only BaseOffsets and flat lists thereof are supported for offset."
284+
with pytest.raises(ValueError, match=msg):
284285
Holiday(
285286
"Holiday2",
286287
month=holiday1.month,

pandas/tseries/holiday.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
datetime,
55
timedelta,
66
)
7-
from typing import (
8-
TYPE_CHECKING,
9-
Callable,
10-
)
7+
from typing import Callable
118
import warnings
129

1310
from dateutil.relativedelta import (
@@ -21,6 +18,7 @@
2118
)
2219
import numpy as np
2320

21+
from pandas._libs.tslibs.offsets import BaseOffset
2422
from pandas.errors import PerformanceWarning
2523

2624
from pandas import (
@@ -37,9 +35,6 @@
3735
Easter,
3836
)
3937

40-
if TYPE_CHECKING:
41-
from pandas._libs.tslibs.offsets import BaseOffset
42-
4338

4439
def next_monday(dt: datetime) -> datetime:
4540
"""
@@ -156,7 +151,6 @@ class Holiday:
156151
for observance.
157152
"""
158153

159-
offset: BaseOffset | list[BaseOffset] | None
160154
start_date: Timestamp | None
161155
end_date: Timestamp | None
162156
days_of_week: tuple[int, ...] | None
@@ -234,13 +228,19 @@ class from pandas.tseries.offsets, default None
234228
>>> July3rd
235229
Holiday: July 3rd (month=7, day=3, )
236230
"""
237-
if offset is not None and observance is not None:
238-
raise NotImplementedError("Cannot use both offset and observance.")
239-
if isinstance(offset, list) and any(isinstance(off, list) for off in offset):
240-
raise ValueError(
241-
"Nested lists are not supported for offset. "
242-
"Flatten composite offsets of `Holiday.offset`s first."
243-
)
231+
if offset is not None:
232+
if observance is not None:
233+
raise NotImplementedError("Cannot use both offset and observance.")
234+
if not (
235+
isinstance(offset, BaseOffset)
236+
or (
237+
isinstance(offset, list)
238+
and all(isinstance(off, BaseOffset) for off in offset)
239+
)
240+
):
241+
raise ValueError(
242+
"Only BaseOffsets and flat lists of them are supported for offset."
243+
)
244244

245245
self.name = name
246246
self.year = year

0 commit comments

Comments
 (0)