Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
764eb1b
Update error messages to be the same in datetime
donbarbos Nov 27, 2024
2bf419f
Add NEWS.d/next
donbarbos Nov 27, 2024
4363e9e
fixed syntax errors
donbarbos Nov 27, 2024
9da0dfc
Move Py_DECREF after PyErr_Format
donbarbos Nov 29, 2024
179423d
Add more info in message error in _pydatetime impl
donbarbos Nov 29, 2024
f691251
Update Modules/_datetimemodule.c
donbarbos Nov 29, 2024
d8973cf
Update Modules/_datetimemodule.c
donbarbos Nov 29, 2024
5eea62f
Update Modules/_datetimemodule.c
donbarbos Nov 29, 2024
79543cc
Update Modules/_datetimemodule.c for optimisation
donbarbos Nov 29, 2024
d174497
Revert last update Modules/_datetimemodule.c
donbarbos Nov 29, 2024
498c4ba
Update Modules/_datetimemodule.c
donbarbos Nov 29, 2024
216d0fe
Update Misc/NEWS.d message
donbarbos Nov 29, 2024
c409fec
Update Misc/NEWS.d message
donbarbos Nov 29, 2024
3f454f6
Update Misc/NEWS.d message
donbarbos Nov 29, 2024
a2b8f7a
Update Misc/NEWS.d message
donbarbos Nov 29, 2024
209c338
Update Lib/_pydatetime.py
donbarbos Nov 30, 2024
0777aa5
Update Misc/NEWS.d/next/Library/2024-11-27-23-29-05.gh-issue-109798.O…
donbarbos Nov 30, 2024
4d31d33
Update Lib/_pydatetime.py
donbarbos Nov 30, 2024
f05ebba
Update Lib/_pydatetime.py
donbarbos Nov 30, 2024
f840105
Update Lib/_pydatetime.py
donbarbos Nov 30, 2024
61c95a5
Update Lib/_pydatetime.py
donbarbos Nov 30, 2024
2ab77b3
Update Lib/_pydatetime.py
donbarbos Nov 30, 2024
b1e272a
Update Lib/_pydatetime.py
donbarbos Nov 30, 2024
7a35bd4
Update Lib/_pydatetime.py
donbarbos Nov 30, 2024
cfd18cb
Add tests
donbarbos Nov 30, 2024
2827514
Update _pydatetime.py
donbarbos Dec 1, 2024
cd3bdc1
Update _pydatetime.py
donbarbos Dec 1, 2024
9915dfe
Change but got to not
donbarbos Dec 1, 2024
1da5a3a
Correct line break
donbarbos Dec 1, 2024
610f067
Update 2024-11-27-23-29-05.gh-issue-109798.OPj1CT.rst
pganssle Feb 12, 2025
410e0ce
Merge branch 'main' into issue-109798
pganssle Feb 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Change but got to not
  • Loading branch information
donbarbos committed Dec 1, 2024
commit 9915dfeaa47b5315d5312f919cfdcc3d3040e48b
28 changes: 14 additions & 14 deletions Lib/_pydatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ def _days_in_month(year, month):

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

def _ymd2ord(year, month, day):
"year, month, day -> ordinal, considering 01-Jan-0001 as day 1."
assert 1 <= month <= 12, f"month must be in 1..12, but got {month}"
assert 1 <= month <= 12, f"month must be in 1..12, not {month}"
dim = _days_in_month(year, month)
assert 1 <= day <= dim, f"day must be in 1..{dim}, but got {day}"
assert 1 <= day <= dim, f"day must be in 1..{dim}, not {day}"
return (_days_before_year(year) +
_days_before_month(year, month) +
day)
Expand Down Expand Up @@ -512,7 +512,7 @@ def _parse_isoformat_time(tstr):
def _isoweek_to_gregorian(year, week, day):
# Year is bounded this way because 9999-12-31 is (9999, 52, 5)
if not MINYEAR <= year <= MAXYEAR:
raise ValueError(f"year must be in {MINYEAR}..{MAXYEAR}, but got {year}")
raise ValueError(f"year must be in {MINYEAR}..{MAXYEAR}, not {year}")

