deprecated.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  2. # For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
  3. """Checker mixin for deprecated functionality."""
  4. from itertools import chain
  5. from typing import Any, Container, Iterable, Tuple, Union
  6. import astroid
  7. from astroid import nodes
  8. from pylint.checkers import utils
  9. from pylint.checkers.base_checker import BaseChecker
  10. from pylint.checkers.utils import get_import_name, infer_all, safe_infer
  11. ACCEPTABLE_NODES = (
  12. astroid.BoundMethod,
  13. astroid.UnboundMethod,
  14. nodes.FunctionDef,
  15. nodes.ClassDef,
  16. )
  17. class DeprecatedMixin(BaseChecker):
  18. """A mixin implementing logic for checking deprecated symbols.
  19. A class implementing mixin must define "deprecated-method" Message.
  20. """
  21. msgs: Any = {
  22. "W1505": (
  23. "Using deprecated method %s()",
  24. "deprecated-method",
  25. "The method is marked as deprecated and will be removed in the future.",
  26. ),
  27. "W1511": (
  28. "Using deprecated argument %s of method %s()",
  29. "deprecated-argument",
  30. "The argument is marked as deprecated and will be removed in the future.",
  31. ),
  32. "W0402": (
  33. "Uses of a deprecated module %r",
  34. "deprecated-module",
  35. "A module marked as deprecated is imported.",
  36. ),
  37. "W1512": (
  38. "Using deprecated class %s of module %s",
  39. "deprecated-class",
  40. "The class is marked as deprecated and will be removed in the future.",
  41. ),
  42. "W1513": (
  43. "Using deprecated decorator %s()",
  44. "deprecated-decorator",
  45. "The decorator is marked as deprecated and will be removed in the future.",
  46. ),
  47. }
  48. @utils.check_messages(
  49. "deprecated-method",
  50. "deprecated-argument",
  51. "deprecated-class",
  52. )
  53. def visit_call(self, node: nodes.Call) -> None:
  54. """Called when a :class:`nodes.Call` node is visited."""
  55. self.check_deprecated_class_in_call(node)
  56. for inferred in infer_all(node.func):
  57. # Calling entry point for deprecation check logic.
  58. self.check_deprecated_method(node, inferred)
  59. @utils.check_messages(
  60. "deprecated-module",
  61. "deprecated-class",
  62. )
  63. def visit_import(self, node: nodes.Import) -> None:
  64. """triggered when an import statement is seen"""
  65. for name in (name for name, _ in node.names):
  66. self.check_deprecated_module(node, name)
  67. if "." in name:
  68. # Checking deprecation for import module with class
  69. mod_name, class_name = name.split(".", 1)
  70. self.check_deprecated_class(node, mod_name, (class_name,))
  71. def deprecated_decorators(self) -> Iterable:
  72. """Callback returning the deprecated decorators.
  73. Returns:
  74. collections.abc.Container of deprecated decorator names.
  75. """
  76. # pylint: disable=no-self-use
  77. return ()
  78. @utils.check_messages("deprecated-decorator")
  79. def visit_decorators(self, node: nodes.Decorators) -> None:
  80. """Triggered when a decorator statement is seen"""
  81. children = list(node.get_children())
  82. if not children:
  83. return
  84. if isinstance(children[0], nodes.Call):
  85. inf = safe_infer(children[0].func)
  86. else:
  87. inf = safe_infer(children[0])
  88. qname = inf.qname() if inf else None
  89. if qname in self.deprecated_decorators():
  90. self.add_message("deprecated-decorator", node=node, args=qname)
  91. @utils.check_messages(
  92. "deprecated-module",
  93. "deprecated-class",
  94. )
  95. def visit_importfrom(self, node: nodes.ImportFrom) -> None:
  96. """triggered when a from statement is seen"""
  97. basename = node.modname
  98. basename = get_import_name(node, basename)
  99. self.check_deprecated_module(node, basename)
  100. class_names = (name for name, _ in node.names)
  101. self.check_deprecated_class(node, basename, class_names)
  102. def deprecated_methods(self) -> Container[str]:
  103. """Callback returning the deprecated methods/functions.
  104. Returns:
  105. collections.abc.Container of deprecated function/method names.
  106. """
  107. # pylint: disable=no-self-use
  108. return ()
  109. def deprecated_arguments(
  110. self, method: str
  111. ) -> Iterable[Tuple[Union[int, None], str]]:
  112. """Callback returning the deprecated arguments of method/function.
  113. Args:
  114. method (str): name of function/method checked for deprecated arguments
  115. Returns:
  116. collections.abc.Iterable in form:
  117. ((POSITION1, PARAM1), (POSITION2: PARAM2) ...)
  118. where
  119. * POSITIONX - position of deprecated argument PARAMX in function definition.
  120. If argument is keyword-only, POSITIONX should be None.
  121. * PARAMX - name of the deprecated argument.
  122. E.g. suppose function:
  123. .. code-block:: python
  124. def bar(arg1, arg2, arg3, arg4, arg5='spam')
  125. with deprecated arguments `arg2` and `arg4`. `deprecated_arguments` should return:
  126. .. code-block:: python
  127. ((1, 'arg2'), (3, 'arg4'))
  128. """
  129. # pylint: disable=no-self-use
  130. # pylint: disable=unused-argument
  131. return ()
  132. def deprecated_modules(self) -> Iterable:
  133. """Callback returning the deprecated modules.
  134. Returns:
  135. collections.abc.Container of deprecated module names.
  136. """
  137. # pylint: disable=no-self-use
  138. return ()
  139. def deprecated_classes(self, module: str) -> Iterable:
  140. """Callback returning the deprecated classes of module.
  141. Args:
  142. module (str): name of module checked for deprecated classes
  143. Returns:
  144. collections.abc.Container of deprecated class names.
  145. """
  146. # pylint: disable=no-self-use
  147. # pylint: disable=unused-argument
  148. return ()
  149. def check_deprecated_module(self, node, mod_path):
  150. """Checks if the module is deprecated"""
  151. for mod_name in self.deprecated_modules():
  152. if mod_path == mod_name or mod_path.startswith(mod_name + "."):
  153. self.add_message("deprecated-module", node=node, args=mod_path)
  154. def check_deprecated_method(self, node, inferred):
  155. """Executes the checker for the given node. This method should
  156. be called from the checker implementing this mixin.
  157. """
  158. # Reject nodes which aren't of interest to us.
  159. if not isinstance(inferred, ACCEPTABLE_NODES):
  160. return
  161. if isinstance(node.func, nodes.Attribute):
  162. func_name = node.func.attrname
  163. elif isinstance(node.func, nodes.Name):
  164. func_name = node.func.name
  165. else:
  166. # Not interested in other nodes.
  167. return
  168. if hasattr(inferred.parent, "qname") and inferred.parent.qname():
  169. # Handling the situation when deprecated function is
  170. # alias to existing function.
  171. qnames = {
  172. inferred.qname(),
  173. f"{inferred.parent.qname()}.{func_name}",
  174. func_name,
  175. }
  176. else:
  177. qnames = {inferred.qname(), func_name}
  178. if any(name in self.deprecated_methods() for name in qnames):
  179. self.add_message("deprecated-method", node=node, args=(func_name,))
  180. return
  181. num_of_args = len(node.args)
  182. kwargs = {kw.arg for kw in node.keywords} if node.keywords else {}
  183. deprecated_arguments = (self.deprecated_arguments(qn) for qn in qnames)
  184. for position, arg_name in chain(*deprecated_arguments):
  185. if arg_name in kwargs:
  186. # function was called with deprecated argument as keyword argument
  187. self.add_message(
  188. "deprecated-argument", node=node, args=(arg_name, func_name)
  189. )
  190. elif position is not None and position < num_of_args:
  191. # function was called with deprecated argument as positional argument
  192. self.add_message(
  193. "deprecated-argument", node=node, args=(arg_name, func_name)
  194. )
  195. def check_deprecated_class(self, node, mod_name, class_names):
  196. """Checks if the class is deprecated"""
  197. for class_name in class_names:
  198. if class_name in self.deprecated_classes(mod_name):
  199. self.add_message(
  200. "deprecated-class", node=node, args=(class_name, mod_name)
  201. )
  202. def check_deprecated_class_in_call(self, node):
  203. """Checks if call the deprecated class"""
  204. if isinstance(node.func, nodes.Attribute) and isinstance(
  205. node.func.expr, nodes.Name
  206. ):
  207. mod_name = node.func.expr.name
  208. class_name = node.func.attrname
  209. self.check_deprecated_class(node, mod_name, (class_name,))