datetime.now() or datetime.today() makes a local time without a time zone

tx0/python:naive-datetime-nowstrictwarningbug

What it finds

datetime.now() without a time zone returns the local wall-clock time as a naive value, which says nothing about the zone it belongs to. Stored or compared with times from another machine or season it is off by the offset; datetime.now(timezone.utc) or a ZoneInfo makes an aware value. datetime.today() takes no time zone and always returns a naive value. Reported are calls of datetime.datetime.now without arguments and of datetime.datetime.today, as the file's imports name them.

From the rule's source, naive_datetime_now.rs.

Example from the rule's tests
import datetime as dtfrom datetime import datetime, timezone a = datetime.now()tx0 reports: datetime.now() returns a local time without a time zoneb = dt.datetime.today()tx0 reports: datetime.today() returns a local timec = datetime.now(timezone.utc)d = dt.date.today()e = now()
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 from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/python:naive-datetime-now" = "warning"

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:naive-datetime-now] reason="..."

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