Skip to content

Commit 764eb1b

Browse files
committed
Update error messages to be the same in datetime
1 parent 78cb377 commit 764eb1b

File tree

2 files changed

+40
-30
lines changed

2 files changed

+40
-30
lines changed

Lib/_pydatetime.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ def _days_in_month(year, month):
6060

6161
def _days_before_month(year, month):
6262
"year, month -> number of days in year preceding first day of month."
63-
assert 1 <= month <= 12, 'month must be in 1..12'
63+
assert 1 <= month <= 12, f"month must be in 1..12, but got {month}"
6464
return _DAYS_BEFORE_MONTH[month] + (month > 2 and _is_leap(year))
6565

6666
def _ymd2ord(year, month, day):
6767
"year, month, day -> ordinal, considering 01-Jan-0001 as day 1."
68-
assert 1 <= month <= 12, 'month must be in 1..12'
68+
assert 1 <= month <= 12, f"month must be in 1..12, but got {month}"
6969
dim = _days_in_month(year, month)
70-
assert 1 <= day <= dim, ('day must be in 1..%d' % dim)
70+
assert 1 <= day <= dim, f"day must be in 1..{dim}, but got {day}"
7171
return (_days_before_year(year) +
7272
_days_before_month(year, month) +
7373
day)
@@ -512,7 +512,7 @@ def _parse_isoformat_time(tstr):
512512
def _isoweek_to_gregorian(year, week, day):
513513
# Year is bounded this way because 9999-12-31 is (9999, 52, 5)
514514
if not MINYEAR <= year <= MAXYEAR:
515-
raise ValueError(f"Year is out of range: {year}")
515+
raise ValueError(f"year must be in {MINYEAR}..{MAXYEAR}, but got {year}")
516516

517517
if not 0 < week < 53:
518518
out_of_range = True
@@ -561,21 +561,21 @@ def _check_utc_offset(name, offset):
561561
raise TypeError("tzinfo.%s() must return None "
562562
"or timedelta, not '%s'" % (name, type(offset)))
563563
if not -timedelta(1) < offset < timedelta(1):
564-
raise ValueError("%s()=%s, must be strictly between "
565-
"-timedelta(hours=24) and timedelta(hours=24)" %
566-
(name, offset))
564+
raise ValueError("offset must be a timedelta "
565+
"strictly between -timedelta(hours=24) and "
566+
f"timedelta(hours=24), not {offset.__repr__()}")
567567

568568
def _check_date_fields(year, month, day):
569569
year = _index(year)
570570
month = _index(month)
571571
day = _index(day)
572572
if not MINYEAR <= year <= MAXYEAR:
573-
raise ValueError('year must be in %d..%d' % (MINYEAR, MAXYEAR), year)
573+
raise ValueError(f"year must be in {MINYEAR}..{MAXYEAR}, but got {year}")
574574
if not 1 <= month <= 12:
575-
raise ValueError('month must be in 1..12', month)
575+
raise ValueError(f"month must be in 1..12, but got {month}")
576576
dim = _days_in_month(year, month)
577577
if not 1 <= day <= dim:
578-
raise ValueError('day must be in 1..%d' % dim, day)
578+
raise ValueError(f"day must be in 1..{dim}, but got {day}")
579579
return year, month, day
580580

581581
def _check_time_fields(hour, minute, second, microsecond, fold):
@@ -584,15 +584,15 @@ def _check_time_fields(hour, minute, second, microsecond, fold):
584584
second = _index(second)
585585
microsecond = _index(microsecond)
586586
if not 0 <= hour <= 23:
587-
raise ValueError('hour must be in 0..23', hour)
587+
raise ValueError(f"hour must be in 0..23, but got {hour}")
588588
if not 0 <= minute <= 59:
589-
raise ValueError('minute must be in 0..59', minute)
589+
raise ValueError(f"minute must be in 0..59, but got {minute}")
590590
if not 0 <= second <= 59:
591-
raise ValueError('second must be in 0..59', second)
591+
raise ValueError(f"second must be in 0..59, but got {second}")
592592
if not 0 <= microsecond <= 999999:
593-
raise ValueError('microsecond must be in 0..999999', microsecond)
593+
raise ValueError(f"microsecond must be in 0..999999, but got {microsecond}")
594594
if fold not in (0, 1):
595-
raise ValueError('fold must be either 0 or 1', fold)
595+
raise ValueError(f"fold must be either 0 or 1, but got {fold}")
596596
return hour, minute, second, microsecond, fold
597597

598598
def _check_tzinfo_arg(tz):
@@ -2419,7 +2419,7 @@ def __new__(cls, offset, name=_Omitted):
24192419
if not cls._minoffset <= offset <= cls._maxoffset:
24202420
raise ValueError("offset must be a timedelta "
24212421
"strictly between -timedelta(hours=24) and "
2422-
"timedelta(hours=24).")
2422+
f"timedelta(hours=24), not {offset.__repr__()}")
24232423
return cls._create(offset, name)
24242424

