Skip to content

fix issue-3405#3411

Merged
sobolevn merged 15 commits intowemake-services:masterfrom
mkaraev:issue-3405-forbid-multiline-f-string
May 9, 2025
Merged

fix issue-3405#3411
sobolevn merged 15 commits intowemake-services:masterfrom
mkaraev:issue-3405-forbid-multiline-f-string

Conversation

@mkaraev
Copy link
Copy Markdown
Contributor

@mkaraev mkaraev commented May 8, 2025

I have made things!

Checklist

  • I have double checked that there are no unrelated changes in this pull request (old patches, accidental config files, etc)
  • I have created at least one test case for the changes I have made
  • I have updated the documentation for the changes I have made
  • I have added my changes to the CHANGELOG.md

Related issues

closes: #3405

Comment thread CHANGELOG.md Outdated
### Features

- Adds `WPS478`: forbids using non strict slice operations, #1011
- Adds `WPS479`: forbids using multiline f strings, #3405
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
- Adds `WPS479`: forbids using multiline f strings, #3405
- Adds `WPS479`: forbids using multiline fstrings, #3405

MultilineFormattedStringTokenVisitor,
)

single_quote_formatted_string_wrong = """x=f'{ 1
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please, add all required fields: reasoning, solution and examples.

self._checker.check_string_modifiers(token, modifiers)


class MultilineFormattedStringTokenVisitor(BaseTokenVisitor):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
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',
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
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.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
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.

Comment on lines +3095 to +3100
x = f\"\"\" { 1
...}\"\"\"

# Wrong:
x = f\" { 1
...}\"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
x = f\"\"\" { 1
...}\"\"\"
# Wrong:
x = f\" { 1
...}\"
x = f'''{ 1
}'''
# Wrong:
x = f'{ 1
}'

@codecov
Copy link
Copy Markdown

codecov Bot commented May 9, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (214bbc0) to head (c84d410).
⚠️ Report is 80 commits behind head on master.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mkaraev
Copy link
Copy Markdown
Contributor Author

mkaraev commented May 9, 2025

@sobolevn I've fixed all comments.

@mkaraev mkaraev requested a review from sobolevn May 9, 2025 12:07
Copy link
Copy Markdown
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Almost done!

_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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

there can also be a fr prefix. please, add a test case for it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed

@mkaraev
Copy link
Copy Markdown
Contributor Author

mkaraev commented May 9, 2025

@sobolevn
For python 3.11 and python 3.10 checks I'm getting error that coverage is less than 100% for test file.
ERROR: Coverage failure: total of 99 is less than fail-under=100
However I've already added:

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 cover

I don't understand why the pipelines are failing.
Could you please provide some information on how to fix them?

P.S. I've added suffix 312 to tests file name. I dont know if that would help :)

@mkaraev mkaraev requested a review from sobolevn May 9, 2025 17:39
@sobolevn
Copy link
Copy Markdown
Member

sobolevn commented May 9, 2025

take a look at 5ea5c47 you need a 312 suffix

…tokens/test_mulitiline_formatted_string312.py
@sobolevn sobolevn merged commit 1d95728 into wemake-services:master May 9, 2025
9 checks passed
@sobolevn
Copy link
Copy Markdown
Member

sobolevn commented May 9, 2025

Thanks a lot!

@mkaraev
Copy link
Copy Markdown
Contributor Author

mkaraev commented May 10, 2025

@sobolevn Thanks for your comments and help. That was a really great experience. I will try to help with more contributions :)

@mkaraev mkaraev deleted the issue-3405-forbid-multiline-f-string branch May 10, 2025 04:20
@sobolevn
Copy link
Copy Markdown
Member

Awesome, please, also check other projects :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Forbid multiline f and t strings

2 participants