Skip to content

Avoid repeating function calls in f-string conversions#10265

Merged
charliermarsh merged 1 commit intomainfrom
charlie/up-function
Mar 7, 2024
Merged

Avoid repeating function calls in f-string conversions#10265
charliermarsh merged 1 commit intomainfrom
charlie/up-function

Conversation

@charliermarsh
Copy link
Member

@charliermarsh charliermarsh commented Mar 7, 2024

Summary

Given a format string like "{x} {x}".format(x=foo()), we should avoid converting to an f-string, since doing so would require repeating the function call (f"{foo()} {foo()}"), which could introduce side effects.

Closes #10258.

@charliermarsh charliermarsh added the bug Something isn't working label Mar 7, 2024
@charliermarsh
Copy link
Member Author

We could arguably make this an unsafe fix, but I think it's unlikely to be a desirable change for users.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 7, 2024

ruff-ecosystem results

Linter (stable)

ℹ️ ecosystem check detected linter changes. (+0 -4 violations, +0 -0 fixes in 3 projects; 40 projects unchanged)

pypa/pip (+0 -1 violations, +0 -0 fixes)

- tests/functional/test_install.py:269:9: UP032 Use f-string instead of `format` call

zulip/zulip (+0 -1 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --no-preview --select ALL

- corporate/lib/stripe.py:2207:28: UP032 Use f-string instead of `format` call

indico/indico (+0 -2 violations, +0 -0 fixes)

- indico/core/db/sqlalchemy/custom/greatest.py:24:12: UP032 Use f-string instead of `format` call
- indico/core/db/sqlalchemy/custom/least.py:24:12: UP032 Use f-string instead of `format` call

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
UP032 4 0 4 0 0

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+0 -4 violations, +0 -0 fixes in 3 projects; 40 projects unchanged)

pypa/pip (+0 -1 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

- tests/functional/test_install.py:269:9: UP032 Use f-string instead of `format` call

zulip/zulip (+0 -1 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview --select ALL

- corporate/lib/stripe.py:2207:28: UP032 Use f-string instead of `format` call

indico/indico (+0 -2 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

- indico/core/db/sqlalchemy/custom/greatest.py:24:12: UP032 Use f-string instead of `format` call
- indico/core/db/sqlalchemy/custom/least.py:24:12: UP032 Use f-string instead of `format` call

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
UP032 4 0 4 0 0

@charliermarsh
Copy link
Member Author

\cc @ThiefMaster who filed this

@charliermarsh charliermarsh merged commit 461cdad into main Mar 7, 2024
@charliermarsh charliermarsh deleted the charlie/up-function branch March 7, 2024 04:33
nkxxll pushed a commit to nkxxll/ruff that referenced this pull request Mar 10, 2024
## Summary

Given a format string like `"{x} {x}".format(x=foo())`, we should avoid
converting to an f-string, since doing so would require repeating the
function call (`f"{foo()} {foo()}"`), which could introduce side
effects.

Closes astral-sh#10258.
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 this pull request may close these issues.

UP032 complains about format args performing function calls

1 participant