Skip to content

Commit 93160bd

Browse files
committed
more craziness
1 parent b5815b6 commit 93160bd

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

14-ast.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,31 @@ class Checker(TypeChecker):
2929
return cls
3030
3131
32-
@dataclass
3332
class Point:
3433
x: int
3534
y: int
3635
3736
3837
p = Point(1, 2)
39-
print(p)
38+
print(p, 'OK')
39+
print('---')
4040
p.x = 'foo'
41-
print(p)
41+
print(p, 'OK')
4242
"""
4343

4444

4545
class DecorateClasses(ast.NodeTransformer):
4646
def visit_ClassDef(self, node):
47-
if not [True for dec in node.decorator_list if dec.id == 'dataclass']:
48-
# Not a dataclass
49-
return node
50-
dec = ast.Name(id='typed', ctx=ast.Load())
51-
node.decorator_list.insert(0, dec)
47+
# Only decorate classes with type annotations
48+
if [True for n in node.body if isinstance(n, ast.AnnAssign)]:
49+
dec = ast.Name(id='dataclass', ctx=ast.Load())
50+
node.decorator_list.insert(0, dec)
51+
dec = ast.Name(id='typed', ctx=ast.Load())
52+
node.decorator_list.insert(0, dec)
5253
return node
5354

5455

5556
tree = ast.parse(CODE)
56-
exec(compile(tree, filename='', mode='exec'))
57-
58-
print('---')
5957
tree = DecorateClasses().visit(tree)
6058
ast.fix_missing_locations(tree)
6159
exec(compile(tree, filename='', mode='exec'))

0 commit comments

Comments
 (0)