-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reset model state when exiting deferred visitors (#6208)
## Summary Very subtle bug related to the AST traversal. Given: ```python from __future__ import annotations from logging import getLogger __all__ = ("getLogger",) def foo() -> None: pass ``` We end up visiting the `-> None` annotation, then reusing the state snapshot when we go to visit the `__all__` exports, so when we visit `"getLogger"`, we think we're inside of a deferred type annotation. This PR changes all the deferred visitors to snapshot and restore the state, which is a lot safer -- that way, the visitors avoid modifying the current visitor state. (Previously, they implicitly left the visitor state set to the state of the _last_ thing they visited.) Closes #6207.
- Loading branch information
1 parent
0fddb31
commit 6ee5cb3
Showing
4 changed files
with
38 additions
and
9 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
crates/ruff/resources/test/fixtures/flake8_type_checking/snapshot.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
"""Regression test: ensure that we don't treat the export entry as a typing-only reference.""" | ||
from __future__ import annotations | ||
|
||
from logging import getLogger | ||
|
||
__all__ = ("getLogger",) | ||
|
||
|
||
def foo() -> None: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
..._rules__flake8_type_checking__tests__typing-only-standard-library-import_snapshot.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_type_checking/mod.rs | ||
--- | ||
|