|
| 1 | +import ast |
| 2 | + |
| 3 | +CODE = """ |
| 4 | +from dataclasses import dataclass |
| 5 | +
|
| 6 | +
|
| 7 | +class TypeChecker: |
| 8 | + required_type = object |
| 9 | +
|
| 10 | + def __init__(self, name=None): |
| 11 | + self.name = name |
| 12 | +
|
| 13 | + def __get__(self, instance, owner=None): |
| 14 | + return instance.__dict__[self.name] |
| 15 | +
|
| 16 | + def __set__(self, instance, value): |
| 17 | + assert isinstance(value, self.required_type), \ |
| 18 | + f'Booooo! Expecting a {self.required_type.__name__}' |
| 19 | + instance.__dict__[self.name] = value |
| 20 | +
|
| 21 | +
|
| 22 | +def typed(cls): |
| 23 | + for var_name, var_type in cls.__annotations__.items(): |
| 24 | + class Checker(TypeChecker): |
| 25 | + required_type = var_type |
| 26 | + setattr(cls, var_name, Checker(var_name)) |
| 27 | + return cls |
| 28 | +
|
| 29 | +
|
| 30 | +@dataclass |
| 31 | +class Point: |
| 32 | + x: int |
| 33 | + y: int |
| 34 | +
|
| 35 | +
|
| 36 | +p = Point(1, 2) |
| 37 | +print(p) |
| 38 | +p.x = 'foo' |
| 39 | +print(p) |
| 40 | +""" |
| 41 | + |
| 42 | + |
| 43 | +class DecorateClasses(ast.NodeTransformer): |
| 44 | + def visit_ClassDef(self, node): |
| 45 | + if not [True for dec in node.decorator_list if dec.id == 'dataclass']: |
| 46 | + # Not a dataclass |
| 47 | + return node |
| 48 | + dec = ast.Name(id='typed', ctx=ast.Load()) |
| 49 | + node.decorator_list.insert(0, dec) |
| 50 | + return node |
| 51 | + |
| 52 | + |
| 53 | +tree = ast.parse(CODE) |
| 54 | +exec(compile(tree, filename='', mode='exec')) |
| 55 | + |
| 56 | +print('---') |
| 57 | +tree = DecorateClasses().visit(tree) |
| 58 | +ast.fix_missing_locations(tree) |
| 59 | +exec(compile(tree, filename='', mode='exec')) |
0 commit comments