From 00e05a34ce6c570d8dbb6afef5b379b81d9b8283 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Tue, 21 Jan 2020 00:49:13 +0200 Subject: [PATCH 1/4] TST: Replaced try-catch blocks with pytest.raises --- pandas/tests/io/test_sql.py | 11 ++++------- pandas/tests/test_errors.py | 5 ++--- pandas/tests/util/test_assert_almost_equal.py | 7 ++----- pandas/tests/util/test_assert_frame_equal.py | 8 ++------ pandas/tests/util/test_assert_series_equal.py | 8 ++------ 5 files changed, 12 insertions(+), 27 deletions(-) diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index 45b3e839a08d1..aa62ddc731c31 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -226,10 +226,8 @@ def _get_all_tables(self): def _close_conn(self): from pymysql.err import Error - try: + with pytest.raises(Error): self.conn.close() - except Error: - pass class SQLiteMixIn(MixInBase): @@ -538,13 +536,12 @@ class DummyException(Exception): # Make sure when transaction is rolled back, no rows get inserted ins_sql = "INSERT INTO test_trans (A,B) VALUES (1, 'blah')" - try: + + with pytest.raises(DummyException): with self.pandasSQL.run_transaction() as trans: trans.execute(ins_sql) raise DummyException("error") - except DummyException: - # ignore raised exception - pass + res = self.pandasSQL.read_query("SELECT * FROM test_trans") assert len(res) == 0 diff --git a/pandas/tests/test_errors.py b/pandas/tests/test_errors.py index 939ea8a64d94d..d72c00ceb0045 100644 --- a/pandas/tests/test_errors.py +++ b/pandas/tests/test_errors.py @@ -36,10 +36,9 @@ def test_exception_importable(exc): def test_catch_oob(): from pandas import errors - try: + msg = "Out of bounds nanosecond timestamp: 1500-01-01 00:00:00" + with pytest.raises(errors.OutOfBoundsDatetime, match=msg): pd.Timestamp("15000101") - except errors.OutOfBoundsDatetime: - pass class Foo: diff --git a/pandas/tests/util/test_assert_almost_equal.py b/pandas/tests/util/test_assert_almost_equal.py index b8048891e4876..2f806c4474242 100644 --- a/pandas/tests/util/test_assert_almost_equal.py +++ b/pandas/tests/util/test_assert_almost_equal.py @@ -37,12 +37,9 @@ def _assert_not_almost_equal(a, b, **kwargs): kwargs : dict The arguments passed to `tm.assert_almost_equal`. """ - try: + msg = f"{a} and {b} were approximately equal when they shouldn't have been" + with pytest.raises(AssertionError, match=msg): tm.assert_almost_equal(a, b, **kwargs) - msg = f"{a} and {b} were approximately equal when they shouldn't have been" - pytest.fail(msg=msg) - except AssertionError: - pass def _assert_not_almost_equal_both(a, b, **kwargs): diff --git a/pandas/tests/util/test_assert_frame_equal.py b/pandas/tests/util/test_assert_frame_equal.py index 23c845f2b2795..3090343ba2fd9 100644 --- a/pandas/tests/util/test_assert_frame_equal.py +++ b/pandas/tests/util/test_assert_frame_equal.py @@ -46,13 +46,9 @@ def _assert_not_frame_equal(a, b, **kwargs): kwargs : dict The arguments passed to `tm.assert_frame_equal`. """ - try: + msg = "The two DataFrames were equal when they shouldn't have been" + with pytest.raises(AssertionError, match=msg): tm.assert_frame_equal(a, b, **kwargs) - msg = "The two DataFrames were equal when they shouldn't have been" - - pytest.fail(msg=msg) - except AssertionError: - pass def _assert_not_frame_equal_both(a, b, **kwargs): diff --git a/pandas/tests/util/test_assert_series_equal.py b/pandas/tests/util/test_assert_series_equal.py index eaf0824f52927..64aae6a477df9 100644 --- a/pandas/tests/util/test_assert_series_equal.py +++ b/pandas/tests/util/test_assert_series_equal.py @@ -36,13 +36,9 @@ def _assert_not_series_equal(a, b, **kwargs): kwargs : dict The arguments passed to `tm.assert_series_equal`. """ - try: + msg = "The two Series were equal when they shouldn't have been" + with pytest.raises(AssertionError, match=msg): tm.assert_series_equal(a, b, **kwargs) - msg = "The two Series were equal when they shouldn't have been" - - pytest.fail(msg=msg) - except AssertionError: - pass def _assert_not_series_equal_both(a, b, **kwargs): From f6fa8234f918573508adeed600c205930b510d1a Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Thu, 23 Jan 2020 01:34:30 +0200 Subject: [PATCH 2/4] Reverted root cause of CI failure --- pandas/tests/util/test_assert_series_equal.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pandas/tests/util/test_assert_series_equal.py b/pandas/tests/util/test_assert_series_equal.py index 64aae6a477df9..eaf0824f52927 100644 --- a/pandas/tests/util/test_assert_series_equal.py +++ b/pandas/tests/util/test_assert_series_equal.py @@ -36,9 +36,13 @@ def _assert_not_series_equal(a, b, **kwargs): kwargs : dict The arguments passed to `tm.assert_series_equal`. """ - msg = "The two Series were equal when they shouldn't have been" - with pytest.raises(AssertionError, match=msg): + try: tm.assert_series_equal(a, b, **kwargs) + msg = "The two Series were equal when they shouldn't have been" + + pytest.fail(msg=msg) + except AssertionError: + pass def _assert_not_series_equal_both(a, b, **kwargs): From c7d27ab58b67fca99dc8ebfe75b37aff7275fb05 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Thu, 23 Jan 2020 01:37:44 +0200 Subject: [PATCH 3/4] Applied @jorisvandenbossche suggestions REFS: https://github.com/pandas-dev/pandas/pull/31165#discussion_r369432765 https://github.com/pandas-dev/pandas/pull/31165#discussion_r369433351 --- pandas/tests/io/test_sql.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index aa62ddc731c31..45b3e839a08d1 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -226,8 +226,10 @@ def _get_all_tables(self): def _close_conn(self): from pymysql.err import Error - with pytest.raises(Error): + try: self.conn.close() + except Error: + pass class SQLiteMixIn(MixInBase): @@ -536,12 +538,13 @@ class DummyException(Exception): # Make sure when transaction is rolled back, no rows get inserted ins_sql = "INSERT INTO test_trans (A,B) VALUES (1, 'blah')" - - with pytest.raises(DummyException): + try: with self.pandasSQL.run_transaction() as trans: trans.execute(ins_sql) raise DummyException("error") - + except DummyException: + # ignore raised exception + pass res = self.pandasSQL.read_query("SELECT * FROM test_trans") assert len(res) == 0 From 8040c47c4954c7f4640b36d6cf5a02cf75903532 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Thu, 23 Jan 2020 02:07:46 +0200 Subject: [PATCH 4/4] Fixed other CI errors --- pandas/tests/util/test_assert_almost_equal.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pandas/tests/util/test_assert_almost_equal.py b/pandas/tests/util/test_assert_almost_equal.py index 2f806c4474242..b8048891e4876 100644 --- a/pandas/tests/util/test_assert_almost_equal.py +++ b/pandas/tests/util/test_assert_almost_equal.py @@ -37,9 +37,12 @@ def _assert_not_almost_equal(a, b, **kwargs): kwargs : dict The arguments passed to `tm.assert_almost_equal`. """ - msg = f"{a} and {b} were approximately equal when they shouldn't have been" - with pytest.raises(AssertionError, match=msg): + try: tm.assert_almost_equal(a, b, **kwargs) + msg = f"{a} and {b} were approximately equal when they shouldn't have been" + pytest.fail(msg=msg) + except AssertionError: + pass def _assert_not_almost_equal_both(a, b, **kwargs):