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 0b83fe4
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 209 deletions.
5 changes: 5 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pyflakes/F404_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Docstring"""

"""Non-docstring"""

from __future__ import absolute_import
36 changes: 17 additions & 19 deletions crates/ruff_linter/resources/test/fixtures/pyupgrade/UP025.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
# These should change
x = u"Hello"
u"Hello"

u'world'
x = u"Hello" # UP025

print(u"Hello")
u'world' # UP025

print(u'world')
print(u"Hello") # UP025

import foo

foo(u"Hello", U"world", a=u"Hello", b=u"world")
print(u'world') # UP025

# These should stay quoted they way they are
import foo

x = u'hello'
x = u"""hello"""
x = u'''hello'''
x = u'Hello "World"'
foo(u"Hello", U"world", a=u"Hello", b=u"world") # UP025

# These should not change
u = "Hello"
# Retain quotes when fixing.
x = u'hello' # UP025
x = u"""hello""" # UP025
x = u'''hello''' # UP025
x = u'Hello "World"' # UP025

u = u
u = "Hello" # OK
u = u # OK

def hello():
return"Hello"
return"Hello" # OK

f"foo"u"bar"
f"foo" u"bar"
f"foo"u"bar" # OK
f"foo" u"bar" # OK
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
3 changes: 2 additions & 1 deletion crates/ruff_linter/src/rules/pyflakes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ mod tests {
#[test_case(Rule::UnusedImport, Path::new("F401_20.py"))]
#[test_case(Rule::ImportShadowedByLoopVar, Path::new("F402.py"))]
#[test_case(Rule::UndefinedLocalWithImportStar, Path::new("F403.py"))]
#[test_case(Rule::LateFutureImport, Path::new("F404.py"))]
#[test_case(Rule::LateFutureImport, Path::new("F404_0.py"))]
#[test_case(Rule::LateFutureImport, Path::new("F404_1.py"))]
#[test_case(Rule::UndefinedLocalWithImportStarUsage, Path::new("F405.py"))]
#[test_case(Rule::UndefinedLocalWithNestedImportStarUsage, Path::new("F406.py"))]
#[test_case(Rule::FutureFeatureNotDefined, Path::new("F407.py"))]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---
F404.py:6:1: F404 `from __future__` imports must occur at the beginning of the file
F404_0.py:6:1: F404 `from __future__` imports must occur at the beginning of the file
|
4 | from collections import namedtuple
5 |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---
F404_1.py:5:1: F404 `from __future__` imports must occur at the beginning of the file
|
3 | """Non-docstring"""
4 |
5 | from __future__ import absolute_import
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ F404
|


Loading

0 comments on commit 0b83fe4

Please sign in to comment.