From 405516b9f177a0d4d4832ff57f710c8908e9e786 Mon Sep 17 00:00:00 2001 From: Sebastian Bank Date: Mon, 13 May 2024 20:28:50 +0200 Subject: [PATCH] refactor `test_deprecate_positional_args()` --- tests/test_tools.py | 46 +++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/tests/test_tools.py b/tests/test_tools.py index 4d9108f5ff..49047931e4 100644 --- a/tests/test_tools.py +++ b/tests/test_tools.py @@ -35,24 +35,34 @@ def test_mkdirs(tmp_path): (False, 'spam/eggs')] -@pytest.mark.parametrize('category, match', - [(FutureWarning, r" third='third' "), - (DeprecationWarning, r" third='third' "), - (PendingDeprecationWarning, r" third='third' "), - (None, None)]) -def test_deprecate_positional_args(category, match): +@pytest.mark.parametrize('category', [FutureWarning, + DeprecationWarning, + PendingDeprecationWarning]) +def test_deprecate_positional_args(category): + result = object() + @_tools.deprecate_positional_args(supported_number=2, category=category) - def func(first, second, third=None, **kwargs): - pass + def func(first, second, third=None, **_): + return result + + # supported call + with warnings.catch_warnings(): + warnings.simplefilter('error') # should fail if warnings are emitted + assert func('first', 'second', third='third', extra='extra') is result + + # deprecated call + with pytest.warns(category, match=r" third='third' "): + assert func('first', 'second', 'third', extra='extra') is result + + +def test_deprecate_positional_args_category_none_should_disable(): + result = object() + + @_tools.deprecate_positional_args(supported_number=2, category=None) + def func(first, second, third=None, **_): + return result with warnings.catch_warnings(): - warnings.simplefilter('error') - func('first', 'second', third='third', extra='extra') - - if category is not None: - with pytest.warns(category, match=match): - func('first', 'second', 'third', extra='extra') - else: - with warnings.catch_warnings(): - warnings.simplefilter('error') - func('first', 'second', 'third', extra='extra') + warnings.simplefilter('error') # should fail if warnings are emitted + assert func('first', 'second', third='third', extra='extra') is result + assert func('first', 'second', 'third', extra='extra') is result