blob: 7454744d9b93408c8caaebf29855b1b8fe2f1496 (
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
|
"""Test that inheriting from something which is not
a class emits a warning. """
# pylint: disable=no-init, import-error, invalid-name, using-constant-test, useless-object-inheritance
# pylint: disable=missing-docstring, too-few-public-methods
from missing import Missing
if 1:
Ambiguous = None
else:
Ambiguous = int
class Empty(object):
""" Empty class. """
def return_class():
""" Return a class. """
return Good3
class Bad(1): # [inherit-non-class]
""" Can't inherit from instance. """
class Bad1(lambda abc: 42): # [inherit-non-class]
""" Can't inherit from lambda. """
class Bad2(object()): # [inherit-non-class]
""" Can't inherit from an instance of object. """
class Bad3(return_class): # [inherit-non-class]
""" Can't inherit from function. """
class Bad4(Empty()): # [inherit-non-class]
""" Can't inherit from instance. """
class Good(object):
pass
class Good1(int):
pass
class Good2(type):
pass
class Good3(type(int)):
pass
class Good4(return_class()):
pass
class Good5(Good4, int, object):
pass
class Good6(Ambiguous):
""" Inherits from something ambiguous.
This could emit a warning when we will have
flow detection.
"""
class Unknown(Missing):
pass
class Unknown1(Good5 if True else Bad1):
pass
class NotInheritableBool(bool): # [inherit-non-class]
pass
class NotInheritableRange(range): # [inherit-non-class]
pass
class NotInheritableSlice(slice): # [inherit-non-class]
pass
class NotInheritableMemoryView(memoryview): # [inherit-non-class]
pass
# Subscription of parent class that implements __class_getitem__
# and returns cls should be allowed.
class ParentGood:
def __class_getitem__(cls, item): # pylint: disable=unused-argument
return cls
class ParentBad:
def __class_getitem__(cls, item): # pylint: disable=unused-argument
return 42
# pylint: disable-next=fixme
# TODO This should emit 'unsubscriptable-object' for Python 3.6
class Child1(ParentGood[int]):
pass
class Child2(ParentBad[int]): # [inherit-non-class]
pass
# Classes that don't implement '__class_getitem__' are marked as unsubscriptable
class Child3(Empty[int]): # [unsubscriptable-object]
pass
|