Skip to content
Merged
Prev Previous commit
Next Next commit
move no_type_check check into visit_decorator
  • Loading branch information
ntBre committed Nov 26, 2024
commit 06c1655c8f7ff9eab3895f65d92b6f39389918de
18 changes: 11 additions & 7 deletions crates/ruff_linter/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,13 +723,6 @@ 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 decorator
.expression
.as_name_expr()
.is_some_and(|name| name.id.as_str() == "no_type_check")
{
self.semantic.flags |= SemanticModelFlags::NO_TYPE_CHECK;
}
self.visit_decorator(decorator);
}

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

fn visit_decorator(&mut self, decorator: &'a ruff_python_ast::Decorator) {
if decorator
.expression
.as_name_expr()
.is_some_and(|name| name.id.as_str() == "no_type_check")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should use match_typing_expr so that it works for @typing.no_type_check too.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, will this apply to type annotations in the body of the function? And should it, per the spec? (I don't know off-hand.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah thanks, I missed match_typing_expr.

Yes, this should apply to annotations in the body too, based on my reading of the no_type_check docs:

This works as a class or function decorator. With a class, it applies recursively to all methods and classes defined in that class (but not to methods defined in its superclasses or subclasses). Type checkers will ignore all annotations in a function or class with this decorator.

Similarly in the typing docs linked in what Micha linked:

If a type checker supports the no_type_check decorator for functions, it should suppress all type errors for the def statement and its body including any nested functions or classes. It should also ignore all parameter and return type annotations and treat the function as if it were unannotated.

I added annotated variables to the test fixtures in my last commit to check this.

{
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