Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix B006 when function docstring is followed by whitespace but no newline #7160

Merged
merged 2 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_bugbear/B006_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Docstring followed by a newline

def foobar(foor, bar={}):
"""
"""
6 changes: 6 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_bugbear/B006_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Docstring followed by whitespace with no newline
# Regression test for https://github.com/astral-sh/ruff/issues/7155

def foobar(foor, bar={}):
"""
"""
6 changes: 6 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_bugbear/B006_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Docstring with no newline


def foobar(foor, bar={}):
"""
"""
3 changes: 3 additions & 0 deletions crates/ruff/src/rules/flake8_bugbear/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ mod tests {
#[test_case(Rule::JumpStatementInFinally, Path::new("B012.py"))]
#[test_case(Rule::LoopVariableOverridesIterator, Path::new("B020.py"))]
#[test_case(Rule::MutableArgumentDefault, Path::new("B006_B008.py"))]
#[test_case(Rule::MutableArgumentDefault, Path::new("B006_1.py"))]
#[test_case(Rule::MutableArgumentDefault, Path::new("B006_2.py"))]
#[test_case(Rule::MutableArgumentDefault, Path::new("B006_3.py"))]
#[test_case(Rule::NoExplicitStacklevel, Path::new("B028.py"))]
#[test_case(Rule::RaiseLiteral, Path::new("B016.py"))]
#[test_case(Rule::RaiseWithoutFromInsideExcept, Path::new("B904.py"))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ fn move_initialization(
return None;
}
Edit::insertion(content, locator.line_start(statement.start()))
} else if statement.end() == locator.text_len() {
} else if locator.full_line_end(statement.end()) == locator.text_len() {
Copy link
Member Author

@zanieb zanieb Sep 5, 2023

Choose a reason for hiding this comment

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

Do we care about the cost of calculating the full_line_end twice here? (we do it on L183 as well)

I presume not since this is a rare case.

// If the statement is at the end of the file, without a trailing newline, insert
// _after_ it with an extra newline.
Edit::insertion(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
source: crates/ruff/src/rules/flake8_bugbear/mod.rs
---
B006_1.py:3:22: B006 [*] Do not use mutable data structures for argument defaults
|
1 | # Docstring followed by a newline
2 |
3 | def foobar(foor, bar={}):
| ^^ B006
4 | """
5 | """
|
= help: Replace with `None`; initialize within function

ℹ Possible fix
1 1 | # Docstring followed by a newline
2 2 |
3 |-def foobar(foor, bar={}):
3 |+def foobar(foor, bar=None):
4 4 | """
5 5 | """
6 |+
7 |+ if bar is None:
8 |+ bar = {}


Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
source: crates/ruff/src/rules/flake8_bugbear/mod.rs
---
B006_2.py:4:22: B006 [*] Do not use mutable data structures for argument defaults
|
2 | # Regression test for https://github.com/astral-sh/ruff/issues/7155
3 |
4 | def foobar(foor, bar={}):
| ^^ B006
5 | """
6 | """
|
= help: Replace with `None`; initialize within function

ℹ Possible fix
1 1 | # Docstring followed by whitespace with no newline
2 2 | # Regression test for https://github.com/astral-sh/ruff/issues/7155
3 3 |
4 |-def foobar(foor, bar={}):
4 |+def foobar(foor, bar=None):
5 5 | """
6 |- """
6 |+ """
7 |+ if bar is None:
8 |+ bar = {}


Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: crates/ruff/src/rules/flake8_bugbear/mod.rs
---
B006_3.py:4:22: B006 [*] Do not use mutable data structures for argument defaults
|
4 | def foobar(foor, bar={}):
| ^^ B006
5 | """
6 | """
|
= help: Replace with `None`; initialize within function

ℹ Possible fix
1 1 | # Docstring with no newline
2 2 |
3 3 |
4 |-def foobar(foor, bar={}):
4 |+def foobar(foor, bar=None):
5 |+ """
5 6 | """
6 |- """
7 |+ if bar is None:
8 |+ bar = {}