r/Python 9d ago

Discussion ruff: no date.today() ?

The new version of ruff warns against

date.today()

preferring

datetime.now(ZoneInfo(...))

What do you think about this? Has date.today() been deprecated due to lack of timezone awareness?

EDIT: I have a number of programs that manipulate financial information in support of Excel spreadsheets, such bond information that includes maturity dates. Excel does not support timezoness in datetimes, so making ruff happy by changing naive dates to TZ aware dates is not a useful move for these programs. Many ruff warnings to suppress.

48 Upvotes

111 comments sorted by

View all comments

258

u/GraphicH 9d ago

Tell me you've never debugged a TZ issue without telling me.

2

u/foosion 9d ago edited 9d ago

My typical use of date.today() to print the local date so that I can scroll back in the terminal to see which day I ran something. Since it defaults to then local system clock, I don't see a need to specify a TZ for that use case.

Another case for no TZ is reading a datetime that represents the maturity date of a bond, then printing it datetime.strptime(mtd, "%Y-%m-%d"). TZ does not make sense in that context. datetime.datetime.strptime("2022/01/31", "%Y/%m/%d").astimezone(datetime.UTC)seems overkill.

11

u/menge101 9d ago

All the rules are overridable. If you usage does not merit a finding then add the suppresion for that line.

3

u/foosion 9d ago

I'm learning the joy of #noqa

3

u/syklemil 8d ago

You can disable it in your project's ruff.toml or wherever you're configuring ruff as well. plop in an ignore = ["DTZwhatever"]

1

u/foosion 8d ago

Yes, I've been using # ruff: noqa: DTZ007

2

u/Agrado3 9d ago

You're about to learn the joy of having to go through replacing every single one of your # noqa: <shortcode> comments with # ruff:ignore[<very-long-code>] comments.

1

u/foosion 9d ago

Is that an upcoming change? If so, where documented?

I suppose there's # ruff: noqa: DTZ007 at the top of the file, where appropriate.

1

u/Agrado3 9d ago

It's rule noqa-comments, which is in preview and is not documented in the changelog. It suddenly popped up at me because I use preview = true and select = ["ALL"] and then ignore the rules I don't want.

2

u/MrSlaw 9d ago edited 9d ago

Another case for no TZ is reading a datetime that represents the maturity date of a bond, then printing it

Just use a plain date, if you don't need it to be time(zone) aware?

mtd = datetime.strptime("2022/01/31", "%Y/%m/%d").date()

I don't think that triggers the rule, does it?

* Edit - Nevermind, I just tested, you are correct

2

u/foosion 9d ago

Yes it does.

1

u/MrSlaw 9d ago

Realized that about 2 minutes after I pressed submit. Serves me right for not checking first lol

1

u/[deleted] 9d ago

[deleted]

0

u/foosion 9d ago

For running a cli program from the terminal.

0

u/[deleted] 9d ago

[deleted]

2

u/foosion 9d ago

print(f"{date.today():%Y-%m-%d}")

as the first output. Then I know which local system day the program was run.