Skip to content

Commit 92f9731

Browse files
committed
chore: more fstrings with flynt's help
1 parent 9df72b4 commit 92f9731

File tree

8 files changed

+25
-40
lines changed

8 files changed

+25
-40
lines changed

docs/benchmark.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ def generate_dummy_code_pybind11(nclasses=10):
1111
bindings = ""
1212

1313
for cl in range(nclasses):
14-
decl += "class cl%03i;\n" % cl
14+
decl += f"class cl{cl:03};\n"
1515
decl += "\n"
1616

1717
for cl in range(nclasses):
18-
decl += "class cl%03i {\n" % cl
18+
decl += f"class {cl:03} {{\n"
1919
decl += "public:\n"
20-
bindings += ' py::class_<cl%03i>(m, "cl%03i")\n' % (cl, cl)
20+
bindings += f' py::class_<cl{cl:03}>(m, "cl{cl:03}")\n'
2121
for fn in range(nfns):
2222
ret = random.randint(0, nclasses - 1)
2323
params = [random.randint(0, nclasses - 1) for i in range(nargs)]
24-
decl += " cl%03i *fn_%03i(" % (ret, fn)
25-
decl += ", ".join("cl%03i *" % p for p in params)
24+
decl += f" cl{ret:03} *fn_{fn:03}("
25+
decl += ", ".join(f"cl{p:03} *" for p in params)
2626
decl += ");\n"
27-
bindings += ' .def("fn_%03i", &cl%03i::fn_%03i)\n' % (fn, cl, fn)
27+
bindings += f' .def("fn_{fn:03}", &cl{cl:03}::fn_{fn:03})\n'
2828
decl += "};\n\n"
2929
bindings += " ;\n"
3030

@@ -42,23 +42,20 @@ def generate_dummy_code_boost(nclasses=10):
4242
bindings = ""
4343

4444
for cl in range(nclasses):
45-
decl += "class cl%03i;\n" % cl
45+
decl += f"class cl{cl:03};\n"
4646
decl += "\n"
4747

4848
for cl in range(nclasses):
4949
decl += "class cl%03i {\n" % cl
5050
decl += "public:\n"
51-
bindings += ' py::class_<cl%03i>("cl%03i")\n' % (cl, cl)
51+
bindings += f' py::class_<cl{cl:03}>("cl{cl:03}")\n'
5252
for fn in range(nfns):
5353
ret = random.randint(0, nclasses - 1)
5454
params = [random.randint(0, nclasses - 1) for i in range(nargs)]
55-
decl += " cl%03i *fn_%03i(" % (ret, fn)
56-
decl += ", ".join("cl%03i *" % p for p in params)
55+
decl += f" cl{ret:03} *fn_{fn:03}("
56+
decl += ", ".join(f"cl{p:03} *" for p in params)
5757
decl += ");\n"
58-
bindings += (
59-
' .def("fn_%03i", &cl%03i::fn_%03i, py::return_value_policy<py::manage_new_object>())\n'
60-
% (fn, cl, fn)
61-
)
58+
bindings += f' .def("fn_{fn:03}", &cl{cl:03}::fn_{fn:03}, py::return_value_policy<py::manage_new_object>())\n'
6259
decl += "};\n\n"
6360
bindings += " ;\n"
6461

pybind11/setup_helpers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,9 @@ def intree_extensions(
322322

323323
if not exts:
324324
msg = (
325-
"path {path} is not a child of any of the directories listed "
326-
"in 'package_dir' ({package_dir})"
327-
).format(path=path, package_dir=package_dir)
325+
f"path {path} is not a child of any of the directories listed "
326+
f"in 'package_dir' ({package_dir})"
327+
)
328328
raise ValueError(msg)
329329

330330
return exts

tests/extra_python_package/test_files.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,7 @@ def tests_build_wheel(monkeypatch, tmpdir):
241241
names = z.namelist()
242242

