PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/pcc-alien/venv_pcc-alien/lib/python3.6/site-packages/pylint/checkers/newstyle.py

https://bitbucket.org/edhilgendorf/public-code
Python | 142 lines | 119 code | 7 blank | 16 comment | 0 complexity | 91bfa003da887beaac02523ff102f26c MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, BSD-3-Clause, MIT, CC-BY-SA-3.0, 0BSD
  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 sys
  15. import astroid
  16. from pylint.interfaces import IAstroidChecker
  17. from pylint.checkers import BaseChecker
  18. from pylint.checkers.utils import check_messages, node_frame_class, has_known_bases
  19. MSGS = {
  20. "E1003": (
  21. "Bad first argument %r given to super()",
  22. "bad-super-call",
  23. "Used when another argument than the current class is given as "
  24. "first argument of the super builtin.",
  25. ),
  26. "E1004": (
  27. "Missing argument to super()",
  28. "missing-super-argument",
  29. "Used when the super builtin didn't receive an argument.",
  30. {"maxversion": (3, 0)},
  31. ),
  32. }
  33. class NewStyleConflictChecker(BaseChecker):
  34. """checks for usage of new style capabilities on old style classes and
  35. other new/old styles conflicts problems
  36. * use of property, __slots__, super
  37. * "super" usage
  38. """
  39. __implements__ = (IAstroidChecker,)
  40. # configuration section name
  41. name = "newstyle"
  42. # messages
  43. msgs = MSGS
  44. priority = -2
  45. # configuration options
  46. options = ()
  47. @check_messages("bad-super-call", "missing-super-argument")
  48. def visit_functiondef(self, node):
  49. """check use of super"""
  50. # ignore actual functions or method within a new style class
  51. if not node.is_method():
  52. return
  53. klass = node.parent.frame()
  54. for stmt in node.nodes_of_class(astroid.Call):
  55. if node_frame_class(stmt) != node_frame_class(node):
  56. # Don't look down in other scopes.
  57. continue
  58. expr = stmt.func
  59. if not isinstance(expr, astroid.Attribute):
  60. continue
  61. call = expr.expr
  62. # skip the test if using super
  63. if not (
  64. isinstance(call, astroid.Call)
  65. and isinstance(call.func, astroid.Name)
  66. and call.func.name == "super"
  67. ):
  68. continue
  69. if not klass.newstyle and has_known_bases(klass):
  70. # super should not be used on an old style class
  71. continue
  72. else:
  73. # super first arg should be the class
  74. if not call.args:
  75. if sys.version_info[0] == 3:
  76. # unless Python 3
  77. continue
  78. else:
  79. self.add_message("missing-super-argument", node=call)
  80. continue
  81. # calling super(type(self), self) can lead to recursion loop
  82. # in derived classes
  83. arg0 = call.args[0]
  84. if (
  85. isinstance(arg0, astroid.Call)
  86. and isinstance(arg0.func, astroid.Name)
  87. and arg0.func.name == "type"
  88. ):
  89. self.add_message("bad-super-call", node=call, args=("type",))
  90. continue
  91. # calling super(self.__class__, self) can lead to recursion loop
  92. # in derived classes
  93. if (
  94. len(call.args) >= 2
  95. and isinstance(call.args[1], astroid.Name)
  96. and call.args[1].name == "self"
  97. and isinstance(arg0, astroid.Attribute)
  98. and arg0.attrname == "__class__"
  99. ):
  100. self.add_message(
  101. "bad-super-call", node=call, args=("self.__class__",)
  102. )
  103. continue
  104. try:
  105. supcls = call.args and next(call.args[0].infer(), None)
  106. except astroid.InferenceError:
  107. continue
  108. if klass is not supcls:
  109. name = None
  110. # if supcls is not Uninferable, then supcls was infered
  111. # and use its name. Otherwise, try to look
  112. # for call.args[0].name
  113. if supcls:
  114. name = supcls.name
  115. elif call.args and hasattr(call.args[0], "name"):
  116. name = call.args[0].name
  117. if name:
  118. self.add_message("bad-super-call", node=call, args=(name,))
  119. visit_asyncfunctiondef = visit_functiondef
  120. def register(linter):
  121. """required method to auto register this checker """
  122. linter.register_checker(NewStyleConflictChecker(linter))