summaryrefslogtreecommitdiff
path: root/tests/functional/a/abstract/abstract_class_instantiated.py
blob: 289870c9d4d2bfb519cf964fc52661b70977f776 (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
"""Check that instantiating a class with
`abc.ABCMeta` as metaclass fails if it defines
abstract methods.
"""

# pylint: disable=too-few-public-methods, missing-docstring
# pylint: disable=abstract-method, import-error

import abc
import weakref
from lala import Bala


class GoodClass(metaclass=abc.ABCMeta):
    pass

class SecondGoodClass(metaclass=abc.ABCMeta):
    def test(self):
        """ do nothing. """

class ThirdGoodClass(metaclass=abc.ABCMeta):
    """ This should not raise the warning. """
    def test(self):
        raise NotImplementedError()

class BadClass(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def test(self):
        """ do nothing. """

class SecondBadClass(metaclass=abc.ABCMeta):
    @property
    @abc.abstractmethod
    def test(self):
        """ do nothing. """

class ThirdBadClass(SecondBadClass):
    pass


class Structure(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def __iter__(self):
        pass
    @abc.abstractmethod
    def __len__(self):
        pass
    @abc.abstractmethod
    def __contains__(self, _):
        pass
    @abc.abstractmethod
    def __hash__(self):
        pass

class Container(Structure):
    def __contains__(self, _):
        pass

class Sizable(Structure):
    def __len__(self):
        return 42

class Hashable(Structure):
    __hash__ = 42


class Iterator(Structure):
    def keys(self):
        return iter([1, 2, 3])

    __iter__ = keys

class AbstractSizable(Structure):
    @abc.abstractmethod
    def length(self):
        pass
    __len__ = length

class NoMroAbstractMethods(Container, Iterator, Sizable, Hashable):
    pass

class BadMroAbstractMethods(Container, Iterator, AbstractSizable):
    pass

class SomeMetaclass(metaclass=abc.ABCMeta):

    @abc.abstractmethod
    def prop(self):
        pass

class FourthGoodClass(SomeMetaclass):
    """Don't consider this abstract if some attributes are
    there, but can't be inferred.
    """
    prop = Bala # missing


def main():
    """ do nothing """
    GoodClass()
    SecondGoodClass()
    ThirdGoodClass()
    FourthGoodClass()
    weakref.WeakKeyDictionary()
    weakref.WeakValueDictionary()
    NoMroAbstractMethods()

    BadMroAbstractMethods() # [abstract-class-instantiated]
    BadClass() # [abstract-class-instantiated]
    SecondBadClass() # [abstract-class-instantiated]
    ThirdBadClass() # [abstract-class-instantiated]


if 1: # pylint: disable=using-constant-test
    class FourthBadClass(metaclass=abc.ABCMeta):

        def test(self):
            pass
else:
    class FourthBadClass(metaclass=abc.ABCMeta):

        @abc.abstractmethod
        def test(self):
            pass


def main2():
    FourthBadClass() # [abstract-class-instantiated]


class BadClassTwo(abc.ABC):
    """
    Check that instantiating a class with `abc.ABCMeta` as ancestor fails if it
    defines abstract methods.
    """
    @abc.abstractmethod
    def test(self):
        pass


def main_two():
    """ do nothing """
    BadClassTwo() # [abstract-class-instantiated]