A date or time is built from a literal field outside its range

tx0/python:datetime-out-of-rangedefaultwarningbug

What it finds

datetime(2024, 13, 1), time(24, 0): the constructors of datetime check their fields and raise ValueError for a month outside 1 to 12, a day outside the month, an hour of 24 or more, and so on. Written as literals, such values fail every time.

Checked are integer literals given to datetime.datetime, datetime.date and datetime.time, by position or keyword. Days are checked against the month, with February given 29 days only in leap years when the year is a literal too.

From the rule's source, datetime_out_of_range.rs.

Example from the rule's tests
import datetimefrom datetime import date, time  def moments(year):    a = datetime.datetime(2024, 13, 1)tx0 reports: datetime refuses the month 13, outside 1 to 12, and raises ValueError    b = date(2023, 2, 29)tx0 reports: date refuses the day 29, outside 1 to 28    c = time(24, 0)tx0 reports: time refuses the hour 24, outside 0 to 23    d = date(2024, 2, 29)    e = date(year, 2, 29)    f = datetime.datetime(2024, 1, 31, 23, 59, 59)    return a, b, c, d, e, f
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/python:datetime-out-of-range" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

# tx0: ignore[tx0/python:datetime-out-of-range] reason="..."

tx0 explain tx0/python:datetime-out-of-range shows the same in the terminal. Found a false positive? Report it.