-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Summary In issue #6785 it is reported that a docstring in the form of `''"assert" ' SAM macro definitions '''` is autocorrected to `"""assert" ' SAM macro definitions '''` (note the triple quotes one only one side), which breaks the python program due `undetermined string lateral`. * `Q002`: Not only would docstrings in the form of `''"assert" ' SAM macro definitions '''` (single quotes) be autofixed wrongly, but also e.g. `""'assert' ' SAM macro definitions '''` (double quotes). The bug is present for docstrings in all scopes (e.g. module docstrings, class docstrings, function docstrings) * `Q000`: The autofix error is not only present for `Q002` (docstrings), but also for inline strings (`Q000`). Therefore `s = ''"assert" ' SAM macro definitions '''` will also be wrongly autofixed. Note that situation in which the first string is non-empty can be fixed, e.g. `'123'"assert" ' SAM macro definitions '''` -> `"123""assert" ' SAM macro definitions '''` is valid. ## What * Change FixAvailability of `Q000` `Q002` to `Sometimes` * Changed both rules such that docstrings/inline strings that cannot be fixed are still reported as bad quotes via diagnostics, but no fix is provided ## Test Plan * For `Q000`: Add docstrings in different scopes that (partially) would have been autofixed wrongly * For `Q002`: Add inline strings that (partially) would have been autofixed wrongly Closes #6785
- Loading branch information
1 parent
dc021dd
commit 2edd617
Showing
22 changed files
with
589 additions
and
13 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
...inter/resources/test/fixtures/flake8_quotes/docstring_doubles_mixed_quotes_class_var_1.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,9 @@ | ||
class SingleLineDocstrings(): | ||
""'Start with empty string' ' and lint docstring safely' | ||
""" Not a docstring """ | ||
|
||
def foo(self, bar="""not a docstring"""): | ||
""'Start with empty string' ' and lint docstring safely' | ||
pass | ||
|
||
class Nested(foo()[:]): ""'Start with empty string' ' and lint docstring safely'; pass |
9 changes: 9 additions & 0 deletions
9
...inter/resources/test/fixtures/flake8_quotes/docstring_doubles_mixed_quotes_class_var_2.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,9 @@ | ||
class SingleLineDocstrings(): | ||
"Do not"' start with empty string' ' and lint docstring safely' | ||
""" Not a docstring """ | ||
|
||
def foo(self, bar="""not a docstring"""): | ||
"Do not"' start with empty string' ' and lint docstring safely' | ||
pass | ||
|
||
class Nested(foo()[:]): "Do not"' start with empty string' ' and lint docstring safely'; pass |
5 changes: 5 additions & 0 deletions
5
...ces/test/fixtures/flake8_quotes/docstring_doubles_mixed_quotes_module_singleline_var_1.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 @@ | ||
""'Start with empty string' ' and lint docstring safely' | ||
|
||
def foo(): | ||
pass | ||
""" this is not a docstring """ |
5 changes: 5 additions & 0 deletions
5
...ces/test/fixtures/flake8_quotes/docstring_doubles_mixed_quotes_module_singleline_var_2.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 @@ | ||
"Do not"' start with empty string' ' and lint docstring safely' | ||
|
||
def foo(): | ||
pass | ||
""" this is not a docstring """ |
9 changes: 9 additions & 0 deletions
9
...inter/resources/test/fixtures/flake8_quotes/docstring_singles_mixed_quotes_class_var_1.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,9 @@ | ||
class SingleLineDocstrings(): | ||
''"Start with empty string" ' and lint docstring safely' | ||
''' Not a docstring ''' | ||
|
||
def foo(self, bar='''not a docstring'''): | ||
''"Start with empty string" ' and lint docstring safely' | ||
pass | ||
|
||
class Nested(foo()[:]): ''"Start with empty string" ' and lint docstring safely'; pass |
9 changes: 9 additions & 0 deletions
9
...inter/resources/test/fixtures/flake8_quotes/docstring_singles_mixed_quotes_class_var_2.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,9 @@ | ||
class SingleLineDocstrings(): | ||
'Do not'" start with empty string" ' and lint docstring safely' | ||
''' Not a docstring ''' | ||
|
||
def foo(self, bar='''not a docstring'''): | ||
'Do not'" start with empty string" ' and lint docstring safely' | ||
pass | ||
|
||
class Nested(foo()[:]): 'Do not'" start with empty string" ' and lint docstring safely'; pass |
5 changes: 5 additions & 0 deletions
5
...ces/test/fixtures/flake8_quotes/docstring_singles_mixed_quotes_module_singleline_var_1.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 @@ | ||
''"Start with empty string" ' and lint docstring safely' | ||
|
||
def foo(): | ||
pass | ||
""" this is not a docstring """ |
5 changes: 5 additions & 0 deletions
5
...ces/test/fixtures/flake8_quotes/docstring_singles_mixed_quotes_module_singleline_var_2.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 @@ | ||
'Do not'" start with empty string" ' and lint docstring safely' | ||
|
||
def foo(): | ||
pass | ||
""" this is not a docstring """ |
2 changes: 2 additions & 0 deletions
2
crates/ruff_linter/resources/test/fixtures/flake8_quotes/doubles_would_be_triple_quotes.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,2 @@ | ||
s = ""'Start with empty string' ' and lint docstring safely' | ||
s = "Do not"' start with empty string' ' and lint docstring safely' |
2 changes: 2 additions & 0 deletions
2
crates/ruff_linter/resources/test/fixtures/flake8_quotes/singles_would_be_triple_quotes.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,2 @@ | ||
s = ''"Start with empty string" ' and lint docstring safely' | ||
s = 'Do not'" start with empty string" ' and lint docstring safely' |
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
56 changes: 56 additions & 0 deletions
56
..._tests__require_docstring_doubles_over_docstring_singles_mixed_quotes_class_var_1.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,56 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_quotes/mod.rs | ||
--- | ||
docstring_singles_mixed_quotes_class_var_1.py:2:5: Q002 Single quote docstring found but double quotes preferred | ||
| | ||
1 | class SingleLineDocstrings(): | ||
2 | ''"Start with empty string" ' and lint docstring safely' | ||
| ^^ Q002 | ||
3 | ''' Not a docstring ''' | ||
| | ||
= help: Replace single quotes docstring with double quotes | ||
|
||
docstring_singles_mixed_quotes_class_var_1.py:2:7: Q000 Double quotes found but single quotes preferred | ||
| | ||
1 | class SingleLineDocstrings(): | ||
2 | ''"Start with empty string" ' and lint docstring safely' | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ Q000 | ||
3 | ''' Not a docstring ''' | ||
| | ||
= help: Replace double quotes with single quotes | ||
|
||
docstring_singles_mixed_quotes_class_var_1.py:6:9: Q002 Single quote docstring found but double quotes preferred | ||
| | ||
5 | def foo(self, bar='''not a docstring'''): | ||
6 | ''"Start with empty string" ' and lint docstring safely' | ||
| ^^ Q002 | ||
7 | pass | ||
| | ||
= help: Replace single quotes docstring with double quotes | ||
|
||
docstring_singles_mixed_quotes_class_var_1.py:6:11: Q000 Double quotes found but single quotes preferred | ||
| | ||
5 | def foo(self, bar='''not a docstring'''): | ||
6 | ''"Start with empty string" ' and lint docstring safely' | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ Q000 | ||
7 | pass | ||
| | ||
= help: Replace double quotes with single quotes | ||
|
||
docstring_singles_mixed_quotes_class_var_1.py:9:29: Q002 Single quote docstring found but double quotes preferred | ||
| | ||
7 | pass | ||
8 | | ||
9 | class Nested(foo()[:]): ''"Start with empty string" ' and lint docstring safely'; pass | ||
| ^^ Q002 | ||
| | ||
= help: Replace single quotes docstring with double quotes | ||
|
||
docstring_singles_mixed_quotes_class_var_1.py:9:31: Q000 Double quotes found but single quotes preferred | ||
| | ||
7 | pass | ||
8 | | ||
9 | class Nested(foo()[:]): ''"Start with empty string" ' and lint docstring safely'; pass | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ Q000 | ||
| | ||
= help: Replace double quotes with single quotes |
Oops, something went wrong.