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

Strangeness with implicit string concatenation, f-strings, and rules F541, RUF027 #11357

Closed
dougthor42 opened this issue May 10, 2024 · 3 comments
Labels
question Asking for support or clarification

Comments

@dougthor42
Copy link
Contributor

Summary

Seems like f-string-missing-placeholders (F541) and missing-f-string-syntax (RUF027) don't play nicely with implicit string concatenation.

$ ruff --version
ruff 0.4.4
$ python --version
Python 3.8.13

Examples:

Commands used:

$ ruff check --select F541 --preview --isolated ruff_bug.py
$ ruff check --select RUF027 --preview --isolated ruff_bug.py
Code Expected F541 Actual F541 Expected RUF027 Actual RUF027
# ruff_bug.py
a = "hello"
print(
    f"some text here"
    f" bar {a}."
    f" {a}"
)
Error on line 4 All checks passed! All checks passed! All checks passed!
# ruff_bug.py
a = "hello"
print(
    f"some text here"
    f" bar {a}."
    " {a}"
)
Error on line 4 All checks passed! Error on line 6 All checks passed!
# ruff_bug.py
a = "hello"
print(
    "some text here"
    f" bar {a}."
    " {a}"
)
All checks passed! All checks passed! Error on line 6 All checks passed!
# ruff_bug.py
a = "hello"
print(
    f"some text here"
    " bar {a}."
    " {a}"
)
Error on line 4 Error on line 4 Error on line 5 and 6 All checks passed!
# ruff_bug.py
a = "hello"
print(
    "some text here"
    " bar {a}."
    f" {a}"
)
All checks passed! All checks passed! Error on line 5 All checks passed!
@dhruvmanila
Copy link
Member

Thank you providing such a detailed set of examples along with the diagnosis!

Regarding F541, we decided to maintain this behavior where the F541 rule is only flagged when all of the f-strings in an implicit string concatenation doesn't have any placeholders. Refer to #10885 for more details.

I've pasted all of the examples in the playground (https://play.ruff.rs/0de4d4a3-bbb3-44bd-b163-2778dba84f64) and I'm seeing the expected behavior for RUF027 for examples 2, 3, 4, and 5.

And, I'm seeing the same behavior on the CLI as well:

$ ruff check --select RUF027 --preview --isolated src/RUF027.py --output-format=concise
src/RUF027.py:14:5: RUF027 Possible f-string without an `f` prefix
src/RUF027.py:22:5: RUF027 Possible f-string without an `f` prefix
src/RUF027.py:29:5: RUF027 Possible f-string without an `f` prefix
src/RUF027.py:30:5: RUF027 Possible f-string without an `f` prefix
src/RUF027.py:37:5: RUF027 Possible f-string without an `f` prefix
Found 5 errors.
No fixes available (5 hidden fixes can be enabled with the `--unsafe-fixes` option).

I'm not sure why you're seeing different behavior for RUF027. Can you re-check?

@dhruvmanila dhruvmanila added the question Asking for support or clarification label May 10, 2024
@dougthor42
Copy link
Contributor Author

dougthor42 commented May 10, 2024

F541

Ah ok, I didn't realize F541 was expected behavior.:+1:. I plan on making a PR to update the docs https://docs.astral.sh/ruff/rules/f-string-missing-placeholders/ to include something like your comment. Perhaps:

Note regarding implicit string concatenation: In order to maintain compatibilty with the source pyflakes rule, this rule is only flagged when all of the f-strings in an implicit string concatenation don't have any placeholders. For example:

Please see Issue #10885 for more details.

RUF027

Strange! Yeah I'll double check.

...

Oh son of a b*. My CLI arg was set to RUF029 🤦

Sorry about that! It looks like everything is WAI so I'll close this. Thanks for double checking!

@dhruvmanila
Copy link
Member

No worries, thanks for confirming!

Ah ok, I didn't realize F541 was expected behavior.:+1:. I plan on making a PR to update the docs docs.astral.sh/ruff/rules/f-string-missing-placeholders to include something like your comment. Perhaps:

Yeah, I think that would be useful

charliermarsh pushed a commit that referenced this issue May 10, 2024
Addresses this comment:
#11357 (comment)


## Summary

The docs for F541 did not mention some surprising, but WAI, behavior
regarding implicit string concatenation. Update the docs to describe the
behavior.

Here's how things rendered for me locally:


![image](https://github.com/astral-sh/ruff/assets/5386897/32067121-b190-4268-b987-ff37df11a618)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Asking for support or clarification
Projects
None yet
Development

No branches or pull requests

2 participants