if not 0 < week < 53:
out_of_range = True
Expand Down Expand Up @@ -570,12 +570,12 @@ def _check_date_fields(year, month, day):
month = _index(month)
day = _index(day)
if not MINYEAR <= year <= MAXYEAR:
raise ValueError(f"year must be in {MINYEAR}..{MAXYEAR}, but got {year}")
raise ValueError(f"year must be in {MINYEAR}..{MAXYEAR}, not {year}")
if not 1 <= month <= 12:
raise ValueError(f"month must be in 1..12, but got {month}")
raise ValueError(f"month must be in 1..12, not {month}")
dim = _days_in_month(year, month)
if not 1 <= day <= dim:
raise ValueError(f"day must be in 1..{dim}, but got {day}")
raise ValueError(f"day must be in 1..{dim}, not {day}")
return year, month, day

def _check_time_fields(hour, minute, second, microsecond, fold):
Expand All @@ -584,22 +584,22 @@ def _check_time_fields(hour, minute, second, microsecond, fold):
second = _index(second)
microsecond = _index(microsecond)
if not 0 <= hour <= 23:
raise ValueError(f"hour must be in 0..23, but got {hour}")
raise ValueError(f"hour must be in 0..23, not {hour}")
if not 0 <= minute <= 59:
raise ValueError(f"minute must be in 0..59, but got {minute}")
raise ValueError(f"minute must be in 0..59, not {minute}")
if not 0 <= second <= 59:
raise ValueError(f"second must be in 0..59, but got {second}")
raise ValueError(f"second must be in 0..59, not {second}")
if not 0 <= microsecond <= 999999:
raise ValueError(f"microsecond must be in 0..999999, but got {microsecond}")
raise ValueError(f"microsecond must be in 0..999999, not {microsecond}")
if fold not in (0, 1):
raise ValueError(f"fold must be either 0 or 1, but got {fold}")
raise ValueError(f"fold must be either 0 or 1, not {fold}")
return hour, minute, second, microsecond, fold

def _check_tzinfo_arg(tz):
if tz is not None and not isinstance(tz, tzinfo):
raise TypeError(
"tzinfo argument must be None or of a tzinfo subclass, "
f"but got {type(tz).__name__!r}"
f"not {type(tz).__name__!r}"
)

def _divide_and_round(a, b):
Expand Down Expand Up @@ -2422,7 +2422,7 @@ def __new__(cls, offset, name=_Omitted):
if not cls._minoffset <= offset <= cls._maxoffset:
raise ValueError("offset must be a timedelta "
"strictly between -timedelta(hours=24) and "
f"timedelta(hours=24), but got {offset!r}")
f"timedelta(hours=24), not {offset!r}")
Comment thread
donbarbos marked this conversation as resolved.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not going to be a great user experience here because of how bad the timedelta formatter is:

>>> print(f"{timedelta(hours=-25)!r}")
datetime.timedelta(days=-2, seconds=82800)

Maybe it will be clearer if we do it this way:

if offset < timedelta(0):
    offset_str = f"-{-offset)}"
else:
     offset_str = str(offset)

That will print stuff like -1 day, 1:00:00 instead of timedelta(days=-2, seconds=82800). Though looking at it, I realize that the way it gets printed is misleading, since that's the output you get from print(timedelta(hours=-23)) and users have no way of knowing what is going on under the hood there.

Probably a more elaborate timedelta formatter would be better, since we basically always want this in HH:MM:SS.fff format, since that reflects what we care about best and also is unambigous, but we don't have a particularly easy way to do that. I guess we can revisit this when/if #85426 is implemented.

Copy link
Copy Markdown
Contributor Author

@donbarbos donbarbos Feb 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pganssle
Okay, then let's solve this issue in a separate PR linked to that issue (I can send the changes soon).
I think this one can be merged for now and I would be glad if you review another PR #127242 for datetime module :)

return cls._create(offset, name)

def __init_subclass__(cls):
Expand Down
8 changes: 4 additions & 4 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -1964,7 +1964,7 @@ def test_backdoor_resistance(self):

