Treat non-future function annotations as required-at-runtime (#4028)

This commit is contained in:
Charlie Marsh 2023-04-19 14:43:55 -04:00 committed by GitHub
parent 0d84517fbc
commit 827cbe7f97
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 32 deletions

View file

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

View file

@ -598,14 +598,9 @@ where
self.visit_expr(expr);
}
// 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
let runtime_annotation = !self.ctx.annotations_future_enabled
&& matches!(
self.ctx.scope().kind,
ScopeKind::Class(..) | ScopeKind::Module
);
// Function annotations are always evaluated at runtime, unless future annotations
// are enabled.
let runtime_annotation = !self.ctx.annotations_future_enabled;
for arg in &args.posonlyargs {
if let Some(expr) = &arg.node.annotation {

View file

@ -1,34 +1,34 @@
---
source: crates/ruff/src/rules/flake8_type_checking/mod.rs
---
strict.py:24:21: TCH002 Move third-party import `pkg.A` into a type-checking block
strict.py:27:21: TCH002 Move third-party import `pkg.A` into a type-checking block
|
24 | # In un-strict mode, this shouldn't rase an error, since `pkg` is used at runtime.
25 | import pkg
26 | from pkg import A
27 | # In un-strict mode, this shouldn't rase an error, since `pkg` is used at runtime.
28 | import pkg
29 | from pkg import A
| ^ TCH002
27 |
28 | def test(value: A):
30 |
31 | def test(value: A):
|
strict.py:32:21: TCH002 Move third-party import `pkg.A` into a type-checking block
strict.py:35:21: TCH002 Move third-party import `pkg.A` into a type-checking block
|
32 | def f():
33 | # In un-strict mode, this shouldn't rase an error, since `pkg` is used at runtime.
34 | from pkg import A, B
35 | def f():
36 | # In un-strict mode, this shouldn't rase an error, since `pkg` is used at runtime.
37 | from pkg import A, B
| ^ TCH002
35 |
36 | def test(value: A):
38 |
39 | def test(value: A):
|
strict.py:51:25: TCH002 Move third-party import `pkg.bar.A` into a type-checking block
strict.py:54:25: TCH002 Move third-party import `pkg.bar.A` into a type-checking block
|
51 | # In un-strict mode, this _should_ rase an error, since `pkg` is used at runtime.
52 | import pkg
53 | from pkg.bar import A
54 | # In un-strict mode, this _should_ rase an error, since `pkg` is used at runtime.
55 | import pkg
56 | from pkg.bar import A
| ^ TCH002
54 |
55 | def test(value: A):
57 |
58 | def test(value: A):
|

View file

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