File: test_py3_12.py

package info (click to toggle)
python-gast 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 260 kB
  • sloc: python: 2,208; sh: 7; makefile: 3
file content (71 lines) | stat: -rw-r--r-- 3,062 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import unittest

import ast
import gast
import sys

def dump(node):
    return gast.dump(node, show_empty=True)


class Python3_12TestCase(unittest.TestCase):

    def __init__(self, *args, **kwargs):
        unittest.TestCase.__init__(self, *args, **kwargs)
        self.maxDiff = None

    def test_type_alias(self):
        code = "type Point = tuple[float, float]"
        tree = gast.parse(code)
        compile(gast.gast_to_ast(tree), '<test>', 'exec')
        norm = ("Module(body=[TypeAlias(name=Name(id='Point', ctx=Store(),"
                " annotation=None, type_comment=None), type_params=[], "
                "value=Subscript(value=Name(id='tuple', ctx=Load(), "
                "annotation=None, type_comment=None), slice=Tuple(elts=["
                "Name(id='float', ctx=Load(), annotation=None, "
                "type_comment=None), Name(id='float', ctx=Load(), "
                "annotation=None, type_comment=None)], ctx=Load()), "
                "ctx=Load()))], type_ignores=[])")
        self.assertEqual(dump(tree), norm)

    def test_generic_type_alias(self):
        code = "type Point[T] = tuple[T, float]"
        tree = gast.parse(code)
        compile(gast.gast_to_ast(tree), '<test>', 'exec')
        norm = ("Module(body=[TypeAlias(name=Name(id='Point', ctx=Store(), "
                "annotation=None, type_comment=None), type_params=[TypeVar("
                "name='T', bound=None)], value=Subscript(value=Name(id='tuple'"
                ", ctx=Load(), annotation=None, type_comment=None), "
                "slice=Tuple(elts=[Name(id='T', ctx=Load(), annotation=None, "
                "type_comment=None), Name(id='float', ctx=Load(), "
                "annotation=None, type_comment=None)], ctx=Load()), ctx=Load()"
                "))], type_ignores=[])")
        self.assertEqual(dump(tree), norm)

    def test_generic_function(self):
        code = "def foo[T]():..."
        tree = gast.parse(code)
        compile(gast.gast_to_ast(tree), '<test>', 'exec')
        norm = ("Module(body=[FunctionDef(name='foo', args=arguments(args=[], "
                "posonlyargs=[], vararg=None, kwonlyargs=[], kw_defaults=[], "
                "kwarg=None, defaults=[]), body=[Expr(value=Constant(value="
                "Ellipsis, kind=None))], decorator_list=[], returns=None, "
                "type_comment=None, type_params=[TypeVar(name='T', "
                "bound=None)])], type_ignores=[])")
        self.assertEqual(dump(tree), norm)

    def test_generic_class(self):
        code = "class foo[T]:..."
        tree = gast.parse(code)
        compile(gast.gast_to_ast(tree), '<test>', 'exec')
        norm = ("Module(body=[ClassDef(name='foo', bases=[], keywords=[], "
                "body=[Expr(value=Constant(value=Ellipsis, kind=None))], "
                "decorator_list=[], type_params=[TypeVar(name='T', bound=None)"
                "])], type_ignores=[])")
        self.assertEqual(dump(tree), norm)

if sys.version_info < (3, 12):
    del Python3_12TestCase

if __name__ == '__main__':
    unittest.main()