summaryrefslogtreecommitdiff
path: root/examples/deprecation_checker.py
blob: 228b7bdadc0fb54a5e1eb8547f287a82cb6dde70 (plain)
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
"""Example checker detecting deprecated functions/methods. Following example searches for usages of
deprecated function `deprecated_function` and deprecated method `MyClass.deprecated_method`
from module mymodule:

.. code-block:: console
    $ cat mymodule.py
    def deprecated_function():
        pass

    def myfunction(arg0, arg1, deprecated_arg1=None, arg2='foo', arg3='bar', deprecated_arg2='spam'):
        pass

    class MyClass:
        def deprecated_method(self):
            pass

        def mymethod(self, arg0, arg1, deprecated1=None, arg2='foo', deprecated2='bar', arg3='spam'):
            pass

    $ cat mymain.py
    from mymodule import deprecated_function, myfunction, MyClass

    deprecated_function()
    myfunction(0, 1, 'deprecated_arg1', deprecated_arg2=None)
    MyClass().deprecated_method()
    MyClass().mymethod(0, 1, deprecated1=None, deprecated2=None)

    $ pylint --load-plugins=deprecation_checker mymain.py
    ************* Module mymain
    mymain.py:3:0: W1505: Using deprecated method deprecated_function() (deprecated-method)
    mymain.py:4:0: W1511: Using deprecated argument deprecated_arg1 of method myfunction() (deprecated-argument)
    mymain.py:4:0: W1511: Using deprecated argument deprecated_arg2 of method myfunction() (deprecated-argument)
    mymain.py:5:0: W1505: Using deprecated method deprecated_method() (deprecated-method)
    mymain.py:6:0: W1511: Using deprecated argument deprecated1 of method mymethod() (deprecated-argument)
    mymain.py:6:0: W1511: Using deprecated argument deprecated2 of method mymethod() (deprecated-argument)

    ------------------------------------------------------------------
    Your code has been rated at 2.00/10 (previous run: 2.00/10, +0.00)
"""

from __future__ import annotations

from typing import TYPE_CHECKING

from pylint.checkers import BaseChecker, DeprecatedMixin

if TYPE_CHECKING:
    from pylint.lint import PyLinter


class DeprecationChecker(DeprecatedMixin, BaseChecker):
    """Class implementing deprecation checker."""

    # DeprecatedMixin class is Mixin class implementing logic for searching deprecated methods and functions.
    # The list of deprecated methods/functions is defined by the implementing class via
    # deprecated_methods callback. DeprecatedMixin class is overriding attributes of BaseChecker hence must
    # be specified *before* BaseChecker in list of base classes.

    # The name defines a custom section of the config for this checker.
    name = "deprecated"

    # Register messages emitted by the checker.
    msgs = {
        **DeprecatedMixin.DEPRECATED_METHOD_MESSAGE,
        **DeprecatedMixin.DEPRECATED_ARGUMENT_MESSAGE,
    }

    def deprecated_methods(self) -> set[str]:
        """Callback method called by DeprecatedMixin for every method/function found in the code.

        Returns:
            collections.abc.Container of deprecated function/method names.
        """
        return {"mymodule.deprecated_function", "mymodule.MyClass.deprecated_method"}

    def deprecated_arguments(self, method: str) -> tuple[tuple[int | None, str], ...]:
        """Callback returning the deprecated arguments of method/function.

        Returns:
            collections.abc.Iterable in form:
                ((POSITION1, PARAM1), (POSITION2: PARAM2) ...)
            where
                * POSITIONX - position of deprecated argument PARAMX in function definition.
                  If argument is keyword-only, POSITIONX should be None.
                * PARAMX - name of the deprecated argument.
        """
        if method == "mymodule.myfunction":
            # myfunction() has two deprecated arguments:
            # * deprecated_arg1 defined at 2nd position and
            # * deprecated_arg2 defined at 5th position.
            return ((2, "deprecated_arg1"), (5, "deprecated_arg2"))
        if method == "mymodule.MyClass.mymethod":
            # mymethod() has two deprecated arguments:
            # * deprecated1 defined at 2nd position and
            # * deprecated2 defined at 4th position.
            return ((2, "deprecated1"), (4, "deprecated2"))
        return ()


def register(linter: PyLinter) -> None:
    linter.register_checker(DeprecationChecker(linter))