243243
trimmed = {n for n in names if "dist-info" not in n}
244-
trimmed |= {
245-
"dist-info/{}".format(n.split("/", 1)[-1]) for n in names if "dist-info" in n
246-
}
244+
trimmed |= {f"dist-info/{n.split('/', 1)[-1]}" for n in names if "dist-info" in n}
247245
assert files == trimmed
248246

249247

tests/extra_setuptools/test_setuphelper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_simple_setup_py(monkeypatch, tmpdir, parallel, std):
1818

1919
(tmpdir / "setup.py").write_text(
2020
dedent(
21-
"""\
21+
f"""\
2222
import sys
2323
sys.path.append({MAIN_DIR!r})
2424
@@ -51,7 +51,7 @@ def test_simple_setup_py(monkeypatch, tmpdir, parallel, std):
5151
ext_modules=ext_modules,
5252
)
5353
"""
54-
).format(MAIN_DIR=MAIN_DIR, std=std, parallel=parallel),
54+
),
5555
encoding="ascii",
5656
)
5757

tests/test_callbacks.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,8 @@ def test_callback_num_times():
188188
if not rep:
189189
print()
190190
print(
191-
"callback_num_times: {:d} million / {:.3f} seconds = {:.3f} million / second".format(
192-
num_millions, td, rate
193-
)
191+
f"callback_num_times: {num_millions:d} million / {td:.3f} seconds = {rate:.3f} million / second"
194192
)
195193
if len(rates) > 1:
196194
print("Min Mean Max")
197-
print(
198-
"{:6.3f} {:6.3f} {:6.3f}".format(
199-
min(rates), sum(rates) / len(rates), max(rates)
200-
)
201-
)
195+
print(f"{min(rates):6.3f} {sum(rates) / len(rates):6.3f} {max(rates):6.3f}")

tests/test_numpy_array.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ def test_dtypes():
1818
assert check.numpy == check.pybind11, check
1919
if check.numpy.num != check.pybind11.num:
2020
print(
21-
"NOTE: typenum mismatch for {}: {} != {}".format(
22-
check, check.numpy.num, check.pybind11.num
23-
)
21+
f"NOTE: typenum mismatch for {check}: {check.numpy.num} != {check.pybind11.num}"
2422
)
2523

2624

@@ -116,9 +114,7 @@ def test_at_fail(arr, dim):
116114
for func in m.at_t, m.mutate_at_t:
117115
with pytest.raises(IndexError) as excinfo:
118116
func(arr, *([0] * dim))
119-
assert str(excinfo.value) == "index dimension mismatch: {} (ndim = 2)".format(
120-
dim
121-
)
117+
assert str(excinfo.value) == f"index dimension mismatch: {dim} (ndim = 2)"
122118

123119

124120
def test_at(arr):

tests/test_pytypes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ def test_non_converting_constructors():
305305
for move in [True, False]:
306306
with pytest.raises(TypeError) as excinfo:
307307
m.nonconverting_constructor(t, v, move)
308-
expected_error = "Object of type '{}' is not an instance of '{}'".format(
309-
type(v).__name__, t
308+
expected_error = (
309+
f"Object of type '{type(v).__name__}' is not an instance of '{t}'"
310310
)
311311
assert str(excinfo.value) == expected_error
312312

tests/test_virtual_functions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def __init__(self, state):
1313
self.data = "Hello world"
1414

1515
def run(self, value):
16-
print("ExtendedExampleVirt::run(%i), calling parent.." % value)
16+
print(f"ExtendedExampleVirt::run({value}), calling parent..")
1717
return super().run(value + 1)
1818

1919
def run_bool(self):
@@ -24,7 +24,7 @@ def get_string1(self):
2424
return "override1"
2525

2626
def pure_virtual(self):
27-
print("ExtendedExampleVirt::pure_virtual(): %s" % self.data)
27+
print(f"ExtendedExampleVirt::pure_virtual(): {self.data}")
2828

2929
class ExtendedExampleVirt2(ExtendedExampleVirt):
3030
def __init__(self, state):

0 commit comments

Comments
 (0)