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(): def f():
# Even in strict mode, this shouldn't rase an error, since `pkg` is used at runtime, # Even in strict mode, this shouldn't rase an error, since `pkg` is used at runtime,
# and implicitly imports `pkg.bar`. # and implicitly imports `pkg.bar`.

View file

@ -598,14 +598,9 @@ where
self.visit_expr(expr); self.visit_expr(expr);
} }
// If we're in a class or module scope, then the annotation needs to be // Function annotations are always evaluated at runtime, unless future annotations
// available at runtime. // are enabled.
// See: https://docs.python.org/3/reference/simple_stmts.html#annotated-assignment-statements let runtime_annotation = !self.ctx.annotations_future_enabled;
let runtime_annotation = !self.ctx.annotations_future_enabled
&& matches!(
self.ctx.scope().kind,
ScopeKind::Class(..) | ScopeKind::Module
);
for arg in &args.posonlyargs { for arg in &args.posonlyargs {
if let Some(expr) = &arg.node.annotation { if let Some(expr) = &arg.node.annotation {

View file

@ -1,34 +1,34 @@
--- ---
source: crates/ruff/src/rules/flake8_type_checking/mod.rs 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. 27 | # In un-strict mode, this shouldn't rase an error, since `pkg` is used at runtime.
25 | import pkg 28 | import pkg
26 | from pkg import A 29 | from pkg import A
| ^ TCH002 | ^ TCH002
27 | 30 |
28 | def test(value: A): 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(): 35 | def f():
33 | # In un-strict mode, this shouldn't rase an error, since `pkg` is used at runtime. 36 | # In un-strict mode, this shouldn't rase an error, since `pkg` is used at runtime.
34 | from pkg import A, B 37 | from pkg import A, B
| ^ TCH002 | ^ TCH002
35 | 38 |
36 | def test(value: A): 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. 54 | # In un-strict mode, this _should_ rase an error, since `pkg` is used at runtime.
52 | import pkg 55 | import pkg
53 | from pkg.bar import A 56 | from pkg.bar import A
| ^ TCH002 | ^ TCH002
54 | 57 |
55 | def test(value: A): 58 | def test(value: A):
| |

View file

@ -1,14 +1,14 @@
--- ---
source: crates/ruff/src/rules/flake8_type_checking/mod.rs 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. 54 | # In un-strict mode, this _should_ rase an error, since `pkg` is used at runtime.
52 | import pkg 55 | import pkg
53 | from pkg.bar import A 56 | from pkg.bar import A
| ^ TCH002 | ^ TCH002
54 | 57 |
55 | def test(value: A): 58 | def test(value: A):
| |