summaryrefslogtreecommitdiff
path: root/tests/functional/n/not_callable.py
blob: 7c361aa6a3ac04ff7cb673b9f5428c1d6e5775c2 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# pylint: disable=missing-docstring,no-self-use,too-few-public-methods,wrong-import-position,useless-object-inheritance,use-dict-literal

REVISION = None

REVISION() # [not-callable]

def correct():
    return 1

REVISION = correct()

class Correct(object):
    """callable object"""

class MetaCorrect(object):
    """callable object"""
    def __call__(self):
        return self

INSTANCE = Correct()
CALLABLE_INSTANCE = MetaCorrect()
CORRECT = CALLABLE_INSTANCE()
INCORRECT = INSTANCE() # [not-callable]
LIST = []
INCORRECT = LIST() # [not-callable]
DICT = {}
INCORRECT = DICT() # [not-callable]
TUPLE = ()
INCORRECT = TUPLE() # [not-callable]
INT = 1
INCORRECT = INT() # [not-callable]

# Test calling properties. Pylint can detect when using only the
# getter, but it doesn't infer properly when having a getter
# and a setter.
class MyProperty(property):
    """ test subclasses """

class PropertyTest(object):
    """ class """

    def __init__(self):
        self.attr = 4

    @property
    def test(self):
        """ Get the attribute """
        return self.attr

    @test.setter
    def test(self, value):
        """ Set the attribute """
        self.attr = value

    @MyProperty
    def custom(self):
        """ Get the attribute """
        return self.attr

    @custom.setter
    def custom(self, value):
        """ Set the attribute """
        self.attr = value

PROP = PropertyTest()
PROP.test(40) # [not-callable]
PROP.custom() # [not-callable]

# Safe from not-callable when using properties.

class SafeProperty(object):
    @property
    def static(self):
        return staticmethod

    @property
    def klass(self):
        return classmethod

    @property
    def get_lambda(self):
        return lambda: None

    @property
    def other_function(self):
        def function(arg):
            return arg
        return function

    @property
    def dict_builtin(self):
        return dict

    @property
    def range_builtin(self):
        return range

    @property
    def instance(self):
        class Empty(object):
            def __call__(self):
                return 42
        return Empty()

    @property
    def does_not_make_sense(self):
        raise NotImplementedError

PROP1 = SafeProperty()
PROP1.static(2)
PROP1.klass(2)
PROP1.get_lambda()
PROP1.other_function(4)
PROP1.dict_builtin()
PROP1.range_builtin(4)
PROP1.instance()
PROP1.does_not_make_sense()


import missing # pylint: disable=import-error

class UnknownBaseCallable(missing.Blah):
    pass

UnknownBaseCallable()()

# Regression test for #4426
# If property is inferrable we shouldn't double emit the message
# See: https://github.com/PyCQA/pylint/issues/4426
class ClassWithProperty:
    @property
    def value(self):
        return 42

CLASS_WITH_PROP = ClassWithProperty().value()  # [not-callable]