/tests/functional/s/super_checks.py

https://github.com/PyCQA/pylint · Python · 125 lines · 98 code · 11 blank · 16 comment · 1 complexity · 8dabddfd49155c63bd1efa81d3d9b28b MD5 · raw file

  1. # pylint: disable=too-few-public-methods,import-error, no-absolute-import,missing-docstring, useless-object-inheritance
  2. # pylint: disable=useless-super-delegation,wrong-import-position,invalid-name, wrong-import-order
  3. # pylint: disable=super-with-arguments
  4. from unknown import Missing
  5. class Aaaa:
  6. """old style"""
  7. def hop(self):
  8. """hop"""
  9. super(Aaaa, self).hop() # >=3.0:[no-member]
  10. def __init__(self):
  11. super(Aaaa, self).__init__()
  12. class NewAaaa(object):
  13. """old style"""
  14. def hop(self):
  15. """hop"""
  16. super(NewAaaa, self).hop() # [no-member]
  17. def __init__(self):
  18. super(Aaaa, self).__init__() # [bad-super-call]
  19. class Py3kAaaa(NewAaaa):
  20. """new style"""
  21. def __init__(self):
  22. super().__init__() # <3.0:[missing-super-argument]
  23. class Py3kWrongSuper(Py3kAaaa):
  24. """new style"""
  25. def __init__(self):
  26. super(NewAaaa, self).__init__() # [bad-super-call]
  27. class WrongNameRegression(Py3kAaaa):
  28. """ test a regression with the message """
  29. def __init__(self):
  30. super(Missing, self).__init__() # [bad-super-call]
  31. class Getattr(object):
  32. """ crash """
  33. name = NewAaaa
  34. class CrashSuper(object):
  35. """ test a crash with this checker """
  36. def __init__(self):
  37. super(Getattr.name, self).__init__() # [bad-super-call]
  38. class Empty(object):
  39. """Just an empty class."""
  40. class SuperDifferentScope(object):
  41. """Don'emit bad-super-call when the super call is in another scope.
  42. For reference, see https://bitbucket.org/logilab/pylint/issue/403.
  43. """
  44. @staticmethod
  45. def test():
  46. """Test that a bad-super-call is not emitted for this case."""
  47. class FalsePositive(Empty):
  48. """The following super is in another scope than `test`."""
  49. def __init__(self, arg):
  50. super(FalsePositive, self).__init__(arg)
  51. super(object, 1).__init__() # [bad-super-call]
  52. class UnknownBases(Missing):
  53. """Don't emit if we don't know all the bases."""
  54. def __init__(self):
  55. super(UnknownBases, self).__init__()
  56. super(UnknownBases, self).test()
  57. super(Missing, self).test() # [bad-super-call]
  58. # Test that we are detecting proper super errors.
  59. class BaseClass(object):
  60. not_a_method = 42
  61. def function(self, param):
  62. return param + self.not_a_method
  63. def __getattr__(self, attr):
  64. return attr
  65. class InvalidSuperChecks(BaseClass):
  66. def __init__(self):
  67. super(InvalidSuperChecks, self).not_a_method() # [not-callable]
  68. super(InvalidSuperChecks, self).attribute_error() # [no-member]
  69. super(InvalidSuperChecks, self).function(42)
  70. super(InvalidSuperChecks, self).function() # [no-value-for-parameter]
  71. super(InvalidSuperChecks, self).function(42, 24, 24) # [too-many-function-args]
  72. # +1: [unexpected-keyword-arg,no-value-for-parameter]
  73. super(InvalidSuperChecks, self).function(lala=42)
  74. # Even though BaseClass has a __getattr__, that won't
  75. # be called.
  76. super(InvalidSuperChecks, self).attribute_error() # [no-member]
  77. # Regression for PyCQA/pylint/issues/773
  78. import subprocess
  79. # The problem was related to astroid not filtering statements
  80. # at scope level properly, basically not doing strong updates.
  81. try:
  82. TimeoutExpired = subprocess.TimeoutExpired
  83. except AttributeError:
  84. class TimeoutExpired(subprocess.CalledProcessError):
  85. def __init__(self):
  86. returncode = -1
  87. self.timeout = -1
  88. super(TimeoutExpired, self).__init__("", returncode)
  89. class SuperWithType(object):
  90. """type(self) may lead to recursion loop in derived classes"""
  91. def __init__(self):
  92. super(type(self), self).__init__() # [bad-super-call]
  93. class SuperWithSelfClass(object):
  94. """self.__class__ may lead to recursion loop in derived classes"""
  95. def __init__(self):
  96. super(self.__class__, self).__init__() # [bad-super-call]