1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
# -*- coding: utf-8 -*-
"""
This example shows a function with an unused optional parameter. A warning
message should be emitted if `z` is used (as a positional or keyword parameter).
"""
import sys
import warnings
import pytest
from deprecated.params import deprecated_params
PY38 = sys.version_info[0:2] >= (3, 8)
if PY38:
# Positional-Only Arguments are only available for Python >= 3
# On other version, this code raises a SyntaxError exception.
exec (
"""
@deprecated_params("z")
def pow2(x, y, z=None, /):
return x ** y
"""
)
else:
@deprecated_params("z")
def pow2(x, y, z=None):
return x ** y
@pytest.mark.parametrize(
"args, kwargs, expected",
[
pytest.param((5, 6), {}, [], id="'z' not used: no warnings"),
pytest.param(
(5, 6, 8),
{},
["'z' parameter is deprecated"],
id="'z' used in positional params",
),
pytest.param(
(5, 6),
{"z": 8},
["'z' parameter is deprecated"],
id="'z' used in keyword params",
marks=pytest.mark.skipif(PY38, reason="'z' parameter is positional only"),
),
],
)
def test_pow2(args, kwargs, expected):
with warnings.catch_warnings(record=True) as warns:
warnings.simplefilter("always")
pow2(*args, **kwargs)
actual = [str(warn.message) for warn in warns]
assert actual == expected
|