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

[Autofix error] UP032 Use f-string instead of format call #6093

Closed
Azd325 opened this issue Jul 26, 2023 · 6 comments
Closed

[Autofix error] UP032 Use f-string instead of format call #6093

Azd325 opened this issue Jul 26, 2023 · 6 comments
Labels
bug Something isn't working

Comments

@Azd325
Copy link

Azd325 commented Jul 26, 2023

Code Example

testing.py

from django.contrib import messages

from apps.user.models import User


def unblock_many(employees):
    return employees


def block_many_in_14_days(user, employees):
    return user, employees


def test_view(request, employees, is_company, action):
    if request.method == "POST":
        if action == "unblock":
            messages.info(
                request,
                ("{} unblock many".format(unblock_many(employees))),
            )
        elif is_company and action == "block-in-14-days":
            messages.info(
                request,
                (
                    "{} block many in 14 days.".format(
                        block_many_in_14_days(
                            request.user, User.objects.filter(employee__in=employees)
                        )
                    )
                ),
            )

pyproject.toml

[tool.ruff]
line-length = 100
exclude = [
    ".git",
    "migrations",
    "frontend",
    "public",
    "lib",
    "lib64",
    "bin",
    ".venv",
]
ignore = [
    "E501",
    "E731",
]
select = [
    "C9",
    "E",
    "F",
    "W",
    "UP",       # pyupgrade
    "I",        # isort
    "RUF100",   # Unused noqa directive
]

CLI

ruff testing.py
testing.py:19:18: UP032 [*] Use f-string instead of `format` call
testing.py:25:21: UP032 [*] Use f-string instead of `format` call
Found 2 errors.
[*] 2 potentially fixable with the --fix option.
ruff testing.py --fix

error: Autofix introduced a syntax error. Reverting all changes.

This indicates a bug in `ruff`. If you could open an issue at:

    https://github.com/astral-sh/ruff/issues/new?title=%5BAutofix%20error%5D

...quoting the contents of `testing.py`, the rule codes UP032, along with the `pyproject.toml` settings and executed command, we'd be very appreciative!

testing.py:19:18: UP032 Use f-string instead of `format` call
testing.py:25:21: UP032 Use f-string instead of `format` call
Found 2 errors.

Version

ruff --version
ruff 0.0.280
@Azd325 Azd325 changed the title [Autofix error] [Autofix error] UP032 Use f-string instead of format call Jul 26, 2023
@charliermarsh
Copy link
Member

@harupy - any thoughts on this one?

@harupy
Copy link
Contributor

harupy commented Jul 26, 2023

Isn't this the issue fixed by #5971?

@harupy
Copy link
Contributor

harupy commented Jul 26, 2023

> git log -1
commit 77396c6f92f1515b0cb9ac1f8ba1cdbf39b1d287 (HEAD -> main, upstream/main)
Author: Harutaka Kawamura <hkawamura0130@gmail.com>
Date:   Wed Jul 26 21:37:32 2023 +0900

    Fix `SIM102` to handle indented `elif` (#6072)

> cat a.py
from django.contrib import messages

from apps.user.models import User


def unblock_many(employees):
    return employees


def block_many_in_14_days(user, employees):
    return user, employees


def test_view(request, employees, is_company, action):
    if request.method == "POST":
        if action == "unblock":
            messages.info(
                request,
                ("{} unblock many".format(unblock_many(employees))),
            )
        elif is_company and action == "block-in-14-days":
            messages.info(
                request,
                (
                    "{} block many in 14 days.".format(
                        block_many_in_14_days(
                            request.user, User.objects.filter(employee__in=employees)
                        )
                    )
                ),
            )

> cargo run -p ruff_cli -- check a.py --no-cache --select UP032 --fix
    Finished dev [unoptimized + debuginfo] target(s) in 0.08s
     Running `target/debug/ruff check a.py --no-cache --select UP032 --fix`
Found 1 error (1 fixed, 0 remaining).

@harupy
Copy link
Contributor

harupy commented Jul 26, 2023

                        block_many_in_14_days(
                            request.user, User.objects.filter(employee__in=employees)
                        )

is a multiline expression. Inserting this in an f-string causes a syntax error. #5971 fixed it.

@charliermarsh
Copy link
Member

Thank you :)

@charliermarsh charliermarsh added the bug Something isn't working label Jul 26, 2023
@Azd325
Copy link
Author

Azd325 commented Jul 26, 2023

Cool Thanks.

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

No branches or pull requests

3 participants