From 804786064b73d198b1f9de0cc677c2e6658bd947 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Mon, 2 Dec 2019 12:58:00 +0200 Subject: [PATCH 1/5] STY: "{foo!r}" -> "{repr(foo)}" --- pandas/tests/frame/test_analytics.py | 15 ++++++------- pandas/tests/groupby/test_whitelist.py | 21 ++++++++++++------- .../tests/indexes/datetimes/test_datetime.py | 2 +- pandas/tests/indexes/multi/test_integrity.py | 2 +- pandas/tests/indexes/test_common.py | 2 +- pandas/tests/io/msgpack/test_case.py | 4 +--- pandas/tests/io/msgpack/test_extension.py | 2 +- 7 files changed, 25 insertions(+), 23 deletions(-) diff --git a/pandas/tests/frame/test_analytics.py b/pandas/tests/frame/test_analytics.py index 005ca8d95182e..d8c84efe74e7b 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 {repr(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 {repr(col)} has dtype {df[col].dtype}, " + f"cannot use method {repr(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..5681a4517dc03 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)}.+{repr(type(gb).__name__)}.+try " + f"using the 'apply' method$)" + ) + + # e.g., query, eval + not_defined = ( + f"(?:^{repr(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 4a38e3a146c0e..97855f0abf479 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: {repr(type(index).__name__)}") ): hash(index) diff --git a/pandas/tests/indexes/multi/test_integrity.py b/pandas/tests/indexes/multi/test_integrity.py index 7cdb5cf31338a..73f53a9660a01 100644 --- a/pandas/tests/indexes/multi/test_integrity.py +++ b/pandas/tests/indexes/multi/test_integrity.py @@ -254,7 +254,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))) + TypeError, match=(f"unhashable type: {repr(type(index).__name__)}") ): hash(indices) diff --git a/pandas/tests/indexes/test_common.py b/pandas/tests/indexes/test_common.py index 9e60b91db5e18..a18395c70eda2 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: {repr(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..8ad34b36479fd 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 {repr(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) From 895906babcf8f5f98fcc1631905b9705c82286a7 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Tue, 3 Dec 2019 20:42:43 +0200 Subject: [PATCH 2/5] Fixes for @jreback review --- pandas/tests/groupby/test_whitelist.py | 4 ++-- pandas/tests/indexes/datetimes/test_datetime.py | 2 +- pandas/tests/indexes/multi/test_integrity.py | 4 +--- pandas/tests/indexes/test_common.py | 2 +- 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/pandas/tests/groupby/test_whitelist.py b/pandas/tests/groupby/test_whitelist.py index 5681a4517dc03..48ea2646c52fc 100644 --- a/pandas/tests/groupby/test_whitelist.py +++ b/pandas/tests/groupby/test_whitelist.py @@ -242,13 +242,13 @@ def test_groupby_blacklist(df_letters): # e.g., to_csv defined_but_not_allowed = ( - f"(?:^Cannot.+{repr(bl)}.+{repr(type(gb).__name__)}.+try " + f"(?:^Cannot.+{repr(bl)}.+'{type(gb).__name__}'.+try " f"using the 'apply' method$)" ) # e.g., query, eval not_defined = ( - f"(?:^{repr(type(gb).__name__)} object has no attribute {repr(bl)}$)" + f"(?:^'{type(gb).__name__}' object has no attribute {repr(bl)}$)" ) msg = f"{defined_but_not_allowed}|{not_defined}" diff --git a/pandas/tests/indexes/datetimes/test_datetime.py b/pandas/tests/indexes/datetimes/test_datetime.py index 97855f0abf479..5b680411b2d02 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=(f"unhashable type: {repr(type(index).__name__)}") + TypeError, match=f"unhashable type: {repr(type(index).__name__)}" ): hash(index) diff --git a/pandas/tests/indexes/multi/test_integrity.py b/pandas/tests/indexes/multi/test_integrity.py index 73f53a9660a01..973ed641b2cb0 100644 --- a/pandas/tests/indexes/multi/test_integrity.py +++ b/pandas/tests/indexes/multi/test_integrity.py @@ -253,9 +253,7 @@ def test_rangeindex_fallback_coercion_bug(): def test_hash_error(indices): index = indices - with pytest.raises( - TypeError, match=(f"unhashable type: {repr(type(index).__name__)}") - ): + 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 a18395c70eda2..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: {repr(type(index).__name__)}") + TypeError, match=f"unhashable type: '{type(index).__name__}'" ): hash(indices) From c49d4322ffd9018a5405768e6bf57d0d799a97de Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Wed, 4 Dec 2019 16:44:54 +0200 Subject: [PATCH 3/5] Fixes for @jreback review --- pandas/tests/frame/test_analytics.py | 2 +- pandas/tests/indexes/datetimes/test_datetime.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/frame/test_analytics.py b/pandas/tests/frame/test_analytics.py index d8c84efe74e7b..bdf083881ad67 100644 --- a/pandas/tests/frame/test_analytics.py +++ b/pandas/tests/frame/test_analytics.py @@ -2617,7 +2617,7 @@ def test_n(self, df_strings, nselect_method, n, order): error_msg = ( f"Column 'b' has dtype object, " - f"cannot use method {repr(nselect_method)} with this dtype" + f"cannot use method '{nselect_method}' with this dtype" ) with pytest.raises(TypeError, match=error_msg): getattr(df, nselect_method)(n, order) diff --git a/pandas/tests/indexes/datetimes/test_datetime.py b/pandas/tests/indexes/datetimes/test_datetime.py index fdbac9866636a..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=f"unhashable type: {repr(type(index).__name__)}" + TypeError, match=f"unhashable type: '{type(index).__name__}'" ): hash(index) From 75a5934cf08a8c9cbdb8fcfb714027a70983c3d1 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Sun, 8 Dec 2019 21:55:11 +0200 Subject: [PATCH 4/5] Fixes for @jreback review --- pandas/tests/frame/test_analytics.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/frame/test_analytics.py b/pandas/tests/frame/test_analytics.py index bdf083881ad67..fd9fd00e00eab 100644 --- a/pandas/tests/frame/test_analytics.py +++ b/pandas/tests/frame/test_analytics.py @@ -2634,8 +2634,8 @@ def test_n_error(self, df_main_dtypes, nselect_method, columns): df = df_main_dtypes col = columns[1] error_msg = ( - f"Column {repr(col)} has dtype {df[col].dtype}, " - f"cannot use method {repr(nselect_method)} with this dtype" + 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 = ( From c00edc7e0bc4e35e1bc7e5a2835d8c8ad3767bfb Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Tue, 10 Dec 2019 14:12:58 +0200 Subject: [PATCH 5/5] Removed reper from length --- pandas/tests/io/msgpack/test_case.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/msgpack/test_case.py b/pandas/tests/io/msgpack/test_case.py index 8ad34b36479fd..7e13e0dd1e8ca 100644 --- a/pandas/tests/io/msgpack/test_case.py +++ b/pandas/tests/io/msgpack/test_case.py @@ -7,7 +7,7 @@ def check(length, obj): v = packb(obj) assert ( len(v) == length - ), f"{repr(obj)} length should be {repr(length)} but got {repr(len(v))}" + ), f"{repr(obj)} length should be {length} but got {repr(len(v))}" assert unpackb(v, use_list=0) == obj