datetime.utcnow or utcfromtimestamp makes a UTC time without a time zone

tx0/python:naive-utc-datetimerecommendedwarningbug

What it finds

datetime.utcnow(), datetime.utcfromtimestamp(ts): both return a naive datetime that holds UTC time but does not say so, and most of the standard library takes naive values as local time, so converting or comparing them goes wrong by the UTC offset. Python 3.12 deprecated both. datetime.now(timezone.utc) and datetime.fromtimestamp(ts, timezone.utc) return aware values.

From the rule's source, naive_utc_datetime.rs.

Example from the rule's tests
import datetimefrom datetime import datetime as dt, timezone  def stamps(ts):    a = datetime.datetime.utcnow()tx0 reports: datetime.utcnow returns UTC time in a datetime that says it has no time zone    b = dt.utcfromtimestamp(ts)tx0 reports: datetime.utcfromtimestamp returns UTC time    c = dt.now(timezone.utc)    return a, b, c
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 recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/python:naive-utc-datetime" = "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-utc-datetime] reason="..."

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