Skip to content

Commit

Permalink
Remove special pre-visit for module docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Dec 23, 2023
1 parent 74dba3e commit 0c81f82
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
22 changes: 11 additions & 11 deletions crates/ruff_linter/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,15 @@ where

// Track whether we've seen docstrings, non-imports, etc.
match stmt {
Stmt::Expr(ast::StmtExpr { value, .. })
if !self
.semantic
.flags
.intersects(SemanticModelFlags::MODULE_DOCSTRING)
&& value.is_string_literal_expr() =>
{
self.semantic.flags |= SemanticModelFlags::MODULE_DOCSTRING;
}
Stmt::ImportFrom(ast::StmtImportFrom { module, names, .. }) => {
// Allow __future__ imports until we see a non-__future__ import.
if let Some("__future__") = module.as_deref() {
Expand Down Expand Up @@ -1435,11 +1444,8 @@ where

impl<'a> Checker<'a> {
/// Visit a [`Module`]. Returns `true` if the module contains a module-level docstring.
fn visit_module(&mut self, python_ast: &'a Suite) -> bool {
fn visit_module(&mut self, python_ast: &'a Suite) {
analyze::module(python_ast, self);

let docstring = docstrings::extraction::docstring_from(python_ast);
docstring.is_some()
}

/// Visit a list of [`Comprehension`] nodes, assumed to be the comprehensions that compose a
Expand Down Expand Up @@ -2006,14 +2012,8 @@ pub(crate) fn check_ast(
);
checker.bind_builtins();

// Check for module docstring.
let python_ast = if checker.visit_module(python_ast) {
&python_ast[1..]
} else {
python_ast
};

// Iterate over the AST.
checker.visit_module(python_ast);
checker.visit_body(python_ast);

// Visit any deferred syntax nodes. Take care to visit in order, such that we avoid adding
Expand Down
15 changes: 12 additions & 3 deletions crates/ruff_python_semantic/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1715,6 +1715,16 @@ bitflags! {
/// ```
const FUTURE_ANNOTATIONS = 1 << 15;

/// The model has traversed past the module docstring.
///
/// For example, the model could be visiting `x` in:
/// ```python
/// """Module docstring."""
///
/// x: int = 1
/// ```
const MODULE_DOCSTRING = 1 << 16;

/// The model is in a type parameter definition.
///
/// For example, the model could be visiting `Record` in:
Expand All @@ -1723,11 +1733,10 @@ bitflags! {
///
/// Record = TypeVar("Record")
///
const TYPE_PARAM_DEFINITION = 1 << 16;
const TYPE_PARAM_DEFINITION = 1 << 17;

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

const ANNOTATION = Self::TYPING_ONLY_ANNOTATION.bits() | Self::RUNTIME_EVALUATED_ANNOTATION.bits() | Self::RUNTIME_REQUIRED_ANNOTATION.bits();

/// The context is in any string type definition.
const STRING_TYPE_DEFINITION = Self::SIMPLE_STRING_TYPE_DEFINITION.bits()
Expand Down

0 comments on commit 0c81f82

Please sign in to comment.