/venv/Lib/site-packages/pylint/checkers/newstyle.py

https://github.com/emreozguruoglu/Researcher · Python · 127 lines · 99 code · 9 blank · 19 comment · 0 complexity · 4d6b70898d93940b25e80e2e75566b09 MD5 · raw file

  1. # Copyright (c) 2006, 2008-2011, 2013-2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
  2. # Copyright (c) 2012-2014 Google, Inc.
  3. # Copyright (c) 2013-2018 Claudiu Popa <pcmanticore@gmail.com>
  4. # Copyright (c) 2014 Michal Nowikowski <godfryd@gmail.com>
  5. # Copyright (c) 2014 Brett Cannon <brett@python.org>
  6. # Copyright (c) 2014 Arun Persaud <arun@nubati.net>
  7. # Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
  8. # Copyright (c) 2016 Alexander Todorov <atodorov@otb.bg>
  9. # Copyright (c) 2016 Jakub Wilk <jwilk@jwilk.net>
  10. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  11. # For details: https://github.com/PyCQA/pylint/blob/master/COPYING
  12. """check for new / old style related problems
  13. """
  14. import astroid
  15. from pylint.checkers import BaseChecker
  16. from pylint.checkers.utils import check_messages, has_known_bases, node_frame_class
  17. from pylint.interfaces import IAstroidChecker
  18. MSGS = {
  19. "E1003": (
  20. "Bad first argument %r given to super()",
  21. "bad-super-call",
  22. "Used when another argument than the current class is given as "
  23. "first argument of the super builtin.",
  24. )
  25. }
  26. class NewStyleConflictChecker(BaseChecker):
  27. """checks for usage of new style capabilities on old style classes and
  28. other new/old styles conflicts problems
  29. * use of property, __slots__, super
  30. * "super" usage
  31. """
  32. __implements__ = (IAstroidChecker,)
  33. # configuration section name
  34. name = "newstyle"
  35. # messages
  36. msgs = MSGS
  37. priority = -2
  38. # configuration options
  39. options = ()
  40. @check_messages("bad-super-call")
  41. def visit_functiondef(self, node):
  42. """check use of super"""
  43. # ignore actual functions or method within a new style class
  44. if not node.is_method():
  45. return
  46. klass = node.parent.frame()
  47. for stmt in node.nodes_of_class(astroid.Call):
  48. if node_frame_class(stmt) != node_frame_class(node):
  49. # Don't look down in other scopes.
  50. continue
  51. expr = stmt.func
  52. if not isinstance(expr, astroid.Attribute):
  53. continue
  54. call = expr.expr
  55. # skip the test if using super
  56. if not (
  57. isinstance(call, astroid.Call)
  58. and isinstance(call.func, astroid.Name)
  59. and call.func.name == "super"
  60. ):
  61. continue
  62. # super should not be used on an old style class
  63. if klass.newstyle or not has_known_bases(klass):
  64. # super first arg should not be the class
  65. if not call.args:
  66. continue
  67. # calling super(type(self), self) can lead to recursion loop
  68. # in derived classes
  69. arg0 = call.args[0]
  70. if (
  71. isinstance(arg0, astroid.Call)
  72. and isinstance(arg0.func, astroid.Name)
  73. and arg0.func.name == "type"
  74. ):
  75. self.add_message("bad-super-call", node=call, args=("type",))
  76. continue
  77. # calling super(self.__class__, self) can lead to recursion loop
  78. # in derived classes
  79. if (
  80. len(call.args) >= 2
  81. and isinstance(call.args[1], astroid.Name)
  82. and call.args[1].name == "self"
  83. and isinstance(arg0, astroid.Attribute)
  84. and arg0.attrname == "__class__"
  85. ):
  86. self.add_message(
  87. "bad-super-call", node=call, args=("self.__class__",)
  88. )
  89. continue
  90. try:
  91. supcls = call.args and next(call.args[0].infer(), None)
  92. except astroid.InferenceError:
  93. continue
  94. if klass is not supcls:
  95. name = None
  96. # if supcls is not Uninferable, then supcls was inferred
  97. # and use its name. Otherwise, try to look
  98. # for call.args[0].name
  99. if supcls:
  100. name = supcls.name
  101. elif call.args and hasattr(call.args[0], "name"):
  102. name = call.args[0].name
  103. if name:
  104. self.add_message("bad-super-call", node=call, args=(name,))
  105. visit_asyncfunctiondef = visit_functiondef
  106. def register(linter):
  107. """required method to auto register this checker """
  108. linter.register_checker(NewStyleConflictChecker(linter))