fix issue-3405#3411
Conversation
| ### Features | ||
|
|
||
| - Adds `WPS478`: forbids using non strict slice operations, #1011 | ||
| - Adds `WPS479`: forbids using multiline f strings, #3405 |
There was a problem hiding this comment.
| - Adds `WPS479`: forbids using multiline f strings, #3405 | |
| - Adds `WPS479`: forbids using multiline fstrings, #3405 |
| MultilineFormattedStringTokenVisitor, | ||
| ) | ||
|
|
||
| single_quote_formatted_string_wrong = """x=f'{ 1 |
There was a problem hiding this comment.
Please, separete # Correct: and # Wrong: cases by comments. It would be easier to read
| @final | ||
| class MultilineFormattedStringViolation(TokenizeViolation): | ||
| """ | ||
| Forbid using multi-line formatted string with single and double quotes. |
There was a problem hiding this comment.
Please, add all required fields: reasoning, solution and examples.
| self._checker.check_string_modifiers(token, modifiers) | ||
|
|
||
|
|
||
| class MultilineFormattedStringTokenVisitor(BaseTokenVisitor): |
There was a problem hiding this comment.
| class MultilineFormattedStringTokenVisitor(BaseTokenVisitor): | |
| @final | |
| class MultilineFormattedStringTokenVisitor(BaseTokenVisitor): |
| """Checks incorrect formatted string usages.""" | ||
|
|
||
| _multiline_fstring_pattern: ClassVar[re.Pattern[str]] = re.compile( | ||
| r'.*f([\'\"])(?!\1\1).*(\{.*\}.)*.*\{[^\}]*\n', |
There was a problem hiding this comment.
please use multiline regex with comments: I can't read it right now.
| Multiline f-strings must use triple quotes for clarity; single f-strings may not span lines. | ||
|
|
||
| Solution: | ||
| Use \"\"\" quotes instead of single quotes. |
There was a problem hiding this comment.
| Use \"\"\" quotes instead of single quotes. | |
| Use triple quotes instead of single quotes. |
| Forbid using multi-line formatted string with single and double quotes. | ||
|
|
||
| Reasoning: | ||
| Multiline f-strings must use triple quotes for clarity; single f-strings may not span lines. |
There was a problem hiding this comment.
| Multiline f-strings must use triple quotes for clarity; single f-strings may not span lines. | |
| Multiline fstrings must use triple quotes for clarity; single fstrings must not span lines. |
| x = f\"\"\" { 1 | ||
| ...}\"\"\" | ||
|
|
||
| # Wrong: | ||
| x = f\" { 1 | ||
| ...}\" |
There was a problem hiding this comment.
| x = f\"\"\" { 1 | |
| ...}\"\"\" | |
| # Wrong: | |
| x = f\" { 1 | |
| ...}\" | |
| x = f'''{ 1 | |
| }''' | |
| # Wrong: | |
| x = f'{ 1 | |
| }' |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3411 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 362 362
Lines 11921 11933 +12
Branches 815 816 +1
=========================================
+ Hits 11921 11933 +12 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@sobolevn I've fixed all comments. |
| _multiline_fstring_pattern: ClassVar[re.Pattern[str]] = re.compile( | ||
| r""" | ||
| .* # (1) anything before the f-string | ||
| f(['"]) # (2) the `f` prefix + a single or double quote |
There was a problem hiding this comment.
there can also be a fr prefix. please, add a test case for it.
|
@sobolevn if not PY312: # pragma: >=3.12 no cover
pytest.skip(
reason='unterminated string literal was added in 3.12',
allow_module_level=True,
) # pragma: no coverI don't understand why the pipelines are failing. P.S. I've added suffix 312 to tests file name. I dont know if that would help :) |
|
take a look at 5ea5c47 you need a |
…tokens/test_mulitiline_formatted_string312.py
|
Thanks a lot! |
|
@sobolevn Thanks for your comments and help. That was a really great experience. I will try to help with more contributions :) |
|
Awesome, please, also check other projects :) |
I have made things!
Checklist
CHANGELOG.mdRelated issues
closes: #3405