Skip to content

Commit 9109117

Browse files
ssikdar1WillAyd
authored andcommitted
replacing '.format' with f-strings in some test files (#30816)
1 parent 7f178c4 commit 9109117

7 files changed

+31
-48
lines changed

pandas/tests/util/test_assert_almost_equal.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ def _assert_not_almost_equal(a, b, **kwargs):
3939
"""
4040
try:
4141
tm.assert_almost_equal(a, b, **kwargs)
42-
msg = (
43-
"{a} and {b} were approximately equal when they shouldn't have been"
44-
).format(a=a, b=b)
42+
msg = f"{a} and {b} were approximately equal when they shouldn't have been"
4543
pytest.fail(msg=msg)
4644
except AssertionError:
4745
pass
@@ -248,13 +246,12 @@ def test_assert_almost_equal_value_mismatch():
248246
[(np.array([1]), 1, "ndarray", "int"), (1, np.array([1]), "int", "ndarray")],
249247
)
250248
def test_assert_almost_equal_class_mismatch(a, b, klass1, klass2):
251-
msg = """numpy array are different
249+
250+
msg = f"""numpy array are different
252251
253252
numpy array classes are different
254253
\\[left\\]: {klass1}
255-
\\[right\\]: {klass2}""".format(
256-
klass1=klass1, klass2=klass2
257-
)
254+
\\[right\\]: {klass2}"""
258255

259256
with pytest.raises(AssertionError, match=msg):
260257
tm.assert_almost_equal(a, b)

pandas/tests/util/test_assert_categorical_equal.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,11 @@ def test_categorical_equal_ordered_mismatch():
7777
@pytest.mark.parametrize("obj", ["index", "foo", "pandas"])
7878
def test_categorical_equal_object_override(obj):
7979
data = [1, 2, 3, 4]
80-
msg = """{obj} are different
80+
msg = f"""{obj} are different
8181
8282
Attribute "ordered" are different
8383
\\[left\\]: False
84-
\\[right\\]: True""".format(
85-
obj=obj
86-
)
84+
\\[right\\]: True"""
8785

8886
c1 = Categorical(data, ordered=False)
8987
c2 = Categorical(data, ordered=True)

pandas/tests/util/test_assert_extension_array_equal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def test_assert_extension_array_equal_non_extension_array(side):
9696
numpy_array = np.arange(5)
9797
extension_array = SparseArray(numpy_array)
9898

99-
msg = "{side} is not an ExtensionArray".format(side=side)
99+
msg = f"{side} is not an ExtensionArray"
100100
args = (
101101
(numpy_array, extension_array)
102102
if side == "left"

pandas/tests/util/test_assert_frame_equal.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def test_frame_equal_row_order_mismatch(check_like, obj_fixture):
8080
df2 = DataFrame({"A": [3, 2, 1], "B": [6, 5, 4]}, index=["c", "b", "a"])
8181

8282
if not check_like: # Do not ignore row-column orderings.
83-
msg = "{obj}.index are different".format(obj=obj_fixture)
83+
msg = f"{obj_fixture}.index are different"
8484
with pytest.raises(AssertionError, match=msg):
8585
tm.assert_frame_equal(df1, df2, check_like=check_like, obj=obj_fixture)
8686
else:
@@ -95,7 +95,7 @@ def test_frame_equal_row_order_mismatch(check_like, obj_fixture):
9595
],
9696
)
9797
def test_frame_equal_shape_mismatch(df1, df2, obj_fixture):
98-
msg = "{obj} are different".format(obj=obj_fixture)
98+
msg = f"{obj_fixture} are different"
9999

100100
with pytest.raises(AssertionError, match=msg):
101101
tm.assert_frame_equal(df1, df2, obj=obj_fixture)
@@ -149,13 +149,11 @@ def test_empty_dtypes(check_dtype):
149149

150150

151151
def test_frame_equal_index_mismatch(obj_fixture):
152-
msg = """{obj}\\.index are different
152+
msg = f"""{obj_fixture}\\.index are different
153153
154-
{obj}\\.index values are different \\(33\\.33333 %\\)
154+
{obj_fixture}\\.index values are different \\(33\\.33333 %\\)
155155
\\[left\\]: Index\\(\\['a', 'b', 'c'\\], dtype='object'\\)
156-
\\[right\\]: Index\\(\\['a', 'b', 'd'\\], dtype='object'\\)""".format(
157-
obj=obj_fixture
158-
)
156+
\\[right\\]: Index\\(\\['a', 'b', 'd'\\], dtype='object'\\)"""
159157

160158
df1 = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}, index=["a", "b", "c"])
161159
df2 = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}, index=["a", "b", "d"])
@@ -165,13 +163,11 @@ def test_frame_equal_index_mismatch(obj_fixture):
165163

166164

167165
def test_frame_equal_columns_mismatch(obj_fixture):
168-
msg = """{obj}\\.columns are different
166+
msg = f"""{obj_fixture}\\.columns are different
169167
170-
{obj}\\.columns values are different \\(50\\.0 %\\)
168+
{obj_fixture}\\.columns values are different \\(50\\.0 %\\)
171169
\\[left\\]: Index\\(\\['A', 'B'\\], dtype='object'\\)
172-
\\[right\\]: Index\\(\\['A', 'b'\\], dtype='object'\\)""".format(
173-
obj=obj_fixture
174-
)
170+
\\[right\\]: Index\\(\\['A', 'b'\\], dtype='object'\\)"""
175171

176172
df1 = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}, index=["a", "b", "c"])
177173
df2 = DataFrame({"A": [1, 2, 3], "b": [4, 5, 6]}, index=["a", "b", "c"])
@@ -181,13 +177,12 @@ def test_frame_equal_columns_mismatch(obj_fixture):
181177

182178

183179
def test_frame_equal_block_mismatch(by_blocks_fixture, obj_fixture):
184-
msg = """{obj}\\.iloc\\[:, 1\\] \\(column name="B"\\) are different
180+
obj = obj_fixture
181+
msg = f"""{obj}\\.iloc\\[:, 1\\] \\(column name="B"\\) are different
185182
186183
{obj}\\.iloc\\[:, 1\\] \\(column name="B"\\) values are different \\(33\\.33333 %\\)
187184
\\[left\\]: \\[4, 5, 6\\]
188-
\\[right\\]: \\[4, 5, 7\\]""".format(
189-
obj=obj_fixture
190-
)
185+
\\[right\\]: \\[4, 5, 7\\]"""
191186

192187
df1 = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
193188
df2 = DataFrame({"A": [1, 2, 3], "B": [4, 5, 7]})

pandas/tests/util/test_assert_index_equal.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,6 @@ def test_index_equal_level_values_mismatch(check_exact, check_less_precise):
135135
[(None, "x"), ("x", "x"), (np.nan, np.nan), (NaT, NaT), (np.nan, NaT)],
136136
)
137137
def test_index_equal_names(name1, name2):
138-
msg = """Index are different
139-
140-
Attribute "names" are different
141-
\\[left\\]: \\[{name1}\\]
142-
\\[right\\]: \\[{name2}\\]"""
143138

144139
idx1 = Index([1, 2, 3], name=name1)
145140
idx2 = Index([1, 2, 3], name=name2)
@@ -149,7 +144,11 @@ def test_index_equal_names(name1, name2):
149144
else:
150145
name1 = "'x'" if name1 == "x" else name1
151146
name2 = "'x'" if name2 == "x" else name2
152-
msg = msg.format(name1=name1, name2=name2)
147+
msg = f"""Index are different
148+
149+
Attribute "names" are different
150+
\\[left\\]: \\[{name1}\\]
151+
\\[right\\]: \\[{name2}\\]"""
153152

154153
with pytest.raises(AssertionError, match=msg):
155154
tm.assert_index_equal(idx1, idx2)

pandas/tests/util/test_assert_numpy_array_equal.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@ def test_assert_numpy_array_equal_bad_type():
2828
[(np.array([1]), 1, "ndarray", "int"), (1, np.array([1]), "int", "ndarray")],
2929
)
3030
def test_assert_numpy_array_equal_class_mismatch(a, b, klass1, klass2):
31-
msg = """numpy array are different
31+
msg = f"""numpy array are different
3232
3333
numpy array classes are different
3434
\\[left\\]: {klass1}
35-
\\[right\\]: {klass2}""".format(
36-
klass1=klass1, klass2=klass2
37-
)
35+
\\[right\\]: {klass2}"""
3836

3937
with pytest.raises(AssertionError, match=msg):
4038
tm.assert_numpy_array_equal(a, b)

pandas/tests/util/test_validate_args_and_kwargs.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ def test_invalid_total_length_max_length_one():
1515
actual_length = len(kwargs) + len(args) + min_fname_arg_count
1616

1717
msg = (
18-
r"{fname}\(\) takes at most {max_length} "
19-
r"argument \({actual_length} given\)".format(
20-
fname=_fname, max_length=max_length, actual_length=actual_length
21-
)
18+
fr"{_fname}\(\) takes at most {max_length} "
19+
fr"argument \({actual_length} given\)"
2220
)
2321

2422
with pytest.raises(TypeError, match=msg):
@@ -35,10 +33,8 @@ def test_invalid_total_length_max_length_multiple():
3533
actual_length = len(kwargs) + len(args) + min_fname_arg_count
3634

3735
msg = (
38-
r"{fname}\(\) takes at most {max_length} "
39-
r"arguments \({actual_length} given\)".format(
40-
fname=_fname, max_length=max_length, actual_length=actual_length
41-
)
36+
fr"{_fname}\(\) takes at most {max_length} "
37+
fr"arguments \({actual_length} given\)"
4238
)
4339

4440
with pytest.raises(TypeError, match=msg):
@@ -53,8 +49,8 @@ def test_missing_args_or_kwargs(args, kwargs):
5349
compat_args = {"foo": -5, bad_arg: 1}
5450

5551
msg = (
56-
r"the '{arg}' parameter is not supported "
57-
r"in the pandas implementation of {func}\(\)".format(arg=bad_arg, func=_fname)
52+
fr"the '{bad_arg}' parameter is not supported "
53+
fr"in the pandas implementation of {_fname}\(\)"
5854
)
5955

6056
with pytest.raises(ValueError, match=msg):

0 commit comments

Comments
 (0)