From b49d1f7e2b36ea3c2e108a09a5e0e1001081010b Mon Sep 17 00:00:00 2001 From: Matteo Felici Date: Thu, 28 May 2020 20:56:05 +0200 Subject: [PATCH 1/4] CLN: GH29547 change string formatting with f-strings --- pandas/tests/io/parser/test_header.py | 2 +- pandas/tests/io/test_html.py | 6 +++--- pandas/tests/io/test_sql.py | 7 +++++-- pandas/tests/reductions/test_reductions.py | 5 ++--- pandas/tests/reshape/test_melt.py | 13 +++++++------ pandas/tests/scalar/timedelta/test_timedelta.py | 16 ++++++++-------- 6 files changed, 26 insertions(+), 23 deletions(-) diff --git a/pandas/tests/io/parser/test_header.py b/pandas/tests/io/parser/test_header.py index 7dc106ef0c186..66a83d3f97e24 100644 --- a/pandas/tests/io/parser/test_header.py +++ b/pandas/tests/io/parser/test_header.py @@ -533,7 +533,7 @@ def test_multi_index_unnamed(all_parsers, index_col, columns): for i, col in enumerate(columns): if not col: # Unnamed. - col = template.format(i=i if index_col is None else i + 1) + col = f"Unnamed: {i if index_col is None else i + 1}_level_0" exp_columns.append(col) diff --git a/pandas/tests/io/test_html.py b/pandas/tests/io/test_html.py index 2c93dbb5b6b83..c6171b6bf74d8 100644 --- a/pandas/tests/io/test_html.py +++ b/pandas/tests/io/test_html.py @@ -507,7 +507,7 @@ def test_tfoot_read(self): Make sure that read_html reads tfoot, containing td or th. Ignores empty tfoot """ - data_template = """ + data_template = lambda footer: f"""
@@ -531,8 +531,8 @@ def test_tfoot_read(self): data=[["bodyA", "bodyB"], ["footA", "footB"]], columns=["A", "B"] ) - data1 = data_template.format(footer="") - data2 = data_template.format(footer="") + data1 = data_template(footer="") + data2 = data_template(footer="") result1 = self.read_html(data1)[0] result2 = self.read_html(data2)[0] diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index 382d4e611c837..97762a012b987 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -1892,7 +1892,8 @@ class _TestMySQLAlchemy: def connect(cls): url = "mysql+{driver}://root@localhost/pandas_nosetest" return sqlalchemy.create_engine( - url.format(driver=cls.driver), connect_args=cls.connect_args + f"mysql+{cls.driver}://root@localhost/pandas_nosetest", + connect_args=cls.connect_args ) @classmethod @@ -1960,7 +1961,9 @@ class _TestPostgreSQLAlchemy: @classmethod def connect(cls): url = "postgresql+{driver}://postgres@localhost/pandas_nosetest" - return sqlalchemy.create_engine(url.format(driver=cls.driver)) + return sqlalchemy.create_engine( + f"postgresql+{cls.driver}://postgres@localhost/pandas_nosetest" + ) @classmethod def setup_driver(cls): diff --git a/pandas/tests/reductions/test_reductions.py b/pandas/tests/reductions/test_reductions.py index f6e0d2f0c1751..a112bc80b60b0 100644 --- a/pandas/tests/reductions/test_reductions.py +++ b/pandas/tests/reductions/test_reductions.py @@ -349,11 +349,10 @@ def test_invalid_td64_reductions(self, opname): msg = "|".join( [ - "reduction operation '{op}' not allowed for this dtype", - r"cannot perform {op} with type timedelta64\[ns\]", + f"reduction operation '{opname}' not allowed for this dtype", + rf"cannot perform {opname} with type timedelta64\[ns\]", ] ) - msg = msg.format(op=opname) with pytest.raises(TypeError, match=msg): getattr(td, opname)() diff --git a/pandas/tests/reshape/test_melt.py b/pandas/tests/reshape/test_melt.py index 000a6354277ab..c121144dfeaed 100644 --- a/pandas/tests/reshape/test_melt.py +++ b/pandas/tests/reshape/test_melt.py @@ -313,31 +313,32 @@ def test_melt_missing_columns_raises(self): df = pd.DataFrame(np.random.randn(5, 4), columns=list("abcd")) # Try to melt with missing `value_vars` column name - msg = "The following '{Var}' are not present in the DataFrame: {Col}" + msg = lambda Var, Col: f"The following '{Var}' are not present in " \ + f"the DataFrame: {Col}" with pytest.raises( - KeyError, match=msg.format(Var="value_vars", Col="\\['C'\\]") + KeyError, match=msg(Var="value_vars", Col="\\['C'\\]") ): df.melt(["a", "b"], ["C", "d"]) # Try to melt with missing `id_vars` column name - with pytest.raises(KeyError, match=msg.format(Var="id_vars", Col="\\['A'\\]")): + with pytest.raises(KeyError, match=msg(Var="id_vars", Col="\\['A'\\]")): df.melt(["A", "b"], ["c", "d"]) # Multiple missing with pytest.raises( KeyError, - match=msg.format(Var="id_vars", Col="\\['not_here', 'or_there'\\]"), + match=msg(Var="id_vars", Col="\\['not_here', 'or_there'\\]"), ): df.melt(["a", "b", "not_here", "or_there"], ["c", "d"]) # Multiindex melt fails if column is missing from multilevel melt multi = df.copy() multi.columns = [list("ABCD"), list("abcd")] - with pytest.raises(KeyError, match=msg.format(Var="id_vars", Col="\\['E'\\]")): + with pytest.raises(KeyError, match=msg(Var="id_vars", Col="\\['E'\\]")): multi.melt([("E", "a")], [("B", "b")]) # Multiindex fails if column is missing from single level melt with pytest.raises( - KeyError, match=msg.format(Var="value_vars", Col="\\['F'\\]") + KeyError, match=msg(Var="value_vars", Col="\\['F'\\]") ): multi.melt(["A"], ["F"], col_level=0) diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index 38e77321418d1..4c30f4b18ee05 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -83,12 +83,12 @@ def check(value): assert rng.microseconds == 0 assert rng.nanoseconds == 0 - msg = "'Timedelta' object has no attribute '{}'" - with pytest.raises(AttributeError, match=msg.format("hours")): + msg = lambda x: f"'Timedelta' object has no attribute '{x}'" + with pytest.raises(AttributeError, match=msg("hours")): rng.hours - with pytest.raises(AttributeError, match=msg.format("minutes")): + with pytest.raises(AttributeError, match=msg("minutes")): rng.minutes - with pytest.raises(AttributeError, match=msg.format("milliseconds")): + with pytest.raises(AttributeError, match=msg("milliseconds")): rng.milliseconds # GH 10050 @@ -109,12 +109,12 @@ def check(value): assert rng.seconds == 10 * 3600 + 11 * 60 + 12 assert rng.microseconds == 100 * 1000 + 123 assert rng.nanoseconds == 456 - msg = "'Timedelta' object has no attribute '{}'" - with pytest.raises(AttributeError, match=msg.format("hours")): + msg = lambda x: f"'Timedelta' object has no attribute '{x}'" + with pytest.raises(AttributeError, match=msg("hours")): rng.hours - with pytest.raises(AttributeError, match=msg.format("minutes")): + with pytest.raises(AttributeError, match=msg("minutes")): rng.minutes - with pytest.raises(AttributeError, match=msg.format("milliseconds")): + with pytest.raises(AttributeError, match=msg("milliseconds")): rng.milliseconds # components From d070823a46048f458511199128e263c072bc81f6 Mon Sep 17 00:00:00 2001 From: Matteo Felici Date: Thu, 28 May 2020 22:24:52 +0200 Subject: [PATCH 2/4] CLN: GH29547 change string formatting with f-strings - auto-reformat with black --- pandas/tests/io/test_html.py | 4 +++- pandas/tests/io/test_sql.py | 2 +- pandas/tests/reshape/test_melt.py | 17 +++++++---------- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/pandas/tests/io/test_html.py b/pandas/tests/io/test_html.py index c6171b6bf74d8..7f410174aa22f 100644 --- a/pandas/tests/io/test_html.py +++ b/pandas/tests/io/test_html.py @@ -507,7 +507,8 @@ def test_tfoot_read(self): Make sure that read_html reads tfoot, containing td or th. Ignores empty tfoot """ - data_template = lambda footer: f"""
A
footAfootB
footAfootB
+ data_template = ( + lambda footer: f"""
@@ -524,6 +525,7 @@ def test_tfoot_read(self): {footer}
A
""" + ) expected1 = DataFrame(data=[["bodyA", "bodyB"]], columns=["A", "B"]) diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index 97762a012b987..75dba0e672458 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -1893,7 +1893,7 @@ def connect(cls): url = "mysql+{driver}://root@localhost/pandas_nosetest" return sqlalchemy.create_engine( f"mysql+{cls.driver}://root@localhost/pandas_nosetest", - connect_args=cls.connect_args + connect_args=cls.connect_args, ) @classmethod diff --git a/pandas/tests/reshape/test_melt.py b/pandas/tests/reshape/test_melt.py index c121144dfeaed..84ed0f6db6c15 100644 --- a/pandas/tests/reshape/test_melt.py +++ b/pandas/tests/reshape/test_melt.py @@ -313,11 +313,11 @@ def test_melt_missing_columns_raises(self): df = pd.DataFrame(np.random.randn(5, 4), columns=list("abcd")) # Try to melt with missing `value_vars` column name - msg = lambda Var, Col: f"The following '{Var}' are not present in " \ - f"the DataFrame: {Col}" - with pytest.raises( - KeyError, match=msg(Var="value_vars", Col="\\['C'\\]") - ): + msg = ( + lambda Var, Col: f"The following '{Var}' are not present in " + f"the DataFrame: {Col}" + ) + with pytest.raises(KeyError, match=msg(Var="value_vars", Col="\\['C'\\]")): df.melt(["a", "b"], ["C", "d"]) # Try to melt with missing `id_vars` column name @@ -326,8 +326,7 @@ def test_melt_missing_columns_raises(self): # Multiple missing with pytest.raises( - KeyError, - match=msg(Var="id_vars", Col="\\['not_here', 'or_there'\\]"), + KeyError, match=msg(Var="id_vars", Col="\\['not_here', 'or_there'\\]"), ): df.melt(["a", "b", "not_here", "or_there"], ["c", "d"]) @@ -337,9 +336,7 @@ def test_melt_missing_columns_raises(self): with pytest.raises(KeyError, match=msg(Var="id_vars", Col="\\['E'\\]")): multi.melt([("E", "a")], [("B", "b")]) # Multiindex fails if column is missing from single level melt - with pytest.raises( - KeyError, match=msg(Var="value_vars", Col="\\['F'\\]") - ): + with pytest.raises(KeyError, match=msg(Var="value_vars", Col="\\['F'\\]")): multi.melt(["A"], ["F"], col_level=0) def test_melt_mixed_int_str_id_vars(self): From 3e6d83617d933367d19d23654978fe4ccf4b288b Mon Sep 17 00:00:00 2001 From: Matteo Felici Date: Thu, 28 May 2020 22:29:53 +0200 Subject: [PATCH 3/4] CLN: GH29547 change string formatting with f-strings - correction from flake8 --- pandas/tests/io/parser/test_header.py | 1 - pandas/tests/io/test_sql.py | 2 -- 2 files changed, 3 deletions(-) diff --git a/pandas/tests/io/parser/test_header.py b/pandas/tests/io/parser/test_header.py index 66a83d3f97e24..4cd110136d7b0 100644 --- a/pandas/tests/io/parser/test_header.py +++ b/pandas/tests/io/parser/test_header.py @@ -528,7 +528,6 @@ def test_multi_index_unnamed(all_parsers, index_col, columns): parser.read_csv(StringIO(data), header=header, index_col=index_col) else: result = parser.read_csv(StringIO(data), header=header, index_col=index_col) - template = "Unnamed: {i}_level_0" exp_columns = [] for i, col in enumerate(columns): diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index 75dba0e672458..622c25b0740f2 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -1890,7 +1890,6 @@ class _TestMySQLAlchemy: @classmethod def connect(cls): - url = "mysql+{driver}://root@localhost/pandas_nosetest" return sqlalchemy.create_engine( f"mysql+{cls.driver}://root@localhost/pandas_nosetest", connect_args=cls.connect_args, @@ -1960,7 +1959,6 @@ class _TestPostgreSQLAlchemy: @classmethod def connect(cls): - url = "postgresql+{driver}://postgres@localhost/pandas_nosetest" return sqlalchemy.create_engine( f"postgresql+{cls.driver}://postgres@localhost/pandas_nosetest" ) From cc2e1c1de3607f3663bfc564514dacc36242bc2c Mon Sep 17 00:00:00 2001 From: Matteo Felici Date: Wed, 17 Jun 2020 16:59:30 +0200 Subject: [PATCH 4/4] CLN: GH29547 change string formatting with f-strings - revert useless changes --- pandas/tests/io/test_html.py | 8 +++----- pandas/tests/reshape/test_melt.py | 20 ++++++++++--------- .../tests/scalar/timedelta/test_timedelta.py | 16 +++++++-------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/pandas/tests/io/test_html.py b/pandas/tests/io/test_html.py index 7f410174aa22f..2c93dbb5b6b83 100644 --- a/pandas/tests/io/test_html.py +++ b/pandas/tests/io/test_html.py @@ -507,8 +507,7 @@ def test_tfoot_read(self): Make sure that read_html reads tfoot, containing td or th. Ignores empty tfoot """ - data_template = ( - lambda footer: f""" + data_template = """
@@ -525,7 +524,6 @@ def test_tfoot_read(self): {footer}
A
""" - ) expected1 = DataFrame(data=[["bodyA", "bodyB"]], columns=["A", "B"]) @@ -533,8 +531,8 @@ def test_tfoot_read(self): data=[["bodyA", "bodyB"], ["footA", "footB"]], columns=["A", "B"] ) - data1 = data_template(footer="") - data2 = data_template(footer="footAfootB") + data1 = data_template.format(footer="") + data2 = data_template.format(footer="footAfootB") result1 = self.read_html(data1)[0] result2 = self.read_html(data2)[0] diff --git a/pandas/tests/reshape/test_melt.py b/pandas/tests/reshape/test_melt.py index 84ed0f6db6c15..000a6354277ab 100644 --- a/pandas/tests/reshape/test_melt.py +++ b/pandas/tests/reshape/test_melt.py @@ -313,30 +313,32 @@ def test_melt_missing_columns_raises(self): df = pd.DataFrame(np.random.randn(5, 4), columns=list("abcd")) # Try to melt with missing `value_vars` column name - msg = ( - lambda Var, Col: f"The following '{Var}' are not present in " - f"the DataFrame: {Col}" - ) - with pytest.raises(KeyError, match=msg(Var="value_vars", Col="\\['C'\\]")): + msg = "The following '{Var}' are not present in the DataFrame: {Col}" + with pytest.raises( + KeyError, match=msg.format(Var="value_vars", Col="\\['C'\\]") + ): df.melt(["a", "b"], ["C", "d"]) # Try to melt with missing `id_vars` column name - with pytest.raises(KeyError, match=msg(Var="id_vars", Col="\\['A'\\]")): + with pytest.raises(KeyError, match=msg.format(Var="id_vars", Col="\\['A'\\]")): df.melt(["A", "b"], ["c", "d"]) # Multiple missing with pytest.raises( - KeyError, match=msg(Var="id_vars", Col="\\['not_here', 'or_there'\\]"), + KeyError, + match=msg.format(Var="id_vars", Col="\\['not_here', 'or_there'\\]"), ): df.melt(["a", "b", "not_here", "or_there"], ["c", "d"]) # Multiindex melt fails if column is missing from multilevel melt multi = df.copy() multi.columns = [list("ABCD"), list("abcd")] - with pytest.raises(KeyError, match=msg(Var="id_vars", Col="\\['E'\\]")): + with pytest.raises(KeyError, match=msg.format(Var="id_vars", Col="\\['E'\\]")): multi.melt([("E", "a")], [("B", "b")]) # Multiindex fails if column is missing from single level melt - with pytest.raises(KeyError, match=msg(Var="value_vars", Col="\\['F'\\]")): + with pytest.raises( + KeyError, match=msg.format(Var="value_vars", Col="\\['F'\\]") + ): multi.melt(["A"], ["F"], col_level=0) def test_melt_mixed_int_str_id_vars(self): diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index 4c30f4b18ee05..38e77321418d1 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -83,12 +83,12 @@ def check(value): assert rng.microseconds == 0 assert rng.nanoseconds == 0 - msg = lambda x: f"'Timedelta' object has no attribute '{x}'" - with pytest.raises(AttributeError, match=msg("hours")): + msg = "'Timedelta' object has no attribute '{}'" + with pytest.raises(AttributeError, match=msg.format("hours")): rng.hours - with pytest.raises(AttributeError, match=msg("minutes")): + with pytest.raises(AttributeError, match=msg.format("minutes")): rng.minutes - with pytest.raises(AttributeError, match=msg("milliseconds")): + with pytest.raises(AttributeError, match=msg.format("milliseconds")): rng.milliseconds # GH 10050 @@ -109,12 +109,12 @@ def check(value): assert rng.seconds == 10 * 3600 + 11 * 60 + 12 assert rng.microseconds == 100 * 1000 + 123 assert rng.nanoseconds == 456 - msg = lambda x: f"'Timedelta' object has no attribute '{x}'" - with pytest.raises(AttributeError, match=msg("hours")): + msg = "'Timedelta' object has no attribute '{}'" + with pytest.raises(AttributeError, match=msg.format("hours")): rng.hours - with pytest.raises(AttributeError, match=msg("minutes")): + with pytest.raises(AttributeError, match=msg.format("minutes")): rng.minutes - with pytest.raises(AttributeError, match=msg("milliseconds")): + with pytest.raises(AttributeError, match=msg.format("milliseconds")): rng.milliseconds # components