Skip to content

[flake8-datetime] Ignore .replace() calls while looking for .astimezone - #16050

Merged
AlexWaygood merged 4 commits into
astral-sh:mainfrom
InSyncWithFoo:DTZ005
Feb 9, 2025
Merged

[flake8-datetime] Ignore .replace() calls while looking for .astimezone#16050
AlexWaygood merged 4 commits into
astral-sh:mainfrom
InSyncWithFoo:DTZ005

Conversation

@InSyncWithFoo

@InSyncWithFoo InSyncWithFoo commented Feb 9, 2025

Copy link
Copy Markdown
Contributor

Summary

Resolves #15998.

Previously, this would be considered an error by DTZ005, as parent_expr_is_astimezone() would only traverse up one level:

datetime.now().replace(...).astimezone()

The function will now check for all expressions in the current stack, ignoring all such intermediate .replace() calls while looking for .astimezone.

Test Plan

cargo nextest run and cargo insta test.

@github-actions

github-actions Bot commented Feb 9, 2025

Copy link
Copy Markdown
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@InSyncWithFoo

Copy link
Copy Markdown
Contributor Author

The second commit is just a rename. All substantial changes are isolated within the first.

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, this overall looks great. Nicely done!

Thank you for adding comments to the code; it made it easier to understand what was going on. It still took me some time to figure out what was going on here, though. I almost wonder if it's worth adding a comment that links to the Ruff playground showing what the AST we're dealing with here looks like, and explaining that we start at the innermost node in the AST and have to work our way upwards https://play.ruff.rs/89b67ffb-c32a-437e-aa8c-8dcfc8b7068a

Comment thread crates/ruff_linter/src/rules/flake8_datetimez/rules/helpers.rs
@AlexWaygood

Copy link
Copy Markdown
Member

OK, I added some documentation for you.

@AlexWaygood AlexWaygood added the bug Something isn't working label Feb 9, 2025
@AlexWaygood
AlexWaygood enabled auto-merge (squash) February 9, 2025 15:47
@AlexWaygood
AlexWaygood merged commit a46fbda into astral-sh:main Feb 9, 2025
@InSyncWithFoo
InSyncWithFoo deleted the DTZ005 branch February 9, 2025 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DTZ005: false positive when replace(...) before astimezone()

2 participants