Debian Codegraph @

mypyc/test-data/run-mypy-sim.test

raw
1 -- Some test code that tries to simulate important/interesting parts of mypy itself!
2 [case testSimulateMypy]
3 from mypy_extensions import trait
4 from typing import List, TypeVar, cast, Generic
5 from abc import abstractmethod
6 import other_strconv as strconv
7 #from other_visitor import ExpressionVisitor, StatementVisitor, NodeVisitor
8 import other_visitor as visitor
9
10 T = TypeVar('T')
11
12 ############ nodes.py
13
14 class Context:
15 def __init__(self) -> None:
16 self.line = -1
17 def set_line(self, line: int) -> None:
18 self.line = line
19
20 class Node(Context):
21 def accept(self, visitor: visitor.NodeVisitor[T]) -> T: return cast(T, None)
22 def to_str(self) -> str:
23 return self.accept(strconv.StrConv())
24
25 @trait
26 class Statement(Node):
27 def accept(self, visitor: visitor.StatementVisitor[T]) -> T: return cast(T, None)
28
29 @trait
30 class Expression(Node):
31 def accept(self, visitor: visitor.ExpressionVisitor[T]) -> T: return cast(T, None)
32
33 @trait
34 class SymbolNode(Node):
35 """Nodes that can be stored in a symbol table."""
36 @abstractmethod
37 def name(self) -> str: return cast(str, None)
38
39 class FuncBase(Node):
40 def __init__(self) -> None:
41 super().__init__()
42 self.is_static = False
43
44 class Block(Statement):
45 def __init__(self, stmts: List[Statement]) -> None:
46 self.stmts = stmts
47
48 def accept(self, visitor: visitor.StatementVisitor[T]) -> T:
49 return visitor.visit_block(self)
50
51 class ExprStmt(Statement):
52 def __init__(self, expr: Expression) -> None:
53 self.expr = expr
54
55 def accept(self, visitor: visitor.StatementVisitor[T]) -> T:
56 return visitor.visit_expr_stmt(self)
57
58 class FuncItem(FuncBase):
59 def __init__(self, body: Block) -> None:
60 self.body = body
61
62 class FuncDef(FuncItem, SymbolNode, Statement):
63 def __init__(self, name: str, body: Block) -> None:
64 super().__init__(body)
65 self._name = name
66 def accept(self, visitor: visitor.StatementVisitor[T]) -> T:
67 return visitor.visit_func_def(self)
68 def name(self) -> str:
69 return self._name
70
71 class LambdaExpr(FuncItem, Expression):
72 def accept(self, visitor: visitor.ExpressionVisitor[T]) -> T:
73 return visitor.visit_lambda_expr(self)
74
75 def lol(x: Statement) -> int:
76 return x.line
77
78 [file other_visitor.py]
79 from mypy_extensions import trait
80 from typing import TypeVar, cast, Generic
81 from abc import abstractmethod
82 import native as nodes
83
84 T = TypeVar('T')
85
86 @trait
87 class ExpressionVisitor(Generic[T]):
88 @abstractmethod
89 def visit_lambda_expr(self, o: 'nodes.LambdaExpr') -> T:
90 return cast(T, None)
91
92 @trait
93 class StatementVisitor(Generic[T]):
94 @abstractmethod
95 def visit_block(self, o: 'nodes.Block') -> T:
96 return cast(T, None)
97
98 @abstractmethod
99 def visit_func_def(self, o: 'nodes.FuncDef') -> T:
100 return cast(T, None)
101
102 @abstractmethod
103 def visit_expr_stmt(self, o: 'nodes.ExprStmt') -> T:
104 return cast(T, None)
105
106 @trait
107 class NodeVisitor(Generic[T], ExpressionVisitor[T], StatementVisitor[T]):
108 pass
109
110 [file other_strconv.py]
111 from typing import List
112 import native as nodes
113 from other_visitor import NodeVisitor
114
115 class StrConv(NodeVisitor[str]):
116 def visit_block(self, b: nodes.Block) -> str:
117 # we really need comprehensions!
118 # TODO: PartialType unsupported
119 things = [] # type: List[str]
120 for s in b.stmts:
121 things.append(s.accept(self))
122 return "{" + "; ".join(things) + "}"
123
124 def visit_func_def(self, f: nodes.FuncDef) -> str:
125 return "def " + f.name() + "(): " + f.body.accept(self)
126
127 def visit_expr_stmt(self, e: nodes.ExprStmt) -> str:
128 return e.expr.accept(self)
129
130 def visit_lambda_expr(self, b: nodes.LambdaExpr) -> str:
131 return "(fn: " + b.body.accept(self) + ")"
132
133 [file driver.py]
134 from native import *
135
136 block = Block([Block([]), ExprStmt(LambdaExpr(Block([])))])
137 fn = FuncDef('test', block)
138 assert fn.to_str() == "def test(): {{}; (fn: {})}"