Skip to content

Commit 827cbe7

Browse files
Treat non-future function annotations as required-at-runtime (#4028)
1 parent 0d84517 commit 827cbe7

File tree

4 files changed

+30
-32
lines changed

4 files changed

+30
-32
lines changed

crates/ruff/resources/test/fixtures/flake8_type_checking/strict.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from __future__ import annotations
2+
3+
14
def f():
25
# Even in strict mode, this shouldn't rase an error, since `pkg` is used at runtime,
36
# and implicitly imports `pkg.bar`.

crates/ruff/src/checkers/ast/mod.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -598,14 +598,9 @@ where
598598
self.visit_expr(expr);
599599
}
600600

601-
// If we're in a class or module scope, then the annotation needs to be
602-
// available at runtime.
603-
// See: https://docs.python.org/3/reference/simple_stmts.html#annotated-assignment-statements
604-
let runtime_annotation = !self.ctx.annotations_future_enabled
605-
&& matches!(
606-
self.ctx.scope().kind,
607-
ScopeKind::Class(..) | ScopeKind::Module
608-
);
601+
// Function annotations are always evaluated at runtime, unless future annotations
602+
// are enabled.
603+
let runtime_annotation = !self.ctx.annotations_future_enabled;
609604

610605
for arg in &args.posonlyargs {
611606
if let Some(expr) = &arg.node.annotation {
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
---
22
source: crates/ruff/src/rules/flake8_type_checking/mod.rs
33
---
4-
strict.py:24:21: TCH002 Move third-party import `pkg.A` into a type-checking block
4+
strict.py:27:21: TCH002 Move third-party import `pkg.A` into a type-checking block
55
|
6-
24 | # In un-strict mode, this shouldn't rase an error, since `pkg` is used at runtime.
7-
25 | import pkg
8-
26 | from pkg import A
6+
27 | # In un-strict mode, this shouldn't rase an error, since `pkg` is used at runtime.
7+
28 | import pkg
8+
29 | from pkg import A
99
| ^ TCH002
10-
27 |
11-
28 | def test(value: A):
10+
30 |
11+
31 | def test(value: A):
1212
|
1313

14-
strict.py:32:21: TCH002 Move third-party import `pkg.A` into a type-checking block
14+
strict.py:35:21: TCH002 Move third-party import `pkg.A` into a type-checking block
1515
|
16-
32 | def f():
17-
33 | # In un-strict mode, this shouldn't rase an error, since `pkg` is used at runtime.
18-
34 | from pkg import A, B
16+
35 | def f():
17+
36 | # In un-strict mode, this shouldn't rase an error, since `pkg` is used at runtime.
18+
37 | from pkg import A, B
1919
| ^ TCH002
20-
35 |
21-
36 | def test(value: A):
20+
38 |
21+
39 | def test(value: A):
2222
|
2323

24-
strict.py:51:25: TCH002 Move third-party import `pkg.bar.A` into a type-checking block
24+
strict.py:54:25: TCH002 Move third-party import `pkg.bar.A` into a type-checking block
2525
|
26-
51 | # In un-strict mode, this _should_ rase an error, since `pkg` is used at runtime.
27-
52 | import pkg
28-
53 | from pkg.bar import A
26+
54 | # In un-strict mode, this _should_ rase an error, since `pkg` is used at runtime.
27+
55 | import pkg
28+
56 | from pkg.bar import A
2929
| ^ TCH002
30-
54 |
31-
55 | def test(value: A):
30+
57 |
31+
58 | def test(value: A):
3232
|
3333

3434

Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
---
22
source: crates/ruff/src/rules/flake8_type_checking/mod.rs
33
---
4-
strict.py:51:25: TCH002 Move third-party import `pkg.bar.A` into a type-checking block
4+
strict.py:54:25: TCH002 Move third-party import `pkg.bar.A` into a type-checking block
55
|
6-
51 | # In un-strict mode, this _should_ rase an error, since `pkg` is used at runtime.
7-
52 | import pkg
8-
53 | from pkg.bar import A
6+
54 | # In un-strict mode, this _should_ rase an error, since `pkg` is used at runtime.
7+
55 | import pkg
8+
56 | from pkg.bar import A
99
| ^ TCH002
10-
54 |
11-
55 | def test(value: A):
10+
57 |
11+
58 | def test(value: A):
1212
|
1313

1414

0 commit comments

Comments
 (0)