Skip to content

Commit 661f7fa

Browse files
committed
Improve error messages in "dates".
1 parent 8274975 commit 661f7fa

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

domdf_python_tools/dates.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,19 +160,21 @@ def parse_month(month: Union[str, int]) -> str:
160160
:return: The full name of the month
161161
"""
162162

163+
error_text = f"The given month ({month!r}) is not recognised."
164+
163165
try:
164166
month = int(month)
165167
except ValueError:
166168
try:
167169
return months[month.capitalize()[:3]] # type: ignore
168170
except KeyError:
169-
raise ValueError("Unrecognised month value")
171+
raise ValueError(error_text)
170172

171173
# Only get here if first try succeeded
172174
if 0 < month <= 12:
173175
return list(months.values())[month - 1]
174176
else:
175-
raise ValueError("Unrecognised month value")
177+
raise ValueError(error_text)
176178

177179

178180
def get_month_number(month: Union[str, int]) -> int:
@@ -189,7 +191,7 @@ def get_month_number(month: Union[str, int]) -> int:
189191
if 0 < month <= 12:
190192
return month
191193
else:
192-
raise ValueError("The given month is not recognised.")
194+
raise ValueError(f"The given month ({month!r}) is not recognised.")
193195
else:
194196
month = parse_month(month)
195197
return list(months.values()).index(month) + 1
@@ -198,14 +200,15 @@ def get_month_number(month: Union[str, int]) -> int:
198200
def check_date(month: Union[str, int], day: int, leap_year: bool = True) -> bool:
199201
"""
200202
Returns :py:obj:`True` if the day number is valid for the given month.
201-
Note that this will return :py:obj:`True` for the 29th Feb. If you don't
202-
want this behaviour set ``leap_year`` to :py:obj:`False`.
203+
204+
.. note::
205+
206+
This function will return :py:obj:`True` for the 29th Feb.
207+
If you don't want this behaviour set ``leap_year`` to :py:obj:`False`.
203208
204209
:param month: The month to test.
205210
:param day: The day number to test.
206211
:param leap_year: Whether to return :py:obj:`True` for 29th Feb.
207-
208-
:return: :py:obj:`True` if the date is valid.
209212
"""
210213

211214
# Ensure day is an integer

tests/test_dates.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
# stdlib
1010
import datetime
11+
import re
1112
from datetime import date
1213

1314
# 3rd party
@@ -187,7 +188,7 @@ def test_parse_month():
187188
assert dates.parse_month(month_idx) == month
188189

189190
for value in ["abc", 0, '0', -1, "-1", 13, "13"]:
190-
with pytest.raises(ValueError, match="Unrecognised month value"):
191+
with pytest.raises(ValueError, match=fr"The given month \({value!r}\) is not recognised."):
191192
dates.parse_month(value) # type: ignore
192193

193194

@@ -211,17 +212,17 @@ def test_get_month_number_from_no(count):
211212
@pytest.mark.parametrize(
212213
"value, match",
213214
[
214-
(0, "The given month is not recognised."),
215-
(-1, "The given month is not recognised."),
216-
(13, "The given month is not recognised."),
217-
("abc", "Unrecognised month value"),
218-
('0', "Unrecognised month value"),
219-
("-1", "Unrecognised month value"),
220-
("13", "Unrecognised month value"),
215+
(0, "The given month (0) is not recognised."),
216+
(-1, "The given month (-1) is not recognised."),
217+
(13, "The given month (13) is not recognised."),
218+
("abc", "The given month ('abc') is not recognised."),
219+
('0', "The given month ('0') is not recognised."),
220+
("-1", "The given month ('-1') is not recognised."),
221+
("13", "The given month ('13') is not recognised."),
221222
]
222223
)
223224
def test_get_month_number_errors(value, match):
224-
with pytest.raises(ValueError, match=match):
225+
with pytest.raises(ValueError, match=re.escape(match)):
225226
dates.get_month_number(value)
226227

227228

0 commit comments

Comments
 (0)