diff --git a/pandas/tests/frame/test_analytics.py b/pandas/tests/frame/test_analytics.py index 5c14c3cd2a2b5..6c822fd5a6822 100644 --- a/pandas/tests/frame/test_analytics.py +++ b/pandas/tests/frame/test_analytics.py @@ -2589,11 +2589,6 @@ def df_main_dtypes(): class TestNLargestNSmallest: - dtype_error_msg_template = ( - "Column {column!r} has dtype {dtype}, cannot " - "use method {method!r} with this dtype" - ) - # ---------------------------------------------------------------------- # Top / bottom @pytest.mark.parametrize( @@ -2620,8 +2615,9 @@ def test_n(self, df_strings, nselect_method, n, order): df = df_strings if "b" in order: - error_msg = self.dtype_error_msg_template.format( - column="b", method=nselect_method, dtype="object" + error_msg = ( + f"Column 'b' has dtype object, " + f"cannot use method '{nselect_method}' with this dtype" ) with pytest.raises(TypeError, match=error_msg): getattr(df, nselect_method)(n, order) @@ -2637,8 +2633,9 @@ def test_n(self, df_strings, nselect_method, n, order): def test_n_error(self, df_main_dtypes, nselect_method, columns): df = df_main_dtypes col = columns[1] - error_msg = self.dtype_error_msg_template.format( - column=col, method=nselect_method, dtype=df[col].dtype + error_msg = ( + f"Column '{col}' has dtype {df[col].dtype}, " + f"cannot use method '{nselect_method}' with this dtype" ) # escape some characters that may be in the repr error_msg = ( diff --git a/pandas/tests/groupby/test_whitelist.py b/pandas/tests/groupby/test_whitelist.py index 58407d90a2cc8..48ea2646c52fc 100644 --- a/pandas/tests/groupby/test_whitelist.py +++ b/pandas/tests/groupby/test_whitelist.py @@ -236,16 +236,23 @@ def test_groupby_blacklist(df_letters): blacklist.extend(to_methods) - # e.g., to_csv - defined_but_not_allowed = "(?:^Cannot.+{0!r}.+{1!r}.+try using the 'apply' method$)" - - # e.g., query, eval - not_defined = "(?:^{1!r} object has no attribute {0!r}$)" - fmt = defined_but_not_allowed + "|" + not_defined for bl in blacklist: for obj in (df, s): gb = obj.groupby(df.letters) - msg = fmt.format(bl, type(gb).__name__) + + # e.g., to_csv + defined_but_not_allowed = ( + f"(?:^Cannot.+{repr(bl)}.+'{type(gb).__name__}'.+try " + f"using the 'apply' method$)" + ) + + # e.g., query, eval + not_defined = ( + f"(?:^'{type(gb).__name__}' object has no attribute {repr(bl)}$)" + ) + + msg = f"{defined_but_not_allowed}|{not_defined}" + with pytest.raises(AttributeError, match=msg): getattr(gb, bl) diff --git a/pandas/tests/indexes/datetimes/test_datetime.py b/pandas/tests/indexes/datetimes/test_datetime.py index e3fa6a8321d5f..03b9502be2735 100644 --- a/pandas/tests/indexes/datetimes/test_datetime.py +++ b/pandas/tests/indexes/datetimes/test_datetime.py @@ -89,7 +89,7 @@ def test_week_of_month_frequency(self): def test_hash_error(self): index = date_range("20010101", periods=10) with pytest.raises( - TypeError, match=("unhashable type: {0.__name__!r}".format(type(index))) + TypeError, match=f"unhashable type: '{type(index).__name__}'" ): hash(index) diff --git a/pandas/tests/indexes/multi/test_integrity.py b/pandas/tests/indexes/multi/test_integrity.py index 2f2daee232efc..a8711533e806c 100644 --- a/pandas/tests/indexes/multi/test_integrity.py +++ b/pandas/tests/indexes/multi/test_integrity.py @@ -252,9 +252,7 @@ def test_rangeindex_fallback_coercion_bug(): def test_hash_error(indices): index = indices - with pytest.raises( - TypeError, match=("unhashable type: {0.__name__!r}".format(type(index))) - ): + with pytest.raises(TypeError, match=f"unhashable type: '{type(index).__name__}'"): hash(indices) diff --git a/pandas/tests/indexes/test_common.py b/pandas/tests/indexes/test_common.py index 9e60b91db5e18..82ef71efa70d0 100644 --- a/pandas/tests/indexes/test_common.py +++ b/pandas/tests/indexes/test_common.py @@ -161,7 +161,7 @@ def test_set_name_methods(self, indices): def test_hash_error(self, indices): index = indices with pytest.raises( - TypeError, match=(f"unhashable type: {type(index).__name__!r}") + TypeError, match=f"unhashable type: '{type(index).__name__}'" ): hash(indices) diff --git a/pandas/tests/io/msgpack/test_case.py b/pandas/tests/io/msgpack/test_case.py index a868da69d5459..7e13e0dd1e8ca 100644 --- a/pandas/tests/io/msgpack/test_case.py +++ b/pandas/tests/io/msgpack/test_case.py @@ -7,9 +7,7 @@ def check(length, obj): v = packb(obj) assert ( len(v) == length - ), "{obj!r} length should be {length!r} but get {got:!r}".format( - obj=obj, length=length, got=len(v) - ) + ), f"{repr(obj)} length should be {length} but got {repr(len(v))}" assert unpackb(v, use_list=0) == obj diff --git a/pandas/tests/io/msgpack/test_extension.py b/pandas/tests/io/msgpack/test_extension.py index 6d1f8cb694601..5390c8aecada7 100644 --- a/pandas/tests/io/msgpack/test_extension.py +++ b/pandas/tests/io/msgpack/test_extension.py @@ -48,7 +48,7 @@ def default(obj): typecode = 123 # application specific typecode data = tobytes(obj) return ExtType(typecode, data) - raise TypeError("Unknown type object {obj!r}".format(obj=obj)) + raise TypeError(f"Unknown type object {repr(obj)}") def ext_hook(code, data): print("ext_hook called", code, data)