-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid attributing runtime references to module-level imports (#4942)
- Loading branch information
1 parent
20240fc
commit ae75b30
Showing
4 changed files
with
98 additions
and
4 deletions.
There are no files selected for viewing
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,32 @@ | ||
"""Test that runtime typing references are properly attributed to scoped imports.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, cast | ||
|
||
if TYPE_CHECKING: | ||
from threading import Thread | ||
|
||
|
||
def fn(thread: Thread): | ||
from threading import Thread | ||
|
||
# The `Thread` on the left-hand side should resolve to the `Thread` imported at the | ||
# top level. | ||
x: Thread | ||
|
||
|
||
def fn(thread: Thread): | ||
from threading import Thread | ||
|
||
# The `Thread` on the left-hand side should resolve to the `Thread` imported at the | ||
# top level. | ||
cast("Thread", thread) | ||
|
||
|
||
def fn(thread: Thread): | ||
from threading import Thread | ||
|
||
# The `Thread` on the right-hand side should resolve to the`Thread` imported within | ||
# `fn`. | ||
cast(Thread, thread) |
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
42 changes: 42 additions & 0 deletions
42
crates/ruff/src/rules/pyflakes/snapshots/ruff__rules__pyflakes__tests__F401_F401_17.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,42 @@ | ||
--- | ||
source: crates/ruff/src/rules/pyflakes/mod.rs | ||
--- | ||
F401_17.py:12:27: F401 [*] `threading.Thread` imported but unused | ||
| | ||
12 | def fn(thread: Thread): | ||
13 | from threading import Thread | ||
| ^^^^^^ F401 | ||
14 | | ||
15 | # The `Thread` on the left-hand side should resolve to the `Thread` imported at the | ||
| | ||
= help: Remove unused import: `threading.Thread` | ||
|
||
ℹ Fix | ||
9 9 | | ||
10 10 | | ||
11 11 | def fn(thread: Thread): | ||
12 |- from threading import Thread | ||
13 12 | | ||
14 13 | # The `Thread` on the left-hand side should resolve to the `Thread` imported at the | ||
15 14 | # top level. | ||
|
||
F401_17.py:20:27: F401 [*] `threading.Thread` imported but unused | ||
| | ||
20 | def fn(thread: Thread): | ||
21 | from threading import Thread | ||
| ^^^^^^ F401 | ||
22 | | ||
23 | # The `Thread` on the left-hand side should resolve to the `Thread` imported at the | ||
| | ||
= help: Remove unused import: `threading.Thread` | ||
|
||
ℹ Fix | ||
17 17 | | ||
18 18 | | ||
19 19 | def fn(thread: Thread): | ||
20 |- from threading import Thread | ||
21 20 | | ||
22 21 | # The `Thread` on the left-hand side should resolve to the `Thread` imported at the | ||
23 22 | # top level. | ||
|
||
|
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