From 86024b5f72b2af09a08cbf120b864f9525193a03 Mon Sep 17 00:00:00 2001 From: shourya5 Date: Sat, 23 Jul 2022 13:38:45 -0500 Subject: [PATCH 01/14] Testing --- pandas/_testing/_warnings.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/_testing/_warnings.py b/pandas/_testing/_warnings.py index 1a8fe71ae3728..9df7b0ae9ff0f 100644 --- a/pandas/_testing/_warnings.py +++ b/pandas/_testing/_warnings.py @@ -9,6 +9,7 @@ from typing import ( Literal, Sequence, + Tuple, Type, cast, ) @@ -17,7 +18,7 @@ @contextmanager def assert_produces_warning( - expected_warning: type[Warning] | bool | None = Warning, + expected_warning: type[Warning] | bool | None = Warning | Tuple[Type[Warning], ...], filter_level: Literal[ "error", "ignore", "always", "default", "module", "once" ] = "always", From b845f98fa776eb80e12392dea96c99f9ebe08a53 Mon Sep 17 00:00:00 2001 From: shourya5 Date: Sat, 23 Jul 2022 15:43:14 -0500 Subject: [PATCH 02/14] TST: added a test for multiple warnings --- pandas/tests/util/test_assert_produces_warning.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pandas/tests/util/test_assert_produces_warning.py b/pandas/tests/util/test_assert_produces_warning.py index e3eb083e1a383..3ceed83651916 100644 --- a/pandas/tests/util/test_assert_produces_warning.py +++ b/pandas/tests/util/test_assert_produces_warning.py @@ -178,6 +178,14 @@ def test_same_category_different_messages_last_match(): warnings.warn("Do not match that either", category) warnings.warn("Match this", category) +def test_match_multiple_warnings(): + category = (FutureWarning, UserWarning) + with tm.assert_produces_warning(category, match=r"^Match this"): + warnings.warn("Match this",FutureWarning ) + warnings.warn("Match this too", UserWarning) + warnings.warn("Do not match this", DeprecationWarning) + + def test_right_category_wrong_match_raises(pair_different_warnings): target_category, other_category = pair_different_warnings From 8609ea4854e0eefabd43533a7a174802d8025b87 Mon Sep 17 00:00:00 2001 From: shourya5 Date: Sat, 23 Jul 2022 13:38:45 -0500 Subject: [PATCH 03/14] TYP: added type annotation Tuple[Type[Warning], ...] in assert_produces_warning function --- pandas/_testing/_warnings.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/_testing/_warnings.py b/pandas/_testing/_warnings.py index 1a8fe71ae3728..9df7b0ae9ff0f 100644 --- a/pandas/_testing/_warnings.py +++ b/pandas/_testing/_warnings.py @@ -9,6 +9,7 @@ from typing import ( Literal, Sequence, + Tuple, Type, cast, ) @@ -17,7 +18,7 @@ @contextmanager def assert_produces_warning( - expected_warning: type[Warning] | bool | None = Warning, + expected_warning: type[Warning] | bool | None = Warning | Tuple[Type[Warning], ...], filter_level: Literal[ "error", "ignore", "always", "default", "module", "once" ] = "always", From d5c515012ff77bd76c0ff62ecf39e8c4f271888a Mon Sep 17 00:00:00 2001 From: shourya5 Date: Sat, 23 Jul 2022 15:43:14 -0500 Subject: [PATCH 04/14] TST: added a test for multiple warnings --- pandas/tests/util/test_assert_produces_warning.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pandas/tests/util/test_assert_produces_warning.py b/pandas/tests/util/test_assert_produces_warning.py index e3eb083e1a383..3ceed83651916 100644 --- a/pandas/tests/util/test_assert_produces_warning.py +++ b/pandas/tests/util/test_assert_produces_warning.py @@ -178,6 +178,14 @@ def test_same_category_different_messages_last_match(): warnings.warn("Do not match that either", category) warnings.warn("Match this", category) +def test_match_multiple_warnings(): + category = (FutureWarning, UserWarning) + with tm.assert_produces_warning(category, match=r"^Match this"): + warnings.warn("Match this",FutureWarning ) + warnings.warn("Match this too", UserWarning) + warnings.warn("Do not match this", DeprecationWarning) + + def test_right_category_wrong_match_raises(pair_different_warnings): target_category, other_category = pair_different_warnings From e7a32f8222973570a9097126c9f1b18042f0a633 Mon Sep 17 00:00:00 2001 From: shourya5 Date: Sun, 31 Jul 2022 10:59:51 -0500 Subject: [PATCH 05/14] TYP: Fixed Annotations --- pandas/_testing/_warnings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_testing/_warnings.py b/pandas/_testing/_warnings.py index 9df7b0ae9ff0f..056f2285da3e4 100644 --- a/pandas/_testing/_warnings.py +++ b/pandas/_testing/_warnings.py @@ -18,7 +18,7 @@ @contextmanager def assert_produces_warning( - expected_warning: type[Warning] | bool | None = Warning | Tuple[Type[Warning], ...], + expected_warning: type[Warning] | Tuple[type[Warning], ...] | bool | None = Warning, filter_level: Literal[ "error", "ignore", "always", "default", "module", "once" ] = "always", From d980f42d64ed8e0e3361d3afd502387f5d10c28f Mon Sep 17 00:00:00 2001 From: shourya5 Date: Sun, 31 Jul 2022 12:11:46 -0500 Subject: [PATCH 06/14] TST: Test for catching multiple warnings --- pandas/tests/util/test_assert_produces_warning.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/util/test_assert_produces_warning.py b/pandas/tests/util/test_assert_produces_warning.py index 3ceed83651916..0092fb3cf7af9 100644 --- a/pandas/tests/util/test_assert_produces_warning.py +++ b/pandas/tests/util/test_assert_produces_warning.py @@ -183,7 +183,6 @@ def test_match_multiple_warnings(): with tm.assert_produces_warning(category, match=r"^Match this"): warnings.warn("Match this",FutureWarning ) warnings.warn("Match this too", UserWarning) - warnings.warn("Do not match this", DeprecationWarning) From 474c84f93e8c3c577b584757e147b3c58ed21a2e Mon Sep 17 00:00:00 2001 From: shourya5 Date: Sun, 31 Jul 2022 12:40:49 -0500 Subject: [PATCH 07/14] TST: Test for catching multiple warnings --- pandas/_testing/_warnings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_testing/_warnings.py b/pandas/_testing/_warnings.py index 056f2285da3e4..d0de93565bc50 100644 --- a/pandas/_testing/_warnings.py +++ b/pandas/_testing/_warnings.py @@ -18,7 +18,7 @@ @contextmanager def assert_produces_warning( - expected_warning: type[Warning] | Tuple[type[Warning], ...] | bool | None = Warning, + expected_warning: type[Warning] | tuple[type[Warning], ...] | bool | None = Warning, filter_level: Literal[ "error", "ignore", "always", "default", "module", "once" ] = "always", From a615ac65707290db2db0b75e4d462cb3fc56b311 Mon Sep 17 00:00:00 2001 From: shourya5 Date: Sun, 31 Jul 2022 12:52:35 -0500 Subject: [PATCH 08/14] TST: Test for catching multiple warnings --- pandas/_testing/_warnings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_testing/_warnings.py b/pandas/_testing/_warnings.py index d0de93565bc50..1c02f513ce30f 100644 --- a/pandas/_testing/_warnings.py +++ b/pandas/_testing/_warnings.py @@ -18,7 +18,7 @@ @contextmanager def assert_produces_warning( - expected_warning: type[Warning] | tuple[type[Warning], ...] | bool | None = Warning, + expected_warning: type[Warning] | bool | Tuple[type[Warning], ...] | None = Warning, filter_level: Literal[ "error", "ignore", "always", "default", "module", "once" ] = "always", From 888befb9668ae6c44ee9538f4831afc2df6a325d Mon Sep 17 00:00:00 2001 From: shourya5 Date: Mon, 1 Aug 2022 10:04:48 -0500 Subject: [PATCH 09/14] TYP : fixed type annotations in _assert_caught_no_extra_warnings --- pandas/_testing/_warnings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_testing/_warnings.py b/pandas/_testing/_warnings.py index 1c02f513ce30f..3b012977ff5b1 100644 --- a/pandas/_testing/_warnings.py +++ b/pandas/_testing/_warnings.py @@ -158,7 +158,7 @@ def _assert_caught_expected_warning( def _assert_caught_no_extra_warnings( *, caught_warnings: Sequence[warnings.WarningMessage], - expected_warning: type[Warning] | bool | None, + expected_warning: type[Warning] | bool | Tuple[type[Warning], ...] | None, ) -> None: """Assert that no extra warnings apart from the expected ones are caught.""" extra_warnings = [] From a546c6ab35d6063ba095c0d1a3ecd46ca617ba1c Mon Sep 17 00:00:00 2001 From: shourya5 Date: Mon, 1 Aug 2022 11:07:26 -0500 Subject: [PATCH 10/14] TYP : updated type annotations --- pandas/_testing/_warnings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_testing/_warnings.py b/pandas/_testing/_warnings.py index 3b012977ff5b1..75143d85aa770 100644 --- a/pandas/_testing/_warnings.py +++ b/pandas/_testing/_warnings.py @@ -196,7 +196,7 @@ def _assert_caught_no_extra_warnings( def _is_unexpected_warning( actual_warning: warnings.WarningMessage, - expected_warning: type[Warning] | bool | None, + expected_warning: type[Warning] | bool | Tuple[type[Warning], ...] | None, ) -> bool: """Check if the actual warning issued is unexpected.""" if actual_warning and not expected_warning: From 6ad3ba9407a825d325d32a0ecd9ccea7db45eced Mon Sep 17 00:00:00 2001 From: shourya5 Date: Mon, 1 Aug 2022 13:38:06 -0500 Subject: [PATCH 11/14] DOC : added issue as inline comment --- pandas/tests/util/test_assert_produces_warning.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/util/test_assert_produces_warning.py b/pandas/tests/util/test_assert_produces_warning.py index 0092fb3cf7af9..f8f6ecbcffa9d 100644 --- a/pandas/tests/util/test_assert_produces_warning.py +++ b/pandas/tests/util/test_assert_produces_warning.py @@ -179,6 +179,7 @@ def test_same_category_different_messages_last_match(): warnings.warn("Match this", category) def test_match_multiple_warnings(): + #https://github.com/pandas-dev/pandas/issues/47829 category = (FutureWarning, UserWarning) with tm.assert_produces_warning(category, match=r"^Match this"): warnings.warn("Match this",FutureWarning ) From 44068418f02691afe1b6f18074f534321d49acfe Mon Sep 17 00:00:00 2001 From: shourya5 Date: Tue, 2 Aug 2022 04:30:34 -0500 Subject: [PATCH 12/14] DOC : added some documentation regarding type annotations --- pandas/_testing/_warnings.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/_testing/_warnings.py b/pandas/_testing/_warnings.py index 75143d85aa770..644b569e24c55 100644 --- a/pandas/_testing/_warnings.py +++ b/pandas/_testing/_warnings.py @@ -28,15 +28,15 @@ def assert_produces_warning( ): """ Context manager for running code expected to either raise a specific - warning, or not raise any warnings. Verifies that the code raises the - expected warning, and that it does not raise any other unexpected + warning, multiple specific warnings , or not raise any warnings. Verifies that the code raises the + expected warnings, and that it does not raise any other unexpected warnings. It is basically a wrapper around ``warnings.catch_warnings``. Parameters ---------- - expected_warning : {Warning, False, None}, default Warning + expected_warning : {Warning, False, Tuple[Warning, ...], None}, default Warning The type of Exception raised. ``exception.Warning`` is the base - class for all warnings. To check that no warning is returned, + class for all warnings. To raise multiple types of exceptions,pass them as a Tuple. To check that no warning is returned, specify ``False`` or ``None``. filter_level : str or None, default "always" Specifies whether warnings are ignored, displayed, or turned From 95cef2b3f78bbe31d23309993f4cd3e883cbcb78 Mon Sep 17 00:00:00 2001 From: shourya5 Date: Tue, 2 Aug 2022 13:57:38 +0000 Subject: [PATCH 13/14] DOC : fixed pre-commit errors --- pandas/_testing/_warnings.py | 20 +++++++++---------- .../util/test_assert_produces_warning.py | 6 +++--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/pandas/_testing/_warnings.py b/pandas/_testing/_warnings.py index 644b569e24c55..07b8c1116d724 100644 --- a/pandas/_testing/_warnings.py +++ b/pandas/_testing/_warnings.py @@ -9,7 +9,6 @@ from typing import ( Literal, Sequence, - Tuple, Type, cast, ) @@ -18,7 +17,7 @@ @contextmanager def assert_produces_warning( - expected_warning: type[Warning] | bool | Tuple[type[Warning], ...] | None = Warning, + expected_warning: type[Warning] | bool | tuple[type[Warning], ...] | None = Warning, filter_level: Literal[ "error", "ignore", "always", "default", "module", "once" ] = "always", @@ -27,16 +26,17 @@ def assert_produces_warning( match: str | None = None, ): """ - Context manager for running code expected to either raise a specific - warning, multiple specific warnings , or not raise any warnings. Verifies that the code raises the - expected warnings, and that it does not raise any other unexpected - warnings. It is basically a wrapper around ``warnings.catch_warnings``. + Context manager for running code expected to either raise a specific warning, + multiple specific warnings , or not raise any warnings. Verifies that the code + raises the expected warnings, and that it does not raise any other unexpected + warnings.It is basically a wrapper around ``warnings.catch_warnings``. Parameters ---------- - expected_warning : {Warning, False, Tuple[Warning, ...], None}, default Warning + expected_warning : {Warning, False, tuple[Warning, ...], None}, default Warning The type of Exception raised. ``exception.Warning`` is the base - class for all warnings. To raise multiple types of exceptions,pass them as a Tuple. To check that no warning is returned, + class for all warnings. To raise multiple types of exceptions, + pass them as a tuple. To check that no warning is returned, specify ``False`` or ``None``. filter_level : str or None, default "always" Specifies whether warnings are ignored, displayed, or turned @@ -158,7 +158,7 @@ def _assert_caught_expected_warning( def _assert_caught_no_extra_warnings( *, caught_warnings: Sequence[warnings.WarningMessage], - expected_warning: type[Warning] | bool | Tuple[type[Warning], ...] | None, + expected_warning: type[Warning] | bool | tuple[type[Warning], ...] | None, ) -> None: """Assert that no extra warnings apart from the expected ones are caught.""" extra_warnings = [] @@ -196,7 +196,7 @@ def _assert_caught_no_extra_warnings( def _is_unexpected_warning( actual_warning: warnings.WarningMessage, - expected_warning: type[Warning] | bool | Tuple[type[Warning], ...] | None, + expected_warning: type[Warning] | bool | tuple[type[Warning], ...] | None, ) -> bool: """Check if the actual warning issued is unexpected.""" if actual_warning and not expected_warning: diff --git a/pandas/tests/util/test_assert_produces_warning.py b/pandas/tests/util/test_assert_produces_warning.py index f8f6ecbcffa9d..70a95bd7cdb48 100644 --- a/pandas/tests/util/test_assert_produces_warning.py +++ b/pandas/tests/util/test_assert_produces_warning.py @@ -178,15 +178,15 @@ def test_same_category_different_messages_last_match(): warnings.warn("Do not match that either", category) warnings.warn("Match this", category) + def test_match_multiple_warnings(): - #https://github.com/pandas-dev/pandas/issues/47829 + # https://github.com/pandas-dev/pandas/issues/47829 category = (FutureWarning, UserWarning) with tm.assert_produces_warning(category, match=r"^Match this"): - warnings.warn("Match this",FutureWarning ) + warnings.warn("Match this", FutureWarning) warnings.warn("Match this too", UserWarning) - def test_right_category_wrong_match_raises(pair_different_warnings): target_category, other_category = pair_different_warnings with pytest.raises(AssertionError, match="Did not see warning.*matching"): From 09262ab70c559c033821139ca56e4333fb0ec5ff Mon Sep 17 00:00:00 2001 From: Marco Edward Gorelli Date: Tue, 2 Aug 2022 16:00:33 +0100 Subject: [PATCH 14/14] fix missing whitespace --- pandas/_testing/_warnings.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/_testing/_warnings.py b/pandas/_testing/_warnings.py index 07b8c1116d724..e9df85eae550a 100644 --- a/pandas/_testing/_warnings.py +++ b/pandas/_testing/_warnings.py @@ -27,9 +27,9 @@ def assert_produces_warning( ): """ Context manager for running code expected to either raise a specific warning, - multiple specific warnings , or not raise any warnings. Verifies that the code - raises the expected warnings, and that it does not raise any other unexpected - warnings.It is basically a wrapper around ``warnings.catch_warnings``. + multiple specific warnings, or not raise any warnings. Verifies that the code + raises the expected warning(s), and that it does not raise any other unexpected + warnings. It is basically a wrapper around ``warnings.catch_warnings``. Parameters ----------