mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 13:25:17 +00:00

## Summary This PR fixes the bug for `DTZ007` rule where it didn't consider to check for the presence of `%z` in f-strings. It also considers the string parts of an implicitly concatenated f-strings for which I want to find a better solution (#10308). fixes: #10601 ## Test Plan Add test cases and update the snapshots.
41 lines
1.1 KiB
Python
41 lines
1.1 KiB
Python
import datetime
|
|
|
|
# bad format
|
|
datetime.datetime.strptime("something", "%H:%M:%S%Z")
|
|
|
|
# no replace or astimezone
|
|
datetime.datetime.strptime("something", "something")
|
|
|
|
# wrong replace
|
|
datetime.datetime.strptime("something", "something").replace(hour=1)
|
|
|
|
# none replace
|
|
datetime.datetime.strptime("something", "something").replace(tzinfo=None)
|
|
|
|
# OK
|
|
datetime.datetime.strptime("something", "something").replace(
|
|
tzinfo=datetime.timezone.utc
|
|
)
|
|
|
|
# OK
|
|
datetime.datetime.strptime("something", "something").astimezone()
|
|
|
|
# OK
|
|
datetime.datetime.strptime("something", "%H:%M:%S%z")
|
|
|
|
# OK
|
|
datetime.datetime.strptime("something", something).astimezone()
|
|
|
|
# OK
|
|
datetime.datetime.strptime("something", something).replace(tzinfo=datetime.timezone.utc)
|
|
|
|
from datetime import datetime
|
|
|
|
# no replace orastimezone unqualified
|
|
datetime.strptime("something", "something")
|
|
|
|
# F-strings
|
|
datetime.strptime("something", f"%Y-%m-%dT%H:%M:%S{('.%f' if millis else '')}%z")
|
|
datetime.strptime("something", f"%Y-%m-%d %H:%M:%S%z")
|
|
# F-string is implicitly concatenated to another string
|
|
datetime.strptime("something", f"%Y-%m-%dT%H:%M:%S{('.%f' if millis else '')}" "%z")
|