-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
check-python2.test
444 lines (371 loc) · 11.3 KB
/
check-python2.test
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
-- Type checker test cases for Python 2.x mode.
[case testUnicode]
u = u'foo'
if int():
u = unicode()
if int():
s = ''
if int():
s = u'foo' # E: Incompatible types in assignment (expression has type "unicode", variable has type "str")
if int():
s = b'foo'
[builtins_py2 fixtures/python2.pyi]
[case testTypeVariableUnicode]
from typing import TypeVar
T = TypeVar(u'T')
[case testPrintStatement]
print ''() # E: "str" not callable
print 1, 1() # E: "int" not callable
[case testPrintStatementWithTarget]
class A:
def write(self, s):
# type: (str) -> None
pass
class B:
def write(self):
# type: () -> int
pass
print >>A(), ''
print >>None, ''
print >>1, '' # E: "int" has no attribute "write"
print >>(None + ''), None # E: Unsupported left operand type for + ("None")
print >> B(), '' # E: Argument "file" to "print" has incompatible type "def () -> builtins.int"; expected "def (builtins.str) -> Any"
[case testDivision]
class A:
def __div__(self, x):
# type: (int) -> str
pass
s = A() / 1
if int():
s = ''
if int():
s = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
[case testStrUnicodeCompatibility]
import typing
def f(x):
# type: (unicode) -> None
pass
f('')
f(u'')
f(b'')
[builtins_py2 fixtures/python2.pyi]
[case testStaticMethodWithCommentSignature]
class A:
@staticmethod
def f(x): # type: (int) -> str
return ''
A.f(1)
A.f('') # E: Argument 1 to "f" of "A" has incompatible type "str"; expected "int"
[builtins_py2 fixtures/staticmethod.pyi]
[case testRaiseTupleTypeFail]
import typing
x = None # type: typing.Type[typing.Tuple[typing.Any, typing.Any, typing.Any]]
raise x # E: Exception must be derived from BaseException
[builtins_py2 fixtures/exception.pyi]
[case testRaiseTupleOfThreeOnPython2]
from types import TracebackType
from typing import Optional, Tuple, Type
e = None # type: Optional[TracebackType]
# Correct raising several items:
raise BaseException
raise BaseException(1)
raise (BaseException,)
raise (BaseException(1),)
raise BaseException, 1
raise BaseException, 1, e
raise BaseException, 1, None
raise Exception
raise Exception(1)
raise Exception()
raise Exception(1), None
raise Exception(), None
raise Exception(1), None, None
raise Exception(1), None, e
raise (Exception,)
raise (Exception(1),)
raise Exception, 1
raise Exception, 1, e
raise Exception, 1, None
# Errors:
raise int, 1 # E: Argument 1 must be "Type[builtins.BaseException]" subtype
raise int, None # E: Argument 1 must be "Union[builtins.BaseException, Type[builtins.BaseException]]" subtype
raise Exception(1), 1 # E: Argument 1 must be "Type[builtins.BaseException]" subtype
raise Exception(1), 1, None # E: Argument 1 must be "Type[builtins.BaseException]" subtype
raise Exception, 1, 1 # E: Argument 3 must be "Union[types.TracebackType, None]" subtype
raise int, 1, 1 # E: Argument 1 must be "Type[builtins.BaseException]" subtype \
# E: Argument 3 must be "Union[types.TracebackType, None]" subtype
# Correct raising tuple:
t1 = (BaseException,)
t2 = (Exception(1), 2, 3, 4) # type: Tuple[Exception, int, int, int]
t3 = (Exception,) # type: Tuple[Type[Exception], ...]
t4 = (Exception(1),) # type: Tuple[Exception, ...]
raise t1
raise t2
raise t3
raise t4
# Errors:
raise t1, 1, None # E: Argument 1 must be "Type[builtins.BaseException]" subtype
raise t2, 1 # E: Argument 1 must be "Type[builtins.BaseException]" subtype
raise t3, 1, e # E: Argument 1 must be "Type[builtins.BaseException]" subtype
raise t4, 1, 1 # E: Argument 1 must be "Type[builtins.BaseException]" subtype \
# E: Argument 3 must be "Union[types.TracebackType, None]" subtype
w1 = ()
w2 = (1, Exception)
w3 = (1,) # type: Tuple[int, ...]
raise w1 # E: Exception must be derived from BaseException
raise w2 # E: When raising a tuple, first element must by derived from BaseException
raise w3 # E: When raising a tuple, first element must by derived from BaseException
# Bare raise:
try:
pass
except Exception:
raise # ok
[builtins_py2 fixtures/exception.pyi]
[file types.pyi]
class TracebackType: pass
[case testTryExceptWithTuple]
try:
None
except BaseException, e:
e() # E: "BaseException" not callable
[builtins_py2 fixtures/exception.pyi]
[case testTryExceptUnsupported]
try:
pass
except BaseException, (e, f): # E: Sorry, "except <expr>, <anything but a name>" is not supported
pass
try:
pass
except BaseException, [e, f, g]: # E: Sorry, "except <expr>, <anything but a name>" is not supported
pass
try:
pass
except BaseException, e[0]: # E: Sorry, "except <expr>, <anything but a name>" is not supported
pass
[builtins_py2 fixtures/exception.pyi]
[case testAlternateNameSuggestions]
class Foo(object):
def say_hello(self):
pass
def say_hell(self):
pass
def say_hullo(self):
pass
def say_goodbye(self):
pass
def go_away(self):
pass
def go_around(self):
pass
def append(self):
pass
def extend(self):
pass
def _add(self):
pass
f = Foo()
f.say_hallo() # E: "Foo" has no attribute "say_hallo"; maybe "say_hullo", "say_hello", or "say_hell"?
f.go_array() # E: "Foo" has no attribute "go_array"; maybe "go_away"?
f.add() # E: "Foo" has no attribute "add"; maybe "append", "extend", or "_add"?
[case testTupleArgListDynamicallyTyped]
def f(x, (y, z)):
x = y + z
f(1, 1)
f(1, (1, 2))
[case testTupleArgListAnnotated]
from typing import Tuple
def f(x, (y, z)): # type: (object, Tuple[int, str]) -> None
x() # E
y() # E
z() # E
f(object(), (1, ''))
f(1, 1) # E
[builtins_py2 fixtures/tuple.pyi]
[out]
main:3: error: "object" not callable
main:4: error: "int" not callable
main:5: error: "str" not callable
main:7: error: Argument 2 to "f" has incompatible type "int"; expected "Tuple[int, str]"
[case testNestedTupleArgListAnnotated]
from typing import Tuple
def f(x, (y, (a, b))): # type: (object, Tuple[int, Tuple[str, int]]) -> None
x() # E
y() # E
a() # E
b() # E
f(object(), (1, ('', 2)))
f(1, 1) # E
[builtins fixtures/tuple.pyi]
[out]
main:3: error: "object" not callable
main:4: error: "int" not callable
main:5: error: "str" not callable
main:6: error: "int" not callable
main:8: error: Argument 2 to "f" has incompatible type "int"; expected "Tuple[int, Tuple[str, int]]"
[case testBackquoteExpr]
`1`.x # E: "str" has no attribute "x"
[case testImportFromPython2Builtin]
from __builtin__ import int as i
x = 1 # type: i
y = '' # type: i # E: Incompatible types in assignment (expression has type "str", variable has type "int")
[case testImportPython2Builtin]
import __builtin__
x = 1 # type: __builtin__.int
y = '' # type: __builtin__.int # E: Incompatible types in assignment (expression has type "str", variable has type "int")
[case testImportAsPython2Builtin]
import __builtin__ as bi
x = 1 # type: bi.int
y = '' # type: bi.int # E: Incompatible types in assignment (expression has type "str", variable has type "int")
[case testImportFromPython2BuiltinOverridingDefault]
from __builtin__ import int
x = 1 # type: int
y = '' # type: int # E: Incompatible types in assignment (expression has type "str", variable has type "int")
-- Copied from check-functions.test
[case testEllipsisWithArbitraryArgsOnBareFunctionInPython2]
def f(x, y, z): # type: (...) -> None
pass
-- Copied from check-functions.test
[case testEllipsisWithSomethingAfterItFailsInPython2]
def f(x, y, z): # type: (..., int) -> None
pass
[out]
main:1: error: Ellipses cannot accompany other argument types in function type signature
[case testLambdaTupleArgInPython2]
f = lambda (x, y): x + y
f((0, 0))
def g(): # type: () -> None
pass
reveal_type(lambda (x,): g()) # N: Revealed type is "def (Any)"
[out]
[case testLambdaTupleArgInferenceInPython2]
from typing import Callable, Tuple
def f(c):
# type: (Callable[[Tuple[int, int]], int]) -> None
pass
def g(c):
# type: (Callable[[Tuple[int, int]], str]) -> None
pass
f(lambda (x, y): y)
f(lambda (x, y): x()) # E: "int" not callable
g(lambda (x, y): y) # E: Argument 1 to "g" has incompatible type "Callable[[Tuple[int, int]], int]"; expected "Callable[[Tuple[int, int]], str]" \
# E: Incompatible return value type (got "int", expected "str")
[out]
[case testLambdaSingletonTupleArgInPython2]
f = lambda (x,): x + 1
f((0,))
[out]
[case testLambdaNoTupleArgInPython2]
f = lambda (x): x + 1
f(0)
[out]
[case testDefTupleEdgeCasesPython2]
def f((x,)): return x
def g((x)): return x
f(0) + g(0)
[out]
[case testLambdaAsSortKeyForTuplePython2]
from typing import Any, Tuple, Callable
def bar(key):
# type: (Callable[[Tuple[int, int]], int]) -> int
pass
def foo():
# type: () -> int
return bar(key=lambda (a, b): a)
[out]
[case testImportBuiltins]
import __builtin__
__builtin__.str
[case testUnicodeAlias]
from typing import List
Alias = List[u'Foo']
class Foo: pass
[builtins_py2 fixtures/python2.pyi]
[case testExec]
exec('print 1 + 1')
[case testUnicodeDocStrings]
# flags: --python-version=2.7
__doc__ = u"unicode"
class A:
u"unicode"
def f():
# type: () -> None
u"unicode"
[case testMetaclassBasics]
class M(type):
x = 0 # type: int
def test(cls):
# type: () -> str
return "test"
class A(object):
__metaclass__ = M
reveal_type(A.x) # N: Revealed type is "builtins.int"
reveal_type(A.test()) # N: Revealed type is "builtins.str"
[case testImportedMetaclass]
import m
class A(object):
__metaclass__ = m.M
reveal_type(A.x) # N: Revealed type is "builtins.int"
reveal_type(A.test()) # N: Revealed type is "builtins.str"
[file m.py]
class M(type):
x = 0
def test(cls):
# type: () -> str
return "test"
[case testDynamicMetaclass]
class C(object):
__metaclass__ = int() # E: Dynamic metaclass not supported for "C"
[case testMetaclassDefinedAsClass]
class C(object):
class __metaclass__: pass # E: Metaclasses defined as inner classes are not supported
[case testErrorInMetaclass]
x = 0
class A(object):
__metaclass__ = m.M # E: Name "m" is not defined
class B(object):
__metaclass__ = M # E: Name "M" is not defined
[case testMetaclassAndSkippedImportInPython2]
# flags: --ignore-missing-imports
from missing import M
class A(object):
__metaclass__ = M
y = 0
reveal_type(A.y) # N: Revealed type is "builtins.int"
A.x # E: "Type[A]" has no attribute "x"
[case testAnyAsBaseOfMetaclass]
from typing import Any, Type
M = None # type: Any
class MM(M): pass
class A(object):
__metaclass__ = MM
[case testSelfTypeNotSelfType2]
class A:
def g(self):
# type: (None) -> None
pass
[out]
main:2: error: Invalid type for self, or extra argument type in function annotation
main:2: note: (Hint: typically annotations omit the type for self)
[case testSuper]
class A:
def f(self): # type: () -> None
pass
class B(A):
def g(self): # type: () -> None
super(B, self).f()
super().f() # E: Too few arguments for "super"
[case testPartialTypeComments_python2]
def foo(
a, # type: str
b,
args=None,
):
# type: (...) -> None
pass
[case testNoneHasNoBoolInPython2]
none = None
b = none.__bool__() # E: "None" has no attribute "__bool__"
[case testDictWithoutTypeCommentInPython2]
# flags: --py2
d = dict() # E: Need type comment for "d" (hint: "d = ... \# type: Dict[<type>, <type>]")
[builtins_py2 fixtures/floatdict_python2.pyi]