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
Ensure that B006 autofixes are inserted after imports #7629
Merged
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
fb0d790
Handle one import
hoxbro ef25c5c
Handle multiple imports
hoxbro 7e1f73b
Use initialization_pos
hoxbro 5dbc050
Find first pos which is not docstring or import
hoxbro 4fbf635
Rename back to statement
hoxbro 923f86a
Add more tests
hoxbro b245d12
Update snapshot
hoxbro 1356849
Fix lint
hoxbro bfb57a8
Merge branch 'main' into fix_b006_import
hoxbro a52e6d6
Update snap
hoxbro 2786dc6
Merge branch 'main' into fix_b006_import
hoxbro 0fdb1a4
Update to work with existing tests
hoxbro 996b650
Add B006 import tests
hoxbro 1d64537
Merge branch 'main' into fix_b006_import
hoxbro 432658e
Remove old snap
hoxbro efb68c3
Rename test file
hoxbro c83dbcb
Add small info at top of test file
hoxbro cd17b8c
Fix duplicate tests
hoxbro d8df26e
Handle when pos is equal to line length
hoxbro 6c86f28
Update info for new tests
hoxbro bec74c3
Merge branch 'main' into fix_b006_import
charliermarsh 7d27160
Collapse cases
charliermarsh File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B006_6.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Docstring followed by whitespace with no newline | ||
# Regression test for https://github.com/astral-sh/ruff/issues/7155 | ||
|
||
def foobar(foor, bar={}): | ||
import os |
5 changes: 5 additions & 0 deletions
5
crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B006_7.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Docstring with no newline | ||
|
||
|
||
def foobar(foor, bar={}): | ||
import os |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -151,7 +151,8 @@ B006_5.py:35:59: B006 [*] Do not use mutable data structures for argument defaul | |
35 |+def import_docstring_module_wrong(value: dict[str, str] = None): | ||
36 36 | """Docstring""" | ||
37 37 | import os | ||
38 |+ if value is None: | ||
39 |+ value = {} | ||
38 |+ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line was added when implementing the I can see something similar happens when doing it with docstrings on This will: def t(foo=[]):
"""Docstring"""
a = 2 will be fixed to def t(foo=None):
"""Docstring"""
if foo is None:
foo = []
a = 2 Whereas def t(foo=[]):
"""Docstring""" will be fixed to def t(foo=None):
"""Docstring"""
if foo is None:
foo = [] |
||
39 |+ if value is None: | ||
40 |+ value = {} | ||
|
||
|
25 changes: 25 additions & 0 deletions
25
...s/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B006_B006_6.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_bugbear/mod.rs | ||
--- | ||
B006_6.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 | import os | ||
| | ||
= 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={}): | ||
5 |- import os | ||
4 |+def foobar(foor, bar=None): | ||
5 |+ import os | ||
6 |+ if bar is None: | ||
7 |+ bar = {} | ||
|
||
|
23 changes: 23 additions & 0 deletions
23
...s/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B006_B006_7.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_bugbear/mod.rs | ||
--- | ||
B006_7.py:4:22: B006 [*] Do not use mutable data structures for argument defaults | ||
| | ||
4 | def foobar(foor, bar={}): | ||
| ^^ B006 | ||
5 | import os | ||
| | ||
= help: Replace with `None`; initialize within function | ||
|
||
ℹ Possible fix | ||
1 1 | # Docstring with no newline | ||
2 2 | | ||
3 3 | | ||
4 |-def foobar(foor, bar={}): | ||
5 |- import os | ||
4 |+def foobar(foor, bar=None): | ||
5 |+ import os | ||
6 |+ if bar is None: | ||
7 |+ bar = {} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you also need to handle the
locator.full_line_end(statement.end()) == locator.text_len()
case from above here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will take a look at it later today.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should now be handled.