forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.py
267 lines (190 loc) · 4.12 KB
/
class.py
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
__name__ = "class"
class Foo:
def __init__(self, x):
assert x == 5
self.x = x
def square(self):
return self.x * self.x
y = 7
foo = Foo(5)
assert foo.y == Foo.y
assert foo.x == 5
assert foo.square() == 25
assert Foo.__name__ == "Foo"
assert Foo.__qualname__ == "Foo"
assert Foo.__module__ == "class"
assert Foo.square.__name__ == "square"
assert Foo.square.__qualname__ == "Foo.square"
assert Foo.square.__module__ == "class"
class Bar:
""" W00t """
def __init__(self, x):
self.x = x
def get_x(self):
assert __class__ is Bar
return self.x
def doc_func(self):
"doc string"
pass
@classmethod
def fubar(cls, x):
assert __class__ is cls
assert cls is Bar
assert x == 2
@staticmethod
def kungfu(x):
assert __class__ is Bar
assert x == 3
assert Bar.__doc__ == " W00t "
bar = Bar(42)
assert bar.get_x.__doc__ == None
assert bar.doc_func.__doc__ == "doc string"
bar.fubar(2)
Bar.fubar(2)
bar.kungfu(3)
Bar.kungfu(3)
class Bar2(Bar):
def __init__(self):
super().__init__(101)
bar2 = Bar2()
assert bar2.get_x() == 101
class A():
def test(self):
return 100
class B():
def test1(self):
return 200
@classmethod
def test3(cls):
return 300
class C(A,B):
def test(self):
return super().test()
def test1(self):
return super().test1()
@classmethod
def test3(cls):
return super().test3()
c = C()
assert c.test() == 100
assert c.test1() == 200
assert c.test3() == 300
assert C.test3() == 300
class Me():
def test(me):
return 100
class Me2(Me):
def test(me):
return super().test()
class A():
def f(self):
pass
class B(A):
def f(self):
super().f()
class C(B):
def f(self):
super().f()
C().f()
me = Me2()
assert me.test() == 100
a = super(bool, True)
assert isinstance(a, super)
assert type(a) is super
assert a.conjugate() == 1
class T1:
"test1"
assert T1.__doc__ == "test1"
class T2:
'''test2'''
assert T2.__doc__ == "test2"
class T3:
"""
test3
"""
assert T3.__doc__ == "\n test3\n "
class T4:
"""test4"""
def t1(self):
"""t1"""
pass
assert T4.__doc__ == "test4"
assert T4.t1.__doc__ == "t1"
cm = classmethod(lambda cls: cls)
assert cm.__func__(int) is int
assert str(super(int, 5)) == "<super: <class 'int'>, <int object>>"
class T5(int):
pass
assert str(super(int, T5(5))) == "<super: <class 'int'>, <T5 object>>"
assert str(super(type, None)) == "<super: <class 'type'>, NULL>"
assert str(super(int).__get__(T5(5))) == "<super: <class 'int'>, <T5 object>>"
a = 1
class A:
a = 2
def b():
assert a == 1
b()
assert a == 2
A.b()
class A:
pass
assert A.__doc__ == None
class B:
"Docstring"
assert B.__doc__ == "Docstring"
# TODO: uncomment once free vars/cells are working
# The symboltable sees that b() is referring to a in the nested scope,
# so it marks it as non local. When it's executed, it walks up the scopes
# and still finds the a from the class scope.
# a = 1
# def nested_scope():
# a = 2
# class A:
# a = 3
# def b():
# assert a == 2
# b()
# assert a == 3
# A.b()
# nested_scope()
# Multiple inheritance and mro tests.
class A():
def f(self):
return 'a'
class B(A):
def f(self):
return 'b' + super().f()
class C(A):
def f(self):
return 'c' + super().f()
class D(B, C):
def f(self):
return 'd' + super().f()
assert D().f() == 'dbca', "Mro resolution using super failed."
class A():
pass
try:
class B(A, A):
pass
except TypeError:
pass
else:
assert False, "Managed to create a class with duplicate base classes."
class A():
pass
class B(A):
pass
try:
class C(A, B):
pass
except TypeError:
pass
else:
assert False, "Managed to create a class without local type precedence."
class A():
a: int
assert A.__annotations__['a'] == int
class A: pass
class B: pass
class C(*(A, B), **{"metaclass": type}):
pass