summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_tools/test_com2ann.py
blob: 2731f82ce7b43fd2ca974c2142f887a2950e517b (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
"""Tests for the com2ann.py script in the Tools/parser directory."""

import unittest
import test.support
import os
import re

from test.test_tools import basepath, toolsdir, skip_if_missing

skip_if_missing()

parser_path = os.path.join(toolsdir, "parser")

with test.support.DirsOnSysPath(parser_path):
    from com2ann import *

class BaseTestCase(unittest.TestCase):

    def check(self, code, expected, n=False, e=False):
        self.assertEqual(com2ann(code,
                         drop_None=n, drop_Ellipsis=e, silent=True),
                         expected)

class SimpleTestCase(BaseTestCase):
    # Tests for basic conversions

    def test_basics(self):
        self.check("z = 5", "z = 5")
        self.check("z: int = 5", "z: int = 5")
        self.check("z = 5 # type: int", "z: int = 5")
        self.check("z = 5 # type: int # comment",
                   "z: int = 5 # comment")

    def test_type_ignore(self):
        self.check("foobar = foobaz() #type: ignore",
                   "foobar = foobaz() #type: ignore")
        self.check("a = 42 #type: ignore #comment",
                   "a = 42 #type: ignore #comment")

    def test_complete_tuple(self):
        self.check("t = 1, 2, 3 # type: Tuple[int, ...]",
                   "t: Tuple[int, ...] = (1, 2, 3)")
        self.check("t = 1, # type: Tuple[int]",
                   "t: Tuple[int] = (1,)")
        self.check("t = (1, 2, 3) # type: Tuple[int, ...]",
                   "t: Tuple[int, ...] = (1, 2, 3)")

    def test_drop_None(self):
        self.check("x = None # type: int",
                   "x: int", True)
        self.check("x = None # type: int # another",
                   "x: int # another", True)
        self.check("x = None # type: int # None",
                   "x: int # None", True)

    def test_drop_Ellipsis(self):
        self.check("x = ... # type: int",
                   "x: int", False, True)
        self.check("x = ... # type: int # another",
                   "x: int # another", False, True)
        self.check("x = ... # type: int # ...",
                   "x: int # ...", False, True)

    def test_newline(self):
        self.check("z = 5 # type: int\r\n", "z: int = 5\r\n")
        self.check("z = 5 # type: int # comment\x85",
                   "z: int = 5 # comment\x85")

    def test_wrong(self):
        self.check("#type : str", "#type : str")
        self.check("x==y #type: bool", "x==y #type: bool")

    def test_pattern(self):
        for line in ["#type: int", "  # type:  str[:] # com"]:
            self.assertTrue(re.search(TYPE_COM, line))
        for line in ["", "#", "# comment", "#type", "type int:"]:
            self.assertFalse(re.search(TYPE_COM, line))

class BigTestCase(BaseTestCase):
    # Tests for really crazy formatting, to be sure
    # that script works reasonably in extreme situations

    def test_crazy(self):
        self.maxDiff = None
        self.check(crazy_code, big_result, False, False)
        self.check(crazy_code, big_result_ne, True, True)

crazy_code = """\
# -*- coding: utf-8 -*- # this should not be spoiled
'''
Docstring here
'''

import testmod
x = 5 #type    : int # this one is OK
ttt \\
    = \\
        1.0, \\
        2.0, \\
        3.0, #type: Tuple[float, float, float]
with foo(x==1) as f: #type: str
    print(f)

for i, j in my_inter(x=1): # type: ignore
    i + j # type: int # what about this

x = y = z = 1 # type: int
x, y, z = [], [], []  # type: (List[int], List[int], List[str])
class C:


    l[f(x
        =1)] = [

         1,
         2,
         ]  # type: List[int]


    (C.x[1]) = \\
        42 == 5# type: bool
lst[...] = \\
    ((\\
...)) # type: int # comment ..

y = ... # type: int # comment ...
z = ...
#type: int


#DONE placement of annotation after target rather than before =

TD.x[1]  \\
    = 0 == 5# type: bool

TD.y[1] =5 == 5# type: bool # one more here
F[G(x == y,

# hm...

    z)]\\
      = None # type: OMG[int] # comment: None
x = None#type:int   #comment : None"""

big_result = """\
# -*- coding: utf-8 -*- # this should not be spoiled
'''
Docstring here
'''

import testmod
x: int = 5 # this one is OK
ttt: Tuple[float, float, float] \\
    = \\
       (1.0, \\
        2.0, \\
        3.0,)
with foo(x==1) as f: #type: str
    print(f)

for i, j in my_inter(x=1): # type: ignore
    i + j # type: int # what about this

x = y = z = 1 # type: int
x, y, z = [], [], []  # type: (List[int], List[int], List[str])
class C:


    l[f(x
        =1)]: List[int] = [

         1,
         2,
         ]


    (C.x[1]): bool = \\
        42 == 5
lst[...]: int = \\
    ((\\
...)) # comment ..

y: int = ... # comment ...
z = ...
#type: int


#DONE placement of annotation after target rather than before =

TD.x[1]: bool  \\
    = 0 == 5

TD.y[1]: bool =5 == 5 # one more here
F[G(x == y,

# hm...

    z)]: OMG[int]\\
      = None # comment: None
x: int = None   #comment : None"""

big_result_ne = """\
# -*- coding: utf-8 -*- # this should not be spoiled
'''
Docstring here
'''

import testmod
x: int = 5 # this one is OK
ttt: Tuple[float, float, float] \\
    = \\
       (1.0, \\
        2.0, \\
        3.0,)
with foo(x==1) as f: #type: str
    print(f)

for i, j in my_inter(x=1): # type: ignore
    i + j # type: int # what about this

x = y = z = 1 # type: int
x, y, z = [], [], []  # type: (List[int], List[int], List[str])
class C:


    l[f(x
        =1)]: List[int] = [

         1,
         2,
         ]


    (C.x[1]): bool = \\
        42 == 5
lst[...]: int \\
    \\
 # comment ..

y: int # comment ...
z = ...
#type: int


#DONE placement of annotation after target rather than before =

TD.x[1]: bool  \\
    = 0 == 5

TD.y[1]: bool =5 == 5 # one more here
F[G(x == y,

# hm...

    z)]: OMG[int]\\
       # comment: None
x: int   #comment : None"""

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