Skip to content

Commit

Permalink
Treat annotated assignments in class and module scopes as runtime (#2667
Browse files Browse the repository at this point in the history
)
  • Loading branch information
charliermarsh authored Feb 8, 2023
1 parent 286d8c1 commit cb4a221
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Tuple, List, Dict

x: Tuple


class C:
x: List


def f():
x: Dict
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Tuple, List, Dict

x: Tuple


class C:
x: List


def f():
x: Dict
16 changes: 15 additions & 1 deletion crates/ruff/src/checkers/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1945,7 +1945,21 @@ where
value,
..
} => {
self.visit_annotation(annotation);
// If we're in a class or module scope, then the annotation needs to be available
// at runtime.
// See: https://docs.python.org/3/reference/simple_stmts.html#annotated-assignment-statements
if !self.annotations_future_enabled
&& matches!(
self.current_scope().kind,
ScopeKind::Class(..) | ScopeKind::Module
)
{
self.in_type_definition = true;
self.visit_expr(annotation);
self.in_type_definition = false;
} else {
self.visit_annotation(annotation);
}
if let Some(expr) = value {
if self.match_typing_expr(annotation, "TypeAlias") {
self.in_type_definition = true;
Expand Down
2 changes: 2 additions & 0 deletions crates/ruff/src/rules/flake8_type_checking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ mod tests {
#[test_case(Rule::RuntimeImportInTypeCheckingBlock, Path::new("TCH004_6.py"); "TCH004_6")]
#[test_case(Rule::RuntimeImportInTypeCheckingBlock, Path::new("TCH004_7.py"); "TCH004_7")]
#[test_case(Rule::RuntimeImportInTypeCheckingBlock, Path::new("TCH004_8.py"); "TCH004_8")]
#[test_case(Rule::RuntimeImportInTypeCheckingBlock, Path::new("TCH004_9.py"); "TCH004_9")]
#[test_case(Rule::RuntimeImportInTypeCheckingBlock, Path::new("TCH004_10.py"); "TCH004_10")]
#[test_case(Rule::EmptyTypeCheckingBlock, Path::new("TCH005.py"); "TCH005")]
#[test_case(Rule::TypingOnlyThirdPartyImport, Path::new("strict.py"); "strict")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: crates/ruff/src/rules/flake8_type_checking/mod.rs
expression: diagnostics
---
[]

Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
---
source: src/rules/flake8_type_checking/mod.rs
source: crates/ruff/src/rules/flake8_type_checking/mod.rs
expression: diagnostics
---
[]
- kind:
RuntimeImportInTypeCheckingBlock:
full_name: typing.Any
location:
row: 4
column: 23
end_location:
row: 4
column: 26
fix: ~
parent: ~

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
source: crates/ruff/src/rules/flake8_type_checking/mod.rs
expression: diagnostics
---
- kind:
RuntimeImportInTypeCheckingBlock:
full_name: typing.Tuple
location:
row: 4
column: 23
end_location:
row: 4
column: 28
fix: ~
parent: ~
- kind:
RuntimeImportInTypeCheckingBlock:
full_name: typing.List
location:
row: 4
column: 30
end_location:
row: 4
column: 34
fix: ~
parent: ~

0 comments on commit cb4a221

Please sign in to comment.