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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# === Class decorator: identity returns the class unchanged ===
def identity(cls):
return cls
@identity
class Foo:
x = 1
assert Foo.x == 1
assert Foo().x == 1
# === Class decorator can replace the class with any value ===
def make_marker(cls):
return 'decorated'
@make_marker
class Bar:
pass
assert Bar == 'decorated'
# === Decorator factory (decorator with arguments) ===
def tag(label):
def deco(cls):
cls.label = label
return cls
return deco
@tag('hello')
class Baz:
pass
assert Baz.label == 'hello'
# === Stacked decorators apply bottom-up (nearest the class first) ===
order = []
def first(cls):
order.append('first')
return cls
def second(cls):
order.append('second')
return cls
@first
@second
class Multi:
pass
assert order == ['second', 'first']
# === Registry pattern: decorator records the class and returns it ===
REGISTRY = {}
def register(cls):
REGISTRY[cls.__name__] = cls
return cls
@register
class Alpha:
pass
@register
class Beta:
pass
assert sorted(REGISTRY) == ['Alpha', 'Beta']
assert REGISTRY['Alpha'] is Alpha
# === Method injection: decorator adds a method usable on instances ===
def add_greet(cls):
def greet(self):
return 'hi'
cls.greet = greet
return cls
@add_greet
class Greeter:
pass
assert Greeter().greet() == 'hi'
# === __init__ injection: the codegen pattern @dataclass builds on ===
def auto_init(cls):
def __init__(self, x):
self.x = x
cls.__init__ = __init__
return cls
@auto_init
class Point:
pass
assert Point(9).x == 9
# === __repr__ injection dispatches through repr() ===
def add_repr(cls):
def __repr__(self):
return 'custom-repr'
cls.__repr__ = __repr__
return cls
@add_repr
class Reprable:
pass
assert repr(Reprable()) == 'custom-repr'
# === Decorator that replaces the class with an instance (singleton) ===
def singleton(cls):
return cls()
@singleton
class Config:
v = 3
assert Config.v == 3
# === Decorators are arbitrary expressions, not just names ===
DECOS = {'k': identity}
@DECOS['k']
class Subscripted:
y = 2
assert Subscripted.y == 2
# === Decorators evaluate in the enclosing scope and capture its locals ===
def outer():
captured = 'outer-value'
def deco(cls):
cls.tag = captured
return cls
@deco
class Inner:
pass
return Inner
assert outer().tag == 'outer-value'
# Multi-hop: the decorator itself comes from a grandparent scope.
def grandparent():
def deco(cls):
cls.mark = 1
return cls
def middle():
@deco
class C:
pass
return C
return middle()
assert grandparent().mark == 1
# Regression: a *nested scope inside the decorator expression* that captures an
# enclosing local needs a cell var too.
def lambda_in_decorator_position():
n = 5
@(lambda cls: setattr(cls, 'n', n) or cls)
class C:
pass
return C
assert lambda_in_decorator_position().n == 5
def lambda_passed_to_decorator_factory():
n = 7
def factory(fn):
return fn
@factory(lambda cls: setattr(cls, 'n', n) or cls)
class C:
pass
return C
assert lambda_passed_to_decorator_factory().n == 7
# === A raising decorator unwinds cleanly and execution continues ===
# The raise happens mid-stack, with the outer decorator still on the operand
# stack; looping checks the cleanup repeats without leaking.
applied = []
def record(cls):
applied.append('record')
return cls
def boom(cls):
raise ValueError('boom')
for _ in range(50):
try:
@record
@boom
@record
class Unwind:
x = 1
assert False, 'expected the decorator to raise'
except ValueError as exc:
assert str(exc) == 'boom'
# only the decorator below `boom` runs; the one above it never does
assert applied == ['record'] * 50
# === A walrus in a decorator expression binds in the enclosing scope ===
# Decorators evaluate in the enclosing scope, so `:=` there makes a local of
# that scope — it must not fall through to the module globals.
def passthrough(v):
def deco(cls):
return cls
return deco
shadowed = 'global'
def walrus_binds_locally():
@passthrough(shadowed := 'local')
class C:
pass
return shadowed
assert walrus_binds_locally() == 'local'
assert shadowed == 'global'
# The binding is a local, so it never escapes to the module namespace.
def walrus_does_not_leak():
@passthrough(never_global := 'inner')
class C:
pass
return never_global
assert walrus_does_not_leak() == 'inner'
try:
never_global
assert False, 'expected NameError'
except NameError as exc:
assert str(exc) == "name 'never_global' is not defined"
# `global` still redirects the walrus to the module scope.
promoted = 'before'
def walrus_with_global():
global promoted
@passthrough(promoted := 'after')
class C:
pass
walrus_with_global()
assert promoted == 'after'
# A walrus in a decorator can be captured by a nested function (needs a cell).
def walrus_captured_by_closure():
@passthrough(captured := 11)
class C:
pass
def read():
return captured
return read()
assert walrus_captured_by_closure() == 11
# === Decorator expressions evaluate top-down, before the class body ===
# Apply order is bottom-up, but *evaluation* of the decorator expressions
# happens first and in source order, then the body, then the applications.
events = []
def trace(label):
events.append('eval:' + label)
def deco(cls):
events.append('apply:' + label)
return cls
return deco
def body_marker():
events.append('body')
return 1
@trace('outer')
@trace('inner')
class Traced:
marker = body_marker()
assert events == ['eval:outer', 'eval:inner', 'body', 'apply:inner', 'apply:outer']
# === Attribute expressions work in decorator position ===
class Holder:
pass
def attach(cls):
cls.via = 'attribute'
return cls
Holder.deco = attach
@Holder.deco
class ViaAttribute:
pass
assert ViaAttribute.via == 'attribute'
# === A non-callable decorator raises TypeError ===
try:
@42
class NotCallable:
pass
assert False, 'expected TypeError'
except TypeError as exc:
assert str(exc) == "'int' object is not callable"
# === The class name stays unbound when a decorator raises ===
def raiser(cls):
raise ValueError('nope')
try:
@raiser
class NeverBound:
pass
assert False, 'expected ValueError'
except ValueError:
pass
try:
NeverBound
assert False, 'expected NameError'
except NameError as exc:
assert str(exc) == "name 'NeverBound' is not defined"
# === Decoration preserves __name__ and __doc__ ===
def identity_deco(cls):
return cls
@identity_deco
class Documented:
"""A docstring."""
assert Documented.__name__ == 'Documented'
assert Documented.__doc__ == 'A docstring.'