From 1ae44621b8df62b5b9c262d8ae3ef3e666ad36f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mochol=C3=AD?= Date: Wed, 21 Sep 2022 19:00:19 +0200 Subject: [PATCH 1/5] Add `lightning_utilities.test.warning.no_warning_call` --- src/lightning_utilities/test/warning.py | 15 +++++++++++++++ tests/unittests/test/test_warnings.py | 25 +++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 src/lightning_utilities/test/warning.py create mode 100644 tests/unittests/test/test_warnings.py diff --git a/src/lightning_utilities/test/warning.py b/src/lightning_utilities/test/warning.py new file mode 100644 index 00000000..c69aa703 --- /dev/null +++ b/src/lightning_utilities/test/warning.py @@ -0,0 +1,15 @@ +import re +import warnings +from contextlib import contextmanager +from typing import Type, Optional, Generator + + +@contextmanager +def no_warning_call(expected_warning: Type[Warning] = Warning, match: Optional[str] = None) -> Generator: + with warnings.catch_warnings(record=True) as record: + yield + + for w in record: + if issubclass(w.category, expected_warning) and (match is None or re.compile(match).search(w.message.args[0])): + msg = "A warning" if expected_warning is None else f"`{expected_warning.__name__}`" + raise AssertionError(f"{msg} was raised: {w.message!r}") diff --git a/tests/unittests/test/test_warnings.py b/tests/unittests/test/test_warnings.py new file mode 100644 index 00000000..3b5954ed --- /dev/null +++ b/tests/unittests/test/test_warnings.py @@ -0,0 +1,25 @@ +import warnings +from re import escape + +import pytest + +from lightning_utilities.test.warning import no_warning_call + + +def test_no_warning_call(): + with no_warning_call(): + ... + + with pytest.raises(AssertionError, match=escape("`Warning` was raised: UserWarning('foo')")): + with no_warning_call(): + warnings.warn("foo") + + with no_warning_call(DeprecationWarning): + warnings.warn("foo") + + class MyDeprecationWarning(DeprecationWarning): + ... + + with pytest.raises(AssertionError, match=escape("`DeprecationWarning` was raised: MyDeprecationWarning('bar')")): + with pytest.warns(DeprecationWarning), no_warning_call(DeprecationWarning): + warnings.warn("bar", category=MyDeprecationWarning) From e0d47a78f309ff9114586f75b322114daef4675f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mochol=C3=AD?= Date: Wed, 21 Sep 2022 19:16:07 +0200 Subject: [PATCH 2/5] Address mypy and pre-commit --- src/lightning_utilities/test/warning.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lightning_utilities/test/warning.py b/src/lightning_utilities/test/warning.py index c69aa703..ed2844bc 100644 --- a/src/lightning_utilities/test/warning.py +++ b/src/lightning_utilities/test/warning.py @@ -1,7 +1,7 @@ import re import warnings from contextlib import contextmanager -from typing import Type, Optional, Generator +from typing import Generator, Optional, Type @contextmanager @@ -10,6 +10,6 @@ def no_warning_call(expected_warning: Type[Warning] = Warning, match: Optional[s yield for w in record: - if issubclass(w.category, expected_warning) and (match is None or re.compile(match).search(w.message.args[0])): + if issubclass(w.category, expected_warning) and (match is None or re.compile(match).search(str(w.message))): msg = "A warning" if expected_warning is None else f"`{expected_warning.__name__}`" raise AssertionError(f"{msg} was raised: {w.message!r}") From 2141e13efe6b1aeb655a043d91a50c125425ac5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mochol=C3=AD?= Date: Wed, 21 Sep 2022 19:16:25 +0200 Subject: [PATCH 3/5] Remove the weird check-manifest --- .github/workflows/check-package.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/check-package.yml b/.github/workflows/check-package.yml index 45c8558b..aa07d920 100644 --- a/.github/workflows/check-package.yml +++ b/.github/workflows/check-package.yml @@ -41,9 +41,6 @@ jobs: - name: Check local package run: | - pip install -q -U check-manifest - # check MANIFEST.in - check-manifest # check package python setup.py check --metadata --strict From 7e1498fcdfb7c2427e94a3633714051a79da7b14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mochol=C3=AD?= Date: Wed, 21 Sep 2022 20:04:56 +0200 Subject: [PATCH 4/5] Apply suggestions from code review --- src/lightning_utilities/test/warning.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lightning_utilities/test/warning.py b/src/lightning_utilities/test/warning.py index ed2844bc..7ece3d51 100644 --- a/src/lightning_utilities/test/warning.py +++ b/src/lightning_utilities/test/warning.py @@ -11,5 +11,4 @@ def no_warning_call(expected_warning: Type[Warning] = Warning, match: Optional[s for w in record: if issubclass(w.category, expected_warning) and (match is None or re.compile(match).search(str(w.message))): - msg = "A warning" if expected_warning is None else f"`{expected_warning.__name__}`" - raise AssertionError(f"{msg} was raised: {w.message!r}") + raise AssertionError(f"{expected_warning.__name__} was raised: {w.message!r}") From 67fc203a1b1938258571772972319bdb0b3b7840 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mochol=C3=AD?= Date: Wed, 21 Sep 2022 20:06:04 +0200 Subject: [PATCH 5/5] Update src/lightning_utilities/test/warning.py --- src/lightning_utilities/test/warning.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lightning_utilities/test/warning.py b/src/lightning_utilities/test/warning.py index 7ece3d51..52fa1047 100644 --- a/src/lightning_utilities/test/warning.py +++ b/src/lightning_utilities/test/warning.py @@ -11,4 +11,4 @@ def no_warning_call(expected_warning: Type[Warning] = Warning, match: Optional[s for w in record: if issubclass(w.category, expected_warning) and (match is None or re.compile(match).search(str(w.message))): - raise AssertionError(f"{expected_warning.__name__} was raised: {w.message!r}") + raise AssertionError(f"`{expected_warning.__name__}` was raised: {w.message!r}")