24252425
def __init_subclass__(cls):

Modules/_datetimemodule.c

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -637,17 +637,22 @@ check_date_args(int year, int month, int day)
637637
{
638638

639639
if (year < MINYEAR || year > MAXYEAR) {
640-
PyErr_Format(PyExc_ValueError, "year %i is out of range", year);
640+
PyErr_Format(PyExc_ValueError,
641+
"year must be in %d..%d, but got %d",
642+
MINYEAR, MAXYEAR, year);
641643
return -1;
642644
}
643645
if (month < 1 || month > 12) {
644646
PyErr_SetString(PyExc_ValueError,
645-
"month must be in 1..12");
647+
"month must be in 1..12, but got %d",
648+
month);
646649
return -1;
647650
}
648-
if (day < 1 || day > days_in_month(year, month)) {
651+
int dim = days_in_month(year, month)
652+
if (day < 1 || day > dim) {
649653
PyErr_SetString(PyExc_ValueError,
650-
"day is out of range for month");
654+
"day must be in 1..%d, but got %d",
655+
dim, day);
651656
return -1;
652657
}
653658
return 0;
@@ -661,27 +666,27 @@ check_time_args(int h, int m, int s, int us, int fold)
661666
{
662667
if (h < 0 || h > 23) {
663668
PyErr_SetString(PyExc_ValueError,
664-
"hour must be in 0..23");
669+
"hour must be in 0..23, but got %i", h);
665670
return -1;
666671
}
667672
if (m < 0 || m > 59) {
668673
PyErr_SetString(PyExc_ValueError,
669-
"minute must be in 0..59");
674+
"minute must be in 0..59, but got %i", m);
670675
return -1;
671676
}
672677
if (s < 0 || s > 59) {
673678
PyErr_SetString(PyExc_ValueError,
674-
"second must be in 0..59");
679+
"second must be in 0..59, but got %i", s);
675680
return -1;
676681
}
677682
if (us < 0 || us > 999999) {
678683
PyErr_SetString(PyExc_ValueError,
679-
"microsecond must be in 0..999999");
684+
"microsecond must be in 0..999999, but got %i", us);
680685
return -1;
681686
}
682687
if (fold != 0 && fold != 1) {
683688
PyErr_SetString(PyExc_ValueError,
684-
"fold must be either 0 or 1");
689+
"fold must be either 0 or 1, but got %i", fold);
685690
return -1;
686691
}
687692
return 0;
@@ -1436,7 +1441,7 @@ new_timezone(PyObject *offset, PyObject *name)
14361441
PyErr_Format(PyExc_ValueError, "offset must be a timedelta"
14371442
" strictly between -timedelta(hours=24) and"
14381443
" timedelta(hours=24),"
1439-
" not %R.", offset);
1444+
" not %R", offset);
14401445
return NULL;
14411446
}
14421447

@@ -1508,7 +1513,8 @@ call_tzinfo_method(PyObject *tzinfo, const char *name, PyObject *tzinfoarg)
15081513
Py_DECREF(offset);
15091514
PyErr_Format(PyExc_ValueError, "offset must be a timedelta"
15101515
" strictly between -timedelta(hours=24) and"
1511-
" timedelta(hours=24).");
1516+
" timedelta(hours=24),"
1517+
" not %R", offset);
15121518
return NULL;
15131519
}
15141520
}
@@ -3387,7 +3393,9 @@ date_fromisocalendar(PyObject *cls, PyObject *args, PyObject *kw)
33873393
int rv = iso_to_ymd(year, week, day, &year, &month, &day);
33883394

33893395
if (rv == -4) {
3390-
PyErr_Format(PyExc_ValueError, "Year is out of range: %d", year);
3396+
PyErr_Format(PyExc_ValueError,
3397+
"year must be in %d..%d, but got %d",
3398+
MINYEAR, MAXYEAR, year);
33913399
return NULL;
33923400
}
33933401

@@ -3397,7 +3405,7 @@ date_fromisocalendar(PyObject *cls, PyObject *args, PyObject *kw)
33973405
}
33983406

33993407
if (rv == -3) {
3400-
PyErr_Format(PyExc_ValueError, "Invalid day: %d (range is [1, 7])",
3408+
PyErr_Format(PyExc_ValueError, "Invalid weekday: %d (range is [1, 7])",
34013409
day);
34023410
return NULL;
34033411
}
@@ -5357,7 +5365,9 @@ utc_to_seconds(int year, int month, int day,
53575365

53585366
/* ymd_to_ord() doesn't support year <= 0 */
53595367
if (year < MINYEAR || year > MAXYEAR) {
5360-
PyErr_Format(PyExc_ValueError, "year %i is out of range", year);
5368+
PyErr_Format(PyExc_ValueError,
5369+
"year must be in %d..%d, but got %d",
5370+
MINYEAR, MAXYEAR, year);
53615371
return -1;
53625372
}
53635373

0 commit comments

Comments
 (0)