def test_valuerror_messages(self):
pattern = re.compile(
r"(year|month|day) must be in \d+\.\.\d+, but got \d+"
r"(year|month|day) must be in \d+\.\.\d+, not \d+"
)
test_cases = [
(2009, 1, 32), # Day out of range
Expand Down Expand Up @@ -3232,7 +3232,7 @@ class DateTimeSubclass(self.theclass):
def test_valuerror_messages(self):
pattern = re.compile(
r"(year|month|day|hour|minute|second) must "
r"be in \d+\.\.\d+, but got \d+"
r"be in \d+\.\.\d+, not \d+"
)
test_cases = [
(2009, 4, 1, 12, 30, 90), # Second out of range
Expand Down Expand Up @@ -3543,7 +3543,7 @@ def test_fromisoformat_fails_datetime(self):
def test_fromisoformat_fails_datetime_valueerror(self):
pattern = re.compile(
r"(year|month|day|hour|minute|second) must "
r"be in \d+\.\.\d+, but got \d+"
r"be in \d+\.\.\d+, not \d+"
)
bad_strs = [
"2009-04-01T12:30:90", # Second out of range
Expand Down Expand Up @@ -4538,7 +4538,7 @@ def utcoffset(self, t):
def test_valuerror_messages(self):
pattern = re.compile(
r"(hour|minute|second|microsecond) must "
r"be in \d+\.\.\d+, but got \d+"
r"be in \d+\.\.\d+, not \d+"
)
test_cases = [
(12, 30, 90, 9999991), # Microsecond out of range
Expand Down
22 changes: 11 additions & 11 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -638,19 +638,19 @@ check_date_args(int year, int month, int day)

if (year < MINYEAR || year > MAXYEAR) {
PyErr_Format(PyExc_ValueError,
"year must be in %d..%d, but got %d",
"year must be in %d..%d, not %d",
MINYEAR, MAXYEAR, year);
return -1;
}
if (month < 1 || month > 12) {
PyErr_Format(PyExc_ValueError,
"month must be in 1..12, but got %d", month);
"month must be in 1..12, not %d", month);
return -1;
}
int dim = days_in_month(year, month);
if (day < 1 || day > dim) {
Comment thread
erlend-aasland marked this conversation as resolved.
PyErr_Format(PyExc_ValueError,
"day must be in 1..%d, but got %d", dim, day);
"day must be in 1..%d, not %d", dim, day);
return -1;
}
return 0;
Expand All @@ -664,27 +664,27 @@ check_time_args(int h, int m, int s, int us, int fold)
{
if (h < 0 || h > 23) {
PyErr_Format(PyExc_ValueError,
"hour must be in 0..23, but got %i", h);
"hour must be in 0..23, not %i", h);
return -1;
}
if (m < 0 || m > 59) {
PyErr_Format(PyExc_ValueError,
"minute must be in 0..59, but got %i", m);
"minute must be in 0..59, not %i", m);
return -1;
}
if (s < 0 || s > 59) {
PyErr_Format(PyExc_ValueError,
"second must be in 0..59, but got %i", s);
"second must be in 0..59, not %i", s);
return -1;
}
if (us < 0 || us > 999999) {
PyErr_Format(PyExc_ValueError,
"microsecond must be in 0..999999, but got %i", us);
"microsecond must be in 0..999999, not %i", us);
return -1;
}
if (fold != 0 && fold != 1) {
PyErr_Format(PyExc_ValueError,
"fold must be either 0 or 1, but got %i", fold);
"fold must be either 0 or 1, not %i", fold);
return -1;
}
return 0;
Expand Down Expand Up @@ -2270,7 +2270,7 @@ get_float_as_integer_ratio(PyObject *floatobj)
if (!PyTuple_Check(ratio)) {
PyErr_Format(PyExc_TypeError,
"unexpected return type from as_integer_ratio(): "
"expected tuple, got '%.200s'",
"expected tuple, not '%.200s'",
Py_TYPE(ratio)->tp_name);
Py_DECREF(ratio);
return NULL;
Expand Down Expand Up @@ -3392,7 +3392,7 @@ date_fromisocalendar(PyObject *cls, PyObject *args, PyObject *kw)

if (rv == -4) {
PyErr_Format(PyExc_ValueError,
"year must be in %d..%d, but got %d",
"year must be in %d..%d, not %d",
MINYEAR, MAXYEAR, year);
return NULL;
}
Expand Down Expand Up @@ -5364,7 +5364,7 @@ utc_to_seconds(int year, int month, int day,
/* ymd_to_ord() doesn't support year <= 0 */
if (year < MINYEAR || year > MAXYEAR) {
PyErr_Format(PyExc_ValueError,
"year must be in %d..%d, but got %d",
"year must be in %d..%d, not %d",
MINYEAR, MAXYEAR, year);
return -1;
}
Expand Down