newstyle.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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-2020 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. # Copyright (c) 2018 Lucas Cimon <lucas.cimon@gmail.com>
  11. # Copyright (c) 2018 Natalie Serebryakova <natalie.serebryakova@Natalies-MacBook-Pro.local>
  12. # Copyright (c) 2018 ssolanki <sushobhitsolanki@gmail.com>
  13. # Copyright (c) 2019, 2021 Pierre Sassoulas <pierre.sassoulas@gmail.com>
  14. # Copyright (c) 2019 Hugo van Kemenade <hugovk@users.noreply.github.com>
  15. # Copyright (c) 2019 Robert Schweizer <robert_schweizer@gmx.de>
  16. # Copyright (c) 2020 hippo91 <guillaume.peillex@gmail.com>
  17. # Copyright (c) 2021 Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
  18. # Copyright (c) 2021 Marc Mueller <30130371+cdce8p@users.noreply.github.com>
  19. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. # For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
  21. """check for new / old style related problems
  22. """
  23. import astroid
  24. from astroid import nodes
  25. from pylint.checkers import BaseChecker
  26. from pylint.checkers.utils import check_messages, has_known_bases, node_frame_class
  27. from pylint.interfaces import IAstroidChecker
  28. MSGS = {
  29. "E1003": (
  30. "Bad first argument %r given to super()",
  31. "bad-super-call",
  32. "Used when another argument than the current class is given as "
  33. "first argument of the super builtin.",
  34. )
  35. }
  36. class NewStyleConflictChecker(BaseChecker):
  37. """checks for usage of new style capabilities on old style classes and
  38. other new/old styles conflicts problems
  39. * use of property, __slots__, super
  40. * "super" usage
  41. """
  42. __implements__ = (IAstroidChecker,)
  43. # configuration section name
  44. name = "newstyle"
  45. # messages
  46. msgs = MSGS
  47. priority = -2
  48. # configuration options
  49. options = ()
  50. @check_messages("bad-super-call")
  51. def visit_functiondef(self, node: nodes.FunctionDef) -> None:
  52. """check use of super"""
  53. # ignore actual functions or method within a new style class
  54. if not node.is_method():
  55. return
  56. klass = node.parent.frame()
  57. for stmt in node.nodes_of_class(nodes.Call):
  58. if node_frame_class(stmt) != node_frame_class(node):
  59. # Don't look down in other scopes.
  60. continue
  61. expr = stmt.func
  62. if not isinstance(expr, nodes.Attribute):
  63. continue
  64. call = expr.expr
  65. # skip the test if using super
  66. if not (
  67. isinstance(call, nodes.Call)
  68. and isinstance(call.func, nodes.Name)
  69. and call.func.name == "super"
  70. ):
  71. continue
  72. # super should not be used on an old style class
  73. if klass.newstyle or not has_known_bases(klass):
  74. # super first arg should not be the class
  75. if not call.args:
  76. continue
  77. # calling super(type(self), self) can lead to recursion loop
  78. # in derived classes
  79. arg0 = call.args[0]
  80. if (
  81. isinstance(arg0, nodes.Call)
  82. and isinstance(arg0.func, nodes.Name)
  83. and arg0.func.name == "type"
  84. ):
  85. self.add_message("bad-super-call", node=call, args=("type",))
  86. continue
  87. # calling super(self.__class__, self) can lead to recursion loop
  88. # in derived classes
  89. if (
  90. len(call.args) >= 2
  91. and isinstance(call.args[1], nodes.Name)
  92. and call.args[1].name == "self"
  93. and isinstance(arg0, nodes.Attribute)
  94. and arg0.attrname == "__class__"
  95. ):
  96. self.add_message(
  97. "bad-super-call", node=call, args=("self.__class__",)
  98. )
  99. continue
  100. try:
  101. supcls = call.args and next(call.args[0].infer(), None)
  102. except astroid.InferenceError:
  103. continue
  104. if klass is not supcls:
  105. name = None
  106. # if supcls is not Uninferable, then supcls was inferred
  107. # and use its name. Otherwise, try to look
  108. # for call.args[0].name
  109. if supcls:
  110. name = supcls.name
  111. elif call.args and hasattr(call.args[0], "name"):
  112. name = call.args[0].name
  113. if name:
  114. self.add_message("bad-super-call", node=call, args=(name,))
  115. visit_asyncfunctiondef = visit_functiondef
  116. def register(linter):
  117. """required method to auto register this checker"""
  118. linter.register_checker(NewStyleConflictChecker(linter))