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

ruff fix on UP032 breaks applied code when f-string is too long #7891

Closed
Grosskopf opened this issue Oct 10, 2023 · 1 comment · Fixed by #7898
Closed

ruff fix on UP032 breaks applied code when f-string is too long #7891

Grosskopf opened this issue Oct 10, 2023 · 1 comment · Fixed by #7898
Assignees
Labels
bug Something isn't working

Comments

@Grosskopf
Copy link

Grosskopf commented Oct 10, 2023

Hi, I'm not sure where I should post this error but I have a weird chain reaction of errors that result in ruff breaking my code.

The following code results in a UP032 Error:

from django.utils.translation import gettext

long = 'long'
split_to = 'split_to'
gettext(
    'some super {} and complicated string so that the error code '
    'E501 Triggers when this is not {} multi-line'.format(
        long, split_to)
)

when I then run ruff --fix on it I get

from django.utils.translation import gettext

long = 'long'
split_to = 'split_to'
gettext(
    f'some super {long} and complicated string so that the error code '
    f'E501 Triggers when this is not {split_to} multi-line'
)

This would be perfect, I love the style of code, but there is a problem, this code does not work.
When I then go ahead and run makemessages as usual, the text generated from this is
some super {long} and complicated string so that the error code and the second line is lost

Should I report this upstream at Django or at python? I would prefer if this would work but sadly this is a rare edge case where ruff --fix breaks code.

this happens on ruff 0.0.290

@charliermarsh
Copy link
Member

It looks like gettext does not yet support f-strings, per the Django docs:

Screen Shot 2023-10-10 at 12 05 33 PM

We can avoid suggesting this in Ruff, for gettext calls...

@charliermarsh charliermarsh added the bug Something isn't working label Oct 10, 2023
@charliermarsh charliermarsh self-assigned this Oct 10, 2023
charliermarsh added a commit that referenced this issue Oct 10, 2023
## Summary

Django's `gettext` doesn't support f-strings, so we should avoid
translating `.format` calls in those cases.

Closes #7891.
konstin pushed a commit that referenced this issue Oct 11, 2023
## Summary

Django's `gettext` doesn't support f-strings, so we should avoid
translating `.format` calls in those cases.

Closes #7891.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants