summaryrefslogtreecommitdiff
path: root/tests/functional/ext/no_self_use/no_self_use.py
blob: dfe3f6f35e3ce93d097c165692fd26b6e96ca845 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# pylint: disable=too-few-public-methods,missing-docstring,useless-object-inheritance,invalid-name
"""test detection of method which could be a function"""
from abc import ABC, abstractmethod
from typing import Protocol, overload


class Toto(object):
    """bla bal abl"""

    def __init__(self):
        self.aaa = 2

    def regular_method(self):
        """this method is a real method since it access to self"""
        self.function_method()

    def function_method(self):  # [no-self-use]
        """this method isn' a real method since it doesn't need self"""
        print('hello')

    async def async_regular_method(self):
        """this async method is a real method since it accesses self"""
        await self.async_function_method()

    async def async_function_method(self):  # [no-self-use]
        """this async method isn't a real method since it doesn't need self"""
        print('hello')

class Base(object):
    """an abstract class"""

    def __init__(self):
        self.aaa = 2

    def check(self, arg):
        """an abstract method, could not be a function"""
        raise NotImplementedError


class Sub(Base):
    """a concrete class"""

    def check(self, arg):
        """a concrete method, could not be a function since it need
        polymorphism benefits
        """
        return arg == 0

class Super(object):
    """same as before without abstract"""
    attr = 1
    def method(self):
        """regular"""
        print(self.attr)

class Sub1(Super):
    """override method with need for self"""
    def method(self):
        """no i can not be a function"""
        print(42)

    def __len__(self):
        """no i can not be a function"""
        return 42

    def __cmp__(self, other):
        """no i can not be a function"""
        print(42)

    def __copy__(self):
        return 24

    def __getstate__(self):
        return 42


class Prop(object):

    @property
    def count(self):
        """Don't emit no-self-use for properties.

        They can't be functions and they can be part of an
        API specification.
        """
        return 42


class A:
    def __init__(self):
        self.store = {}

    def get(self, key, default=None):
        return self.store.get(key, default)

class B(A):
    def get_memo(self, obj):
        return super().get(obj)


class C:
    def a(self, /):  # [no-self-use]
        ...

    # Disable with old error code
    # pylint: disable=use-symbolic-message-instead
    def b(self, /):  # pylint: disable=R0201
        ...


def func_a(self):  # pylint: disable=unused-argument
    pass


class Foo1(ABC):
    """Don't emit no-self-use for abstract methods."""

    @abstractmethod
    def a(self):
        pass

    def b(self):
        raise NotImplementedError

    def c(self):
        pass  # pass counts as abstract


class Foo2(Protocol):
    """Don't emit no-self-use for methods in Protocol classes."""

    def a(self):
        ...

class Foo3:
    """Don't emit no-self-use for overload methods."""

    @overload
    def a(self, var): ...

    @overload
    def a(self, var): ...

    def a(self, var):
        pass


class Foo4:
    """Other false positive cases."""

    @staticmethod
    def a(self):  # pylint: disable=unused-argument,bad-staticmethod-argument
        ...

    @staticmethod
    def b():
        ...

    @classmethod
    def c(self):  # pylint: disable=bad-classmethod-argument
        ...

    def d():  # pylint: disable=no-method-argument
        ...