summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorMatus Valo <matusvalo@gmail.com>2021-02-22 00:26:53 +0100
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2021-03-06 17:43:08 +0100
commit6721cd1cf2da0294124b75d382a042e23ec27d47 (patch)
tree1820e79e37898629f8fce21abc3e6565a7adf298 /examples
parentd7cc8bedd1be8f69abc9e981775498569cae18c2 (diff)
downloadpylint-git-6721cd1cf2da0294124b75d382a042e23ec27d47.tar.gz
Introduced deprecated attributes
Diffstat (limited to 'examples')
-rw-r--r--examples/deprecation_checker.py62
1 files changed, 39 insertions, 23 deletions
diff --git a/examples/deprecation_checker.py b/examples/deprecation_checker.py
index 382f76bb3..03b8e3e36 100644
--- a/examples/deprecation_checker.py
+++ b/examples/deprecation_checker.py
@@ -8,28 +8,39 @@ from module mymodule:
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
@@ -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.
@@ -71,6 +64,29 @@ class DeprecationChecker(DeprecatedMixin, BaseChecker):
"""
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.