Skip to content

Commit

Permalink
Introduced deprecated attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
matusvalo authored and Pierre-Sassoulas committed Mar 6, 2021
1 parent d7cc8be commit 6721cd1
Show file tree
Hide file tree
Showing 5 changed files with 393 additions and 49 deletions.
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Release date: TBA
..
Put new features here

* Introduce logic for checking deprecated attributes in DeprecationMixin.


What's New in Pylint 2.7.3?
===========================
Expand Down
1 change: 1 addition & 0 deletions doc/whatsnew/2.8.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Summary -- Release highlights
New checkers
============

* Add ``deprecated-argument`` check for deprecated arguments.

Other Changes
=============
62 changes: 39 additions & 23 deletions examples/deprecation_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,39 @@
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, MyClass
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: W1505: Using deprecated method deprecated_method() (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 3.33/10 (previous run: 3.33/10, +0.00)
Your code has been rated at 2.00/10 (previous run: 2.00/10, +0.00)
"""

import astroid

from pylint.checkers import BaseChecker, DeprecatedMixin, utils
from pylint.checkers import BaseChecker, DeprecatedMixin
from pylint.interfaces import IAstroidChecker


Expand All @@ -45,24 +56,6 @@ class DeprecationChecker(DeprecatedMixin, BaseChecker):
# The name defines a custom section of the config for this checker.
name = "deprecated"

@utils.check_messages(
"deprecated-method",
)
def visit_call(self, node):
"""Called when a :class:`.astroid.node_classes.Call` node is visited.
See :mod:`astroid` for the description of available nodes.
:param node: The node to check.
:type node: astroid.node_classes.Call
"""
try:
for inferred in node.func.infer():
# Calling entry point for deprecation check logic.
self.check_deprecated_method(node, inferred)
except astroid.InferenceError:
return

def deprecated_methods(self):
"""Callback method called by DeprecatedMixin for every method/function found in the code.
Expand All @@ -71,6 +64,29 @@ def deprecated_methods(self):
"""
return {"mymodule.deprecated_function", "mymodule.MyClass.deprecated_method"}

def deprecated_arguments(self, method: 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):
"""This required method auto registers the checker.
Expand Down
75 changes: 71 additions & 4 deletions pylint/checkers/deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@

"""Checker mixin for deprecated functionality."""

import abc
from itertools import chain
from typing import Any

import astroid

from pylint.checkers import utils

ACCEPTABLE_NODES = (
astroid.BoundMethod,
astroid.UnboundMethod,
astroid.FunctionDef,
)


class DeprecatedMixin(metaclass=abc.ABCMeta):
class DeprecatedMixin:
"""A mixin implementing logic for checking deprecated symbols.
A class imlementing mixin must define "deprecated-method" Message.
A class implementing mixin must define "deprecated-method" Message.
"""

msgs: Any = {
Expand All @@ -26,15 +28,65 @@ class DeprecatedMixin(metaclass=abc.ABCMeta):
"deprecated-method",
"The method is marked as deprecated and will be removed in the future.",
),
"W1511": (
"Using deprecated argument %s of method %s()",
"deprecated-argument",
"The argument is marked as deprecated and will be removed in the future.",
),
}

@abc.abstractmethod
@utils.check_messages(
"deprecated-method",
"deprecated-argument",
)
def visit_call(self, node):
"""Called when a :class:`.astroid.node_classes.Call` node is visited.
Args:
node (astroid.node_classes.Call): The node to check.
"""
try:
for inferred in node.func.infer():
# Calling entry point for deprecation check logic.
self.check_deprecated_method(node, inferred)
except astroid.InferenceError:
return

def deprecated_methods(self):
"""Callback returning the deprecated methods/functions.
Returns:
collections.abc.Container of deprecated function/method names.
"""
# pylint: disable=no-self-use
return ()

def deprecated_arguments(self, method: str):
"""Callback returning the deprecated arguments of method/function.
Args:
method (str): name of function/method checked for deprecated arguments
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.
E.g. suppose function:
.. code-block:: python
def bar(arg1, arg2, arg3, arg4, arg5='spam')
with deprecated arguments `arg2` and `arg4`. `deprecated_arguments` should return:
.. code-block:: python
((1, 'arg2'), (3, 'arg4'))
"""
# pylint: disable=no-self-use
# pylint: disable=unused-argument
return ()

def check_deprecated_method(self, node, inferred):
"""Executes the checker for the given node. This method should
Expand All @@ -56,3 +108,18 @@ def check_deprecated_method(self, node, inferred):
qname = inferred.qname()
if any(name in self.deprecated_methods() for name in (qname, func_name)):
self.add_message("deprecated-method", node=node, args=(func_name,))
num_of_args = len(node.args)
kwargs = {kw.arg for kw in node.keywords} if node.keywords else {}
for position, arg_name in chain(
self.deprecated_arguments(func_name), self.deprecated_arguments(qname)
):
if arg_name in kwargs:
# function was called with deprecated argument as keyword argument
self.add_message(
"deprecated-argument", node=node, args=(arg_name, func_name)
)
elif position is not None and position < num_of_args:
# function was called with deprecated argument as positional argument
self.add_message(
"deprecated-argument", node=node, args=(arg_name, func_name)
)
Loading

0 comments on commit 6721cd1

Please sign in to comment.