| 1 | #!/usr/bin/env python2
|
| 2 | """
|
| 3 | typed_demo.py: uses typed_demo.asdl
|
| 4 |
|
| 5 | MyPy typed are checked by asdl/TEST.sh
|
| 6 | """
|
| 7 | from __future__ import print_function
|
| 8 |
|
| 9 | import sys
|
| 10 |
|
| 11 | from _devbuild.gen.typed_demo_asdl import (
|
| 12 | cflow, cflow_e, op_id_e, source_location, word, bool_expr, Dicts
|
| 13 | )
|
| 14 |
|
| 15 | from typing import List, cast
|
| 16 |
|
| 17 |
|
| 18 | def main(argv):
|
| 19 | # type: (List[str]) -> None
|
| 20 |
|
| 21 | op = op_id_e.Plus
|
| 22 | print(op)
|
| 23 | print(repr(op))
|
| 24 |
|
| 25 | n1 = cflow.Break
|
| 26 |
|
| 27 | # Type error!
|
| 28 | # n2 = cflow.Return()
|
| 29 |
|
| 30 | # The real way to do it
|
| 31 | n2 = cflow.Return.CreateNull(alloc_lists=True)
|
| 32 |
|
| 33 | #n3 = cflow.Return('hi') # type error, yay!
|
| 34 | n3 = cflow.Return(42)
|
| 35 |
|
| 36 | print(n1)
|
| 37 | print(n2)
|
| 38 | print(n3)
|
| 39 |
|
| 40 | nodes =[n1, n2, n3]
|
| 41 | #reveal_type(nodes)
|
| 42 |
|
| 43 | for n in nodes:
|
| 44 | print(n.tag())
|
| 45 | if n.tag() == cflow_e.Return:
|
| 46 | print('Return = %s' % n)
|
| 47 |
|
| 48 | # Hm mypy doesn't like this one, but I think it should be equivalent.
|
| 49 | # type aliases are only at the top level?
|
| 50 |
|
| 51 | # https://github.com/python/mypy/issues/3855
|
| 52 | # This is closed by #5926 that emits a better error message, and accepts
|
| 53 | # safe use cases (e.g. when one nested class is a subclass of another
|
| 54 | # nested class).
|
| 55 |
|
| 56 | #reveal_type(n)
|
| 57 | #n2 = cast(cflow.Return, n)
|
| 58 |
|
| 59 | n2 = cast(cflow.Return, n)
|
| 60 | #reveal_type(n2)
|
| 61 |
|
| 62 | print('status = %s' % n2.status)
|
| 63 |
|
| 64 | loc = source_location('foo', 13, 0, 2)
|
| 65 | print(loc)
|
| 66 |
|
| 67 | w1 = word('w1')
|
| 68 | w2 = word('e2')
|
| 69 | b1 = bool_expr.Binary(w1, w2)
|
| 70 | b2 = bool_expr.LogicalNot(b1)
|
| 71 | print(b1)
|
| 72 | print(b2)
|
| 73 |
|
| 74 | b3 = bool_expr.LogicalBinary(op_id_e.Star, b1, b2)
|
| 75 | print(b3)
|
| 76 | #b4 = bool_expr.LogicalBinary(op_id_e.Star, b1, 'a')
|
| 77 |
|
| 78 | # default should be None to avoid allocation?
|
| 79 | m = Dicts.CreateNull(alloc_lists=True)
|
| 80 |
|
| 81 | # assert m.ss is None, m.ss
|
| 82 | # assert m.ib is None, m.ib
|
| 83 | print(m.ss)
|
| 84 | print(m.ib)
|
| 85 |
|
| 86 | m.ss = {}
|
| 87 | m.ib = {}
|
| 88 |
|
| 89 | m.ss['str'] = 'str'
|
| 90 | m.ib[3] = True
|
| 91 |
|
| 92 | # Type errors
|
| 93 | #m.ss['str'] = 3
|
| 94 | #m.ib[3] = 'str'
|
| 95 |
|
| 96 |
|
| 97 | if __name__ == '__main__':
|
| 98 | try:
|
| 99 | main(sys.argv)
|
| 100 | except RuntimeError as e:
|
| 101 | print('FATAL: %s' % e, file=sys.stderr)
|
| 102 | sys.exit(1)
|