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

Prefer f-strings instead of str.format #2164

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions piptools/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ def resolve(self, max_rounds: int = 10) -> set[InstallRequirement]:
raise RuntimeError(
"No stable configuration of concrete packages "
"could be found for the given constraints after "
"{max_rounds} rounds of resolving.\n"
"This is likely a bug.".format(max_rounds=max_rounds)
f"{max_rounds} rounds of resolving.\n"
"This is likely a bug."
)

log.debug("")
Expand Down Expand Up @@ -429,9 +429,9 @@ def get_best_match(self, ireq: InstallRequirement) -> InstallRequirement:

# Format the best match
log.debug(
Copy link
Member

Choose a reason for hiding this comment

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

Logging is supposed to pass params through dedicated arguments, though. Instead of pre-rendering strings before passing them into logging..

Copy link
Member Author

Choose a reason for hiding this comment

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

That's exactly what my change does.

Previously a pre-rendered string was being passed (one using .format()). Now dedicated arguments are passed.

"found candidate {} (constraint was {})".format(
format_requirement(best_match), format_specifier(ireq)
)
"found candidate %s (constraint was %s)",
format_requirement(best_match),
format_specifier(ireq),
)
best_match.comes_from = ireq.comes_from
if hasattr(ireq, "_source_ireqs"):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_cli_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1058,14 +1058,14 @@ def test_generate_hashes_with_editable(pip_conf, runner):
fp.write(f"-e {small_fake_package_url}\n")
out = runner.invoke(cli, ["--no-annotate", "--generate-hashes"])
expected = (
"-e {}\n"
f"-e {small_fake_package_url}\n"
"small-fake-a==0.1 \\\n"
" --hash=sha256:5e6071ee6e4c59e0d0408d366f"
"e9b66781d2cf01be9a6e19a2433bb3c5336330\n"
"small-fake-b==0.1 \\\n"
" --hash=sha256:acdba8f8b8a816213c30d5310c"
"3fe296c0107b16ed452062f7f994a5672e3b3f\n"
).format(small_fake_package_url)
)
assert out.exit_code == 0
assert expected in out.stderr

Expand Down
6 changes: 3 additions & 3 deletions tests/test_cli_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ def test_pip_install_flags(run, cli_flags, expected_install_flags, runner):

call_args = [call[0][0] for call in run.call_args_list]
called_install_options = [args[6:] for args in call_args if args[3] == "install"]
assert called_install_options == [expected_install_flags], "Called args: {}".format(
call_args
)
assert called_install_options == [
expected_install_flags
], f"Called args: {call_args}"


@pytest.mark.parametrize(
Expand Down
Loading