| 1 |
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html |
| 2 |
# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE |
| 3 |
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt |
| 4 |
|
| 5 |
import pytest |
| 6 |
from _pytest.recwarn import WarningsRecorder |
| 7 |
|
| 8 |
from astroid.decorators import deprecate_default_argument_values |
| 9 |
|
| 10 |
|
| 11 |
class SomeClass: |
| 12 |
@deprecate_default_argument_values(name="str") |
| 13 |
def __init__(self, name=None, lineno=None): ... |
| 14 |
|
| 15 |
@deprecate_default_argument_values("3.2", name="str", var="int") |
| 16 |
def func(self, name=None, var=None, type_annotation=None): ... |
| 17 |
|
| 18 |
|
| 19 |
class TestDeprecationDecorators: |
| 20 |
@staticmethod |
| 21 |
def test_deprecated_default_argument_values_one_arg() -> None: |
| 22 |
with pytest.warns(DeprecationWarning) as records: |
| 23 |
# No argument passed for 'name' |
| 24 |
SomeClass() |
| 25 |
assert len(records) == 1 |
| 26 |
assert "name" in records[0].message.args[0] |
| 27 |
assert "'SomeClass.__init__'" in records[0].message.args[0] |
| 28 |
|
| 29 |
with pytest.warns(DeprecationWarning) as records: |
| 30 |
# 'None' passed as argument for 'name' |
| 31 |
SomeClass(None) |
| 32 |
assert len(records) == 1 |
| 33 |
assert "name" in records[0].message.args[0] |
| 34 |
|
| 35 |
with pytest.warns(DeprecationWarning) as records: |
| 36 |
# 'None' passed as keyword argument for 'name' |
| 37 |
SomeClass(name=None) |
| 38 |
assert len(records) == 1 |
| 39 |
assert "name" in records[0].message.args[0] |
| 40 |
|
| 41 |
with pytest.warns(DeprecationWarning) as records: |
| 42 |
# No value passed for 'name' |
| 43 |
SomeClass(lineno=42) |
| 44 |
assert len(records) == 1 |
| 45 |
assert "name" in records[0].message.args[0] |
| 46 |
|
| 47 |
@staticmethod |
| 48 |
def test_deprecated_default_argument_values_two_args() -> None: |
| 49 |
instance = SomeClass(name="") |
| 50 |
|
| 51 |
# No value of 'None' passed for both arguments |
| 52 |
with pytest.warns(DeprecationWarning) as records: |
| 53 |
instance.func() |
| 54 |
assert len(records) == 2 |
| 55 |
assert "'SomeClass.func'" in records[0].message.args[0] |
| 56 |
assert "astroid 3.2" in records[0].message.args[0] |
| 57 |
|
| 58 |
with pytest.warns(DeprecationWarning) as records: |
| 59 |
instance.func(None) |
| 60 |
assert len(records) == 2 |
| 61 |
|
| 62 |
with pytest.warns(DeprecationWarning) as records: |
| 63 |
instance.func(name=None) |
| 64 |
assert len(records) == 2 |
| 65 |
|
| 66 |
with pytest.warns(DeprecationWarning) as records: |
| 67 |
instance.func(var=None) |
| 68 |
assert len(records) == 2 |
| 69 |
|
| 70 |
with pytest.warns(DeprecationWarning) as records: |
| 71 |
instance.func(name=None, var=None) |
| 72 |
assert len(records) == 2 |
| 73 |
|
| 74 |
with pytest.warns(DeprecationWarning) as records: |
| 75 |
instance.func(type_annotation="") |
| 76 |
assert len(records) == 2 |
| 77 |
|
| 78 |
# No value of 'None' for one argument |
| 79 |
with pytest.warns(DeprecationWarning) as records: |
| 80 |
instance.func(42) |
| 81 |
assert len(records) == 1 |
| 82 |
assert "var" in records[0].message.args[0] |
| 83 |
|
| 84 |
with pytest.warns(DeprecationWarning) as records: |
| 85 |
instance.func(name="") |
| 86 |
assert len(records) == 1 |
| 87 |
assert "var" in records[0].message.args[0] |
| 88 |
|
| 89 |
with pytest.warns(DeprecationWarning) as records: |
| 90 |
instance.func(var=42) |
| 91 |
assert len(records) == 1 |
| 92 |
assert "name" in records[0].message.args[0] |
| 93 |
|
| 94 |
@staticmethod |
| 95 |
def test_deprecated_default_argument_values_ok(recwarn: WarningsRecorder) -> None: |
| 96 |
"""No DeprecationWarning should be emitted |
| 97 |
if all arguments are passed with not None values. |
| 98 |
""" |
| 99 |
instance = SomeClass(name="some_name") |
| 100 |
instance.func(name="", var=42) |
| 101 |
assert len(recwarn) == 0 |