-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathqualifiers_final_decorator.py
More file actions
127 lines (87 loc) · 2.56 KB
/
qualifiers_final_decorator.py
File metadata and controls
127 lines (87 loc) · 2.56 KB
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
"""
Tests the @final decorator.
"""
# Specification: https://typing.readthedocs.io/en/latest/spec/qualifiers.html#final
from typing import final, overload
from _qualifiers_final_decorator import Base3, Base4
# > A type checker should prohibit any class decorated with @final from being
# > subclassed and any method decorated with @final from being overridden in a
# > subclass. The method decorator version may be used with all of instance
# > methods, class methods, static methods, and properties.
@final
class Base1:
...
class Derived1(Base1): # E: Cannot inherit from final class "Base"
...
class Base2:
@final
def method1(self) -> None:
pass
@final
@classmethod
def method2(cls) -> None:
pass
@final
@staticmethod
def method3() -> None:
pass
# > For overloaded methods, @final should be placed on the implementation.
@overload
def method4(self, x: int) -> int:
...
@overload
def method4(self, x: str) -> str:
...
@final
def method4(self, x: int | str) -> int | str:
return 0
class Derived2(Base2):
def method1(self) -> None: # E
pass
@classmethod # E[method2]
def method2(cls) -> None: # E[method2]
pass
@staticmethod # E[method3]
def method3() -> None: # E[method3]
pass
@overload # E[method4]
def method4(self, x: int) -> int: # E[method4]
...
@overload
def method4(self, x: str) -> str:
...
def method4(self, x: int | str) -> int | str: # E[method4]
return 0
class Derived3(Base3):
@overload # E[Derived3]
def method(self, x: int) -> int: # E[Derived3]
...
@overload # E[Derived3-2]
@final # E[Derived3-2]: should be applied only to implementation
def method(self, x: str) -> str: # E[Derived3-2]
...
def method(self, x: int | str) -> int | str: # E[Derived3]
return 0
class Derived4(Base4):
@overload # E[Derived4]
def method(self, x: int) -> int: # E[Derived4]
...
@overload
def method(self, x: str) -> str:
...
def method(self, x: int | str) -> int | str: # E[Derived4]
return 0
class Base5_1:
...
class Base5_2:
@final
def method(self, v: int) -> None:
...
# Test multiple inheritance case.
class Derived5(Base5_1, Base5_2):
def method(self) -> None: # E
...
# > It is an error to use @final on a non-method function.
@final # E[func]: not allowed on non-method function.
def func1() -> int: # E[func]
return 0