Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DTZ007 with F-string format specifier #10601

Closed
MichaReiser opened this issue Mar 26, 2024 · 0 comments · Fixed by #10651
Closed

DTZ007 with F-string format specifier #10601

MichaReiser opened this issue Mar 26, 2024 · 0 comments · Fixed by #10651
Assignees
Labels
bug Something isn't working rule Implementing or modifying a lint rule

Comments

@MichaReiser
Copy link
Member

          Don't know if this is the same issue, but there is also a false-positive if the format string is an f-string.

For example:

from datetime import datetime
def parse_iso(iso_str,millis=True):
    return datetime.strptime(iso_str, f"%Y-%m-%dT%H:%M:%S{('.%f' if millis else '')}%z")

Even without a condition inside the f-string, it immediately triggers the lint error.

For example:

from datetime import datetime
dt = datetime.strptime("","%Y-%m-%d %H:%M:%S%z") # everything okay

dt = datetime.strptime("", f"%Y-%m-%d %H:%M:%S%z") # throws DTZ007

Originally posted by @jonas-w in #1306 (comment)

@MichaReiser MichaReiser added bug Something isn't working rule Implementing or modifying a lint rule labels Mar 26, 2024
@dhruvmanila dhruvmanila self-assigned this Mar 29, 2024
dhruvmanila added a commit that referenced this issue Mar 29, 2024
## 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants