Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
ignore @no_type_check for classes
  • Loading branch information
ntBre committed Nov 26, 2024
commit 1d5ee646f6ad0aaf352c0518f870f7504e12b5df
3 changes: 3 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pyflakes/F722_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

Don't report an error when the function being annotated has the
`@no_type_check` decorator.

However, we still want to ignore this annotation on classes. See
https://github.com/python/typing/pull/1615/files and the discussion on #14615.
"""

from typing import no_type_check
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

Don't report an error when the function being annotated has the
`@no_type_check` decorator.

However, we still want to ignore this annotation on classes. See
https://github.com/python/typing/pull/1615/files and the discussion on #14615.
"""

import typing
Expand Down
16 changes: 6 additions & 10 deletions crates/ruff_linter/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,12 @@ impl<'a> Visitor<'a> for Checker<'a> {
// Visit the decorators and arguments, but avoid the body, which will be
// deferred.
for decorator in decorator_list {
if self
.semantic
.match_typing_expr(&decorator.expression, "no_type_check")
{
self.semantic.flags |= SemanticModelFlags::NO_TYPE_CHECK;
}
self.visit_decorator(decorator);
}

Expand Down Expand Up @@ -1060,16 +1066,6 @@ impl<'a> Visitor<'a> for Checker<'a> {
self.semantic.flags = flags_snapshot;
}

fn visit_decorator(&mut self, decorator: &'a ruff_python_ast::Decorator) {
if self
.semantic
.match_typing_expr(&decorator.expression, "no_type_check")
{
self.semantic.flags |= SemanticModelFlags::NO_TYPE_CHECK;
}
visitor::walk_decorator(self, decorator);
}

fn visit_expr(&mut self, expr: &'a Expr) {
// Step 0: Pre-processing
if self.source_type.is_stub()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,28 @@
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
snapshot_kind: text
---
F722_1.py:20:16: F722 Syntax error in forward annotation: `this isn't python`
|
18 | @no_type_check
19 | class C:
20 | def f(arg: "this isn't python") -> "this isn't python either":
| ^^^^^^^^^^^^^^^^^^^ F722
21 | x: "this also isn't python" = 1
|

F722_1.py:20:40: F722 Syntax error in forward annotation: `this isn't python either`
|
18 | @no_type_check
19 | class C:
20 | def f(arg: "this isn't python") -> "this isn't python either":
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ F722
21 | x: "this also isn't python" = 1
|

F722_1.py:21:12: F722 Syntax error in forward annotation: `this also isn't python`
|
19 | class C:
20 | def f(arg: "this isn't python") -> "this isn't python either":
21 | x: "this also isn't python" = 1
| ^^^^^^^^^^^^^^^^^^^^^^^^ F722
|
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,28 @@
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
snapshot_kind: text
---
F821_30.py:20:23: F821 Undefined name `B`
|
18 | @typing.no_type_check
19 | class C:
20 | def f(self, arg: "B") -> "S":
| ^ F821
21 | x: "B" = 1
|

F821_30.py:20:31: F821 Undefined name `S`
|
18 | @typing.no_type_check
19 | class C:
20 | def f(self, arg: "B") -> "S":
| ^ F821
21 | x: "B" = 1
|

F821_30.py:21:13: F821 Undefined name `B`
|
19 | class C:
20 | def f(self, arg: "B") -> "S":
21 | x: "B" = 1
| ^ F821
|