run-mypy-sim.test 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. T = TypeVar('T')
  10. ############ nodes.py
  11. class Context:
  12. def __init__(self) -> None:
  13. self.line = -1
  14. def set_line(self, line: int) -> None:
  15. self.line = line
  16. class Node(Context):
  17. def accept(self, visitor: visitor.NodeVisitor[T]) -> T: return cast(T, None)
  18. def to_str(self) -> str:
  19. return self.accept(strconv.StrConv())
  20. @trait
  21. class Statement(Node):
  22. def accept(self, visitor: visitor.StatementVisitor[T]) -> T: return cast(T, None)
  23. @trait
  24. class Expression(Node):
  25. def accept(self, visitor: visitor.ExpressionVisitor[T]) -> T: return cast(T, None)
  26. @trait
  27. class SymbolNode(Node):
  28. """Nodes that can be stored in a symbol table."""
  29. @abstractmethod
  30. def name(self) -> str: return cast(str, None)
  31. class FuncBase(Node):
  32. def __init__(self) -> None:
  33. super().__init__()
  34. self.is_static = False
  35. class Block(Statement):
  36. def __init__(self, stmts: List[Statement]) -> None:
  37. self.stmts = stmts
  38. def accept(self, visitor: visitor.StatementVisitor[T]) -> T:
  39. return visitor.visit_block(self)
  40. class ExprStmt(Statement):
  41. def __init__(self, expr: Expression) -> None:
  42. self.expr = expr
  43. def accept(self, visitor: visitor.StatementVisitor[T]) -> T:
  44. return visitor.visit_expr_stmt(self)
  45. class FuncItem(FuncBase):
  46. def __init__(self, body: Block) -> None:
  47. self.body = body
  48. class FuncDef(FuncItem, SymbolNode, Statement):
  49. def __init__(self, name: str, body: Block) -> None:
  50. super().__init__(body)
  51. self._name = name
  52. def accept(self, visitor: visitor.StatementVisitor[T]) -> T:
  53. return visitor.visit_func_def(self)
  54. def name(self) -> str:
  55. return self._name
  56. class LambdaExpr(FuncItem, Expression):
  57. def accept(self, visitor: visitor.ExpressionVisitor[T]) -> T:
  58. return visitor.visit_lambda_expr(self)
  59. def lol(x: Statement) -> int:
  60. return x.line
  61. [file other_visitor.py]
  62. from mypy_extensions import trait
  63. from typing import TypeVar, cast, Generic
  64. from abc import abstractmethod
  65. import native as nodes
  66. T = TypeVar('T')
  67. @trait
  68. class ExpressionVisitor(Generic[T]):
  69. @abstractmethod
  70. def visit_lambda_expr(self, o: 'nodes.LambdaExpr') -> T:
  71. return cast(T, None)
  72. @trait
  73. class StatementVisitor(Generic[T]):
  74. @abstractmethod
  75. def visit_block(self, o: 'nodes.Block') -> T:
  76. return cast(T, None)
  77. @abstractmethod
  78. def visit_func_def(self, o: 'nodes.FuncDef') -> T:
  79. return cast(T, None)
  80. @abstractmethod
  81. def visit_expr_stmt(self, o: 'nodes.ExprStmt') -> T:
  82. return cast(T, None)
  83. @trait
  84. class NodeVisitor(Generic[T], ExpressionVisitor[T], StatementVisitor[T]):
  85. pass
  86. [file other_strconv.py]
  87. from typing import List
  88. import native as nodes
  89. from other_visitor import NodeVisitor
  90. class StrConv(NodeVisitor[str]):
  91. def visit_block(self, b: nodes.Block) -> str:
  92. # we really need comprehensions!
  93. # TODO: PartialType unsupported
  94. things = [] # type: List[str]
  95. for s in b.stmts:
  96. things.append(s.accept(self))
  97. return "{" + "; ".join(things) + "}"
  98. def visit_func_def(self, f: nodes.FuncDef) -> str:
  99. return "def " + f.name() + "(): " + f.body.accept(self)
  100. def visit_expr_stmt(self, e: nodes.ExprStmt) -> str:
  101. return e.expr.accept(self)
  102. def visit_lambda_expr(self, b: nodes.LambdaExpr) -> str:
  103. return "(fn: " + b.body.accept(self) + ")"
  104. [file driver.py]
  105. from native import *
  106. block = Block([Block([]), ExprStmt(LambdaExpr(Block([])))])
  107. fn = FuncDef('test', block)
  108. assert fn.to_str() == "def test(): {{}; (fn: {})}"