Skip to content
Merged
Prev Previous commit
Next Next commit
skip visit_type_definition in a @no_type_check context
  • Loading branch information
ntBre committed Nov 26, 2024
commit 3c5ceab687196acbaf4b1c08bd3ede1ba5a186db
10 changes: 10 additions & 0 deletions crates/ruff_linter/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,13 @@ 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 @@ -1851,6 +1858,9 @@ impl<'a> Checker<'a> {

/// Visit an [`Expr`], and treat it as a type definition.
fn visit_type_definition(&mut self, expr: &'a Expr) {
if !(self.semantic.flags & SemanticModelFlags::NO_TYPE_CHECK).is_empty() {
return;
}
let snapshot = self.semantic.flags;
self.semantic.flags |= SemanticModelFlags::TYPE_DEFINITION;
self.visit_expr(expr);
Expand Down
2 changes: 2 additions & 0 deletions crates/ruff_python_semantic/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2222,6 +2222,8 @@ bitflags! {
/// [PEP 257]: https://peps.python.org/pep-0257/#what-is-a-docstring
const ATTRIBUTE_DOCSTRING = 1 << 25;

const NO_TYPE_CHECK = 1 << 26;

/// The context is in any type annotation.
const ANNOTATION = Self::TYPING_ONLY_ANNOTATION.bits() | Self::RUNTIME_EVALUATED_ANNOTATION.bits() | Self::RUNTIME_REQUIRED_ANNOTATION.bits();

Expand Down