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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt
import pytest
from _pytest.recwarn import WarningsRecorder
from astroid.decorators import deprecate_default_argument_values
class SomeClass:
@deprecate_default_argument_values(name="str")
def __init__(self, name=None, lineno=None): ...
@deprecate_default_argument_values("3.2", name="str", var="int")
def func(self, name=None, var=None, type_annotation=None): ...
class TestDeprecationDecorators:
@staticmethod
def test_deprecated_default_argument_values_one_arg() -> None:
with pytest.warns(DeprecationWarning) as records:
# No argument passed for 'name'
SomeClass()
assert len(records) == 1
assert "name" in records[0].message.args[0]
assert "'SomeClass.__init__'" in records[0].message.args[0]
with pytest.warns(DeprecationWarning) as records:
# 'None' passed as argument for 'name'
SomeClass(None)
assert len(records) == 1
assert "name" in records[0].message.args[0]
with pytest.warns(DeprecationWarning) as records:
# 'None' passed as keyword argument for 'name'
SomeClass(name=None)
assert len(records) == 1
assert "name" in records[0].message.args[0]
with pytest.warns(DeprecationWarning) as records:
# No value passed for 'name'
SomeClass(lineno=42)
assert len(records) == 1
assert "name" in records[0].message.args[0]
@staticmethod
def test_deprecated_default_argument_values_two_args() -> None:
instance = SomeClass(name="")
# No value of 'None' passed for both arguments
with pytest.warns(DeprecationWarning) as records:
instance.func()
assert len(records) == 2
assert "'SomeClass.func'" in records[0].message.args[0]
assert "astroid 3.2" in records[0].message.args[0]
with pytest.warns(DeprecationWarning) as records:
instance.func(None)
assert len(records) == 2
with pytest.warns(DeprecationWarning) as records:
instance.func(name=None)
assert len(records) == 2
with pytest.warns(DeprecationWarning) as records:
instance.func(var=None)
assert len(records) == 2
with pytest.warns(DeprecationWarning) as records:
instance.func(name=None, var=None)
assert len(records) == 2
with pytest.warns(DeprecationWarning) as records:
instance.func(type_annotation="")
assert len(records) == 2
# No value of 'None' for one argument
with pytest.warns(DeprecationWarning) as records:
instance.func(42)
assert len(records) == 1
assert "var" in records[0].message.args[0]
with pytest.warns(DeprecationWarning) as records:
instance.func(name="")
assert len(records) == 1
assert "var" in records[0].message.args[0]
with pytest.warns(DeprecationWarning) as records:
instance.func(var=42)
assert len(records) == 1
assert "name" in records[0].message.args[0]
@staticmethod
def test_deprecated_default_argument_values_ok(recwarn: WarningsRecorder) -> None:
"""No DeprecationWarning should be emitted
if all arguments are passed with not None values.
"""
instance = SomeClass(name="some_name")
instance.func(name="", var=42)
assert len(recwarn) == 0
|