summaryrefslogtreecommitdiff
path: root/pylint/test/functional/super_checks.py
blob: f4aec0acd9dcecd88ced33e574d9e8d1275e9387 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# pylint: disable=too-few-public-methods,import-error, no-absolute-import,missing-docstring
"""check use of super"""

from unknown import Missing

class Aaaa:  # <3.0:[old-style-class]
    """old style"""
    def hop(self):  # <3.0:[super-on-old-class]
        """hop"""
        super(Aaaa, self).hop() # >=3.0:[no-member]

    def __init__(self):  # <3.0:[super-on-old-class]
        super(Aaaa, self).__init__()

class NewAaaa(object):
    """old style"""
    def hop(self):
        """hop"""
        super(NewAaaa, self).hop() # [no-member]

    def __init__(self):
        super(Aaaa, self).__init__()  # [bad-super-call]

class Py3kAaaa(NewAaaa):
    """new style"""
    def __init__(self):
        super().__init__()  # <3.0:[missing-super-argument]

class Py3kWrongSuper(Py3kAaaa):
    """new style"""
    def __init__(self):
        super(NewAaaa, self).__init__()  # [bad-super-call]

class WrongNameRegression(Py3kAaaa):
    """ test a regression with the message """
    def __init__(self):
        super(Missing, self).__init__()  # [bad-super-call]

class Getattr(object):
    """ crash """
    name = NewAaaa

class CrashSuper(object):
    """ test a crash with this checker """
    def __init__(self):
        super(Getattr.name, self).__init__()  # [bad-super-call]

class Empty(object):
    """Just an empty class."""

class SuperDifferentScope(object):
    """Don'emit bad-super-call when the super call is in another scope.
    For reference, see https://bitbucket.org/logilab/pylint/issue/403.
    """
    @staticmethod
    def test():
        """Test that a bad-super-call is not emitted for this case."""
        class FalsePositive(Empty):
            """The following super is in another scope than `test`."""
            def __init__(self, arg):
                super(FalsePositive, self).__init__(arg)
        super(object, 1).__init__() # [bad-super-call]


class UnknownBases(Missing):
    """Don't emit if we don't know all the bases."""
    def __init__(self):
        super(UnknownBases, self).__init__()
        super(UnknownBases, self).test()
        super(Missing, self).test() # [bad-super-call]


# Test that we are detecting proper super errors.

class BaseClass(object):

    not_a_method = 42

    def function(self, param):
        return param + self.not_a_method

    def __getattr__(self, attr):
        return attr


class InvalidSuperChecks(BaseClass):

    def __init__(self):
        super(InvalidSuperChecks, self).not_a_method() # [not-callable]
        super(InvalidSuperChecks, self).attribute_error() # [no-member]
        super(InvalidSuperChecks, self).function(42)
        super(InvalidSuperChecks, self).function() # [no-value-for-parameter]
        super(InvalidSuperChecks, self).function(42, 24, 24) # [too-many-function-args]
        # +1: [unexpected-keyword-arg,no-value-for-parameter]
        super(InvalidSuperChecks, self).function(lala=42)
        # Even though BaseClass has a __getattr__, that won't
        # be called.
        super(InvalidSuperChecks, self).attribute_error() # [no-member]