Skip to content

Commit 1f3708a

Browse files
authored
Update old string formatting to f-strings (#1018)
1 parent 9c64b44 commit 1f3708a

File tree

9 files changed

+32
-40
lines changed

9 files changed

+32
-40
lines changed

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
# General information about the project.
2929
project = 'pytest-django'
30-
copyright = '%d, Andreas Pelme and contributors' % datetime.date.today().year
30+
copyright = f'{datetime.date.today().year}, Andreas Pelme and contributors'
3131

3232
exclude_patterns = ['_build']
3333

pytest_django/fixtures.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def teardown_database() -> None:
132132
except Exception as exc:
133133
request.node.warn(
134134
pytest.PytestWarning(
135-
"Error when trying to teardown test databases: %r" % exc
135+
f"Error when trying to teardown test databases: {exc!r}"
136136
)
137137
)
138138

@@ -287,7 +287,7 @@ def _set_suffix_to_test_databases(suffix: str) -> None:
287287
if not test_name:
288288
if db_settings["ENGINE"] == "django.db.backends.sqlite3":
289289
continue
290-
test_name = "test_{}".format(db_settings["NAME"])
290+
test_name = f"test_{db_settings['NAME']}"
291291

292292
if test_name == ":memory:":
293293
continue
@@ -591,13 +591,11 @@ def _assert_num_queries(
591591
else:
592592
failed = num_performed > num
593593
if failed:
594-
msg = "Expected to perform {} queries {}{}".format(
595-
num,
596-
"" if exact else "or less ",
597-
"but {} done".format(
598-
num_performed == 1 and "1 was" or f"{num_performed} were"
599-
),
600-
)
594+
msg = f"Expected to perform {num} queries "
595+
if not exact:
596+
msg += "or less "
597+
verb = "was" if num_performed == 1 else "were"
598+
msg += f"but {num_performed} {verb} done"
601599
if info:
602600
msg += f"\n{info}"
603601
if verbose:

pytest_django/live_server_helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,4 @@ def __add__(self, other) -> str:
7878
return f"{self}{other}"
7979

8080
def __repr__(self) -> str:
81-
return "<LiveServer listening at %s>" % self.url
81+
return f"<LiveServer listening at {self.url}>"

pytest_django/plugin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,9 @@ def _get_boolean_value(
251251
try:
252252
return possible_values[x.lower()]
253253
except KeyError:
254+
possible = ", ".join(possible_values)
254255
raise ValueError(
255-
"{} is not a valid value for {}. "
256-
"It must be one of {}.".format(x, name, ", ".join(possible_values.keys()))
256+
f"{x} is not a valid value for {name}. It must be one of {possible}."
257257
)
258258

259259

@@ -621,7 +621,7 @@ def __mod__(self, var: str) -> str:
621621
if origin:
622622
msg = f"Undefined template variable '{var}' in '{origin}'"
623623
else:
624-
msg = "Undefined template variable '%s'" % var
624+
msg = f"Undefined template variable '{var}'"
625625
if self.fail:
626626
pytest.fail(msg)
627627
else:

pytest_django_test/app/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ def admin_required_view(request: HttpRequest) -> HttpResponse:
1111

1212

1313
def item_count(request: HttpRequest) -> HttpResponse:
14-
return HttpResponse("Item count: %d" % Item.objects.count())
14+
return HttpResponse(f"Item count: {Item.objects.count()}")

pytest_django_test/db_helpers.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,18 @@ def drop_database(db_suffix=None):
102102
db_engine = get_db_engine()
103103

104104
if db_engine == "postgresql":
105-
r = run_psql("postgres", "-c", "DROP DATABASE %s" % name)
105+
r = run_psql("postgres", "-c", f"DROP DATABASE {name}")
106106
assert "DROP DATABASE" in force_str(
107107
r.std_out
108108
) or "does not exist" in force_str(r.std_err)
109109
return
110110

111111
if db_engine == "mysql":
112-
r = run_mysql("-e", "DROP DATABASE %s" % name)
112+
r = run_mysql("-e", f"DROP DATABASE {name}")
113113
assert "database doesn't exist" in force_str(r.std_err) or r.status_code == 0
114114
return
115115

116-
assert db_engine == "sqlite3", "%s cannot be tested properly!" % db_engine
116+
assert db_engine == "sqlite3", f"{db_engine} cannot be tested properly!"
117117
assert name != ":memory:", "sqlite in-memory database cannot be dropped!"
118118
if os.path.exists(name): # pragma: no branch
119119
os.unlink(name)
@@ -131,7 +131,7 @@ def db_exists(db_suffix=None):
131131
r = run_mysql(name, "-e", "SELECT 1")
132132
return r.status_code == 0
133133

134-
assert db_engine == "sqlite3", "%s cannot be tested properly!" % db_engine
134+
assert db_engine == "sqlite3", f"{db_engine} cannot be tested properly!"
135135
assert TEST_DB_NAME != ":memory:", (
136136
"sqlite in-memory database cannot be checked for existence!")
137137
return os.path.exists(name)
@@ -150,7 +150,7 @@ def mark_database():
150150
assert r.status_code == 0
151151
return
152152

153-
assert db_engine == "sqlite3", "%s cannot be tested properly!" % db_engine
153+
assert db_engine == "sqlite3", f"{db_engine} cannot be tested properly!"
154154
assert TEST_DB_NAME != ":memory:", (
155155
"sqlite in-memory database cannot be marked!")
156156

@@ -175,7 +175,7 @@ def mark_exists():
175175

176176
return r.status_code == 0
177177

178-
assert db_engine == "sqlite3", "%s cannot be tested properly!" % db_engine
178+
assert db_engine == "sqlite3", f"{db_engine} cannot be tested properly!"
179179
assert TEST_DB_NAME != ":memory:", (
180180
"sqlite in-memory database cannot be checked for mark!")
181181

tests/test_django_settings_module.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -310,12 +310,10 @@ def test_debug_is_false():
310310
@pytest.mark.parametrize('django_debug_mode', (False, True))
311311
def test_django_debug_mode_true_false(testdir, monkeypatch, django_debug_mode: bool) -> None:
312312
monkeypatch.delenv("DJANGO_SETTINGS_MODULE")
313-
testdir.makeini(
314-
"""
313+
testdir.makeini(f"""
315314
[pytest]
316-
django_debug_mode = {}
317-
""".format(django_debug_mode)
318-
)
315+
django_debug_mode = {django_debug_mode}
316+
""")
319317
testdir.makeconftest(
320318
"""
321319
from django.conf import settings
@@ -331,13 +329,11 @@ def pytest_configure():
331329
""" % (not django_debug_mode)
332330
)
333331

334-
testdir.makepyfile(
335-
"""
332+
testdir.makepyfile(f"""
336333
from django.conf import settings
337334
def test_debug_is_false():
338-
assert settings.DEBUG is {}
339-
""".format(django_debug_mode)
340-
)
335+
assert settings.DEBUG is {django_debug_mode}
336+
""")
341337

342338
r = testdir.runpytest_subprocess()
343339
assert r.ret == 0
@@ -368,11 +364,11 @@ def pytest_configure():
368364
)
369365

370366
testdir.makepyfile(
371-
"""
367+
f"""
372368
from django.conf import settings
373369
def test_debug_is_false():
374-
assert settings.DEBUG is {}
375-
""".format(settings_debug)
370+
assert settings.DEBUG is {settings_debug}
371+
"""
376372
)
377373

378374
r = testdir.runpytest_subprocess()

tests/test_environment.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ def test_ignore(client):
9696
result.stdout.fnmatch_lines_random(
9797
[
9898
"tpkg/test_the_test.py F.*",
99-
"E * Failed: Undefined template variable 'invalid_var' in {}".format(
100-
origin
101-
),
99+
f"E * Failed: Undefined template variable 'invalid_var' in {origin}",
102100
]
103101
)
104102

tests/test_fixtures.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ def test_with_live_server(live_server):
546546
% port
547547
)
548548

549-
django_testdir.runpytest_subprocess("--liveserver=localhost:%s" % port)
549+
django_testdir.runpytest_subprocess(f"--liveserver=localhost:{port}")
550550

551551

552552
@pytest.mark.parametrize("username_field", ("email", "identifier"))
@@ -565,7 +565,7 @@ def test_with_live_server(live_server):
565565
)
566566
def test_custom_user_model(django_testdir, username_field) -> None:
567567
django_testdir.create_app_file(
568-
"""
568+
f"""
569569
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
570570
from django.db import models
571571
@@ -599,7 +599,7 @@ class MyCustomUser(AbstractBaseUser, PermissionsMixin):
599599
objects = MyCustomUserManager()
600600
601601
USERNAME_FIELD = '{username_field}'
602-
""".format(username_field=username_field),
602+
""",
603603
"models.py",
604604
)
605605
django_testdir.create_app_file(

0 commit comments

Comments
 (0)