File: check-fastparse.test

package info (click to toggle)
mypy 1.19.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,412 kB
  • sloc: python: 114,754; ansic: 13,343; cpp: 11,380; makefile: 257; sh: 28
file content (341 lines) | stat: -rw-r--r-- 9,615 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
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
[case testFastParseSyntaxError]

1 +  # E: Invalid syntax

[case testFastParseTypeCommentSyntaxError]

x = None # type: a : b  # E: Syntax error in type comment "a : b"

[case testFastParseInvalidTypeComment]

x = None # type: a + b  # E: Invalid type comment or annotation

-- Function type comments are attributed to the function def line.
-- This happens in both parsers.
[case testFastParseFunctionAnnotationSyntaxError]

def f():  # E: Syntax error in type comment "None -> None" # N: Suggestion: wrap argument types in parentheses
  # type: None -> None
  pass

[case testFastParseFunctionAnnotationSyntaxErrorSpaces]

def f():  # E: Syntax error in type comment "None -> None" # N: Suggestion: wrap argument types in parentheses
  # type:             None -> None
  pass

[case testFastParseInvalidFunctionAnnotation]

def f(x):  # E: Invalid type comment or annotation
  # type: (a + b) -> None
  pass

[case testFastParseInvalidTypes3]
# All of these should not crash
from typing import Callable, Tuple, Iterable

x: Tuple[int, str].x # E: Invalid type comment or annotation
a: Iterable[x].x # E: Invalid type comment or annotation
b: Tuple[x][x] # E: Invalid type comment or annotation
c: Iterable[x][x] # E: Invalid type comment or annotation
d: Callable[..., int][x] # E: Invalid type comment or annotation
e: Callable[..., int].x # E: Invalid type comment or annotation

f = None # type: Tuple[int, str].x # E: Invalid type comment or annotation
g = None # type: Iterable[x].x # E: Invalid type comment or annotation
h = None # type: Tuple[x][x] # E: Invalid type comment or annotation
i = None # type: Iterable[x][x] # E: Invalid type comment or annotation
j = None # type: Callable[..., int][x] # E: Invalid type comment or annotation
k = None # type: Callable[..., int].x # E: Invalid type comment or annotation

def f1(x: Tuple[int, str].x) -> None: pass # E: Invalid type comment or annotation
def f2(x: Iterable[x].x) -> None: pass # E: Invalid type comment or annotation
def f3(x: Tuple[x][x]) -> None: pass # E: Invalid type comment or annotation
def f4(x: Iterable[x][x]) -> None: pass # E: Invalid type comment or annotation
def f5(x: Callable[..., int][x]) -> None: pass # E: Invalid type comment or annotation
def f6(x: Callable[..., int].x) -> None: pass # E: Invalid type comment or annotation

[case testFastParseTypeWithIgnore]
def f(x,  # type: x  # type: ignore
      ):
    # type: (...) -> None
    pass

[case testFastParseVariableTypeWithIgnore]

x = 1 # type: str # type: ignore

[case testFastParseVariableTypeWithIgnoreNoSpace]

x = 1 # type: str #type:ignore

[case testFastParseVariableTypeWithIgnoreAndComment]

x = 1 # type: str # type: ignore # comment

[case testFastParseTypeWithIgnoreWithStmt]
with open('test', 'r') as f:  # type: int  # type: ignore
    pass

[case testFastParseTypeWithIgnoreForStmt]
for i in (1, 2, 3, 100):  # type: str # type: ignore
    pass
[builtins fixtures/tuple.pyi]

[case testFastParseVariableCommentThenIgnore]
a="test" # type: int #comment # type: ignore  # E: Incompatible types in assignment (expression has type "str", variable has type "int")

[case testFastParseProperty]

class C:
  @property
  def x(self) -> str: pass
  @x.setter
  def x(self, value: str) -> None: pass
[builtins fixtures/property.pyi]

[case testFastParseConditionalProperty]

class C:
  if bool():
    @property
    def x(self) -> str: pass
    @x.setter
    def x(self, value: str) -> None: pass
[builtins fixtures/property.pyi]

[case testFastParsePerArgumentAnnotations]
# flags: --implicit-optional

class A: pass
class B: pass
class C: pass
class D: pass
class E: pass
class F: pass
def f(a,        # type: A
      b = None, # type: B
      *args,    # type: C
      d = None, # type: D
      e,        # type: E
      **kwargs  # type: F
      ):
    reveal_type(a)      # N: Revealed type is "__main__.A"
    reveal_type(b)      # N: Revealed type is "Union[__main__.B, None]"
    reveal_type(args)   # N: Revealed type is "builtins.tuple[__main__.C, ...]"
    reveal_type(d)      # N: Revealed type is "Union[__main__.D, None]"
    reveal_type(e)      # N: Revealed type is "__main__.E"
    reveal_type(kwargs) # N: Revealed type is "builtins.dict[builtins.str, __main__.F]"
[builtins fixtures/dict.pyi]
[out]

[case testFastParsePerArgumentAnnotationsWithReturn]
# flags: --implicit-optional

class A: pass
class B: pass
class C: pass
class D: pass
class E: pass
class F: pass
def f(a,        # type: A
      b = None, # type: B
      *args,    # type: C
      d = None, # type: D
      e,        # type: E
      **kwargs  # type: F
      ):
      # type: (...) -> int
    reveal_type(a)      # N: Revealed type is "__main__.A"
    reveal_type(b)      # N: Revealed type is "Union[__main__.B, None]"
    reveal_type(args)   # N: Revealed type is "builtins.tuple[__main__.C, ...]"
    reveal_type(d)      # N: Revealed type is "Union[__main__.D, None]"
    reveal_type(e)      # N: Revealed type is "__main__.E"
    reveal_type(kwargs) # N: Revealed type is "builtins.dict[builtins.str, __main__.F]"
    return "not an int"  # E: Incompatible return value type (got "str", expected "int")
[builtins fixtures/dict.pyi]
[out]

[case testFastParsePerArgumentAnnotationsWithAnnotatedBareStar]

def f(*, # type: int  # E: Bare * has associated type comment
      x  # type: str
      ):
      # type: (...) -> int
    pass
[builtins fixtures/dict.pyi]
[out]

[case testFastParsePerArgumentAnnotationsWithReturnAndBareStar]

def f(*,
      x  # type: str
      ):
      # type: (...) -> int
    reveal_type(x) # N: Revealed type is "builtins.str"
    return "not an int"  # E: Incompatible return value type (got "str", expected "int")
[builtins fixtures/dict.pyi]
[out]

[case testFasterParseTooManyArgumentsAnnotation]
def f():  # E: Type signature has too many arguments
    # type: (int) -> None
    pass

f()
f(1) # E: Too many arguments for "f"

[case testFasterParseTooFewArgumentsAnnotation]
def f(x, y):  # E: Type signature has too few arguments
    # type: (int) -> None
    x()
    y()

f(1, 2)
f(1) # E: Missing positional argument "y" in call to "f"

[case testFasterParseTypeErrorCustom]

from typing import TypeVar, Generic
T = TypeVar('T')
class Foo(Generic[T]):
  pass

def f(a: Foo(int)) -> int:
    pass
[out]
main:7: error: Invalid type comment or annotation
main:7: note: Suggestion: use Foo[...] instead of Foo(...)

[case testFastParseMatMul]

from typing import Any
x = None  # type: Any
x @ 1
x @= 1

[case testFastParserShowsMultipleErrors]
def f(x):  # E: Type signature has too few arguments
    # type: () -> None
    pass
def g():  # E: Type signature has too many arguments
    # type: (int) -> None
    pass

[case testFastParseMalformedAssert]

assert 1, 2
assert (1, 2)  # E: Assertion is always true, perhaps remove parentheses?
assert (1, 2), 3  # E: Assertion is always true, perhaps remove parentheses?
assert (1,)  # E: Assertion is always true, perhaps remove parentheses?
assert ()
[builtins fixtures/tuple.pyi]

[case testFastParseAssertMessage]

assert 1
assert 1, 2
assert 1, 1+2
assert 1, 1+'test'  # E: Unsupported operand types for + ("int" and "str")
assert 1, f()  # E: Name "f" is not defined

[case testFastParserConsistentFunctionTypes]

def f1(x, y, z):
  # type: (int, int, int) -> int
  pass

def f2(x,  # type: int  # E: Function has duplicate type signatures
      y,  # type: int
      z   # type: int
    ):
    # type: (int, int, int) -> int
    pass

def f3(x,  # type: int
      y,  # type: int
      z   # type: int
    ):
    # type: (...) -> int
    pass

def f4(x, y, z):
  # type: (int, int, int) -> int
  pass

def f5(x) -> int:  # E: Function has duplicate type signatures
  # type: (int) -> int
  pass

def f6(x: int, y: int, z: int):
  # type: (...) -> int
  pass

def f7(x: int):  # E: Function has duplicate type signatures
  # type: (int) -> int
  pass

[case testFastParserDuplicateNames]

def f(x, y, z):
  pass

def g(x, y, x):  # E: Duplicate argument "x" in function definition
  pass

def h(x, y, *x):  # E: Duplicate argument "x" in function definition
  pass

def i(x, y, *z, **z):  # E: Duplicate argument "z" in function definition
  pass

def j(x: int, y: int, *, x: int = 3):  # E: Duplicate argument "x" in function definition
  pass

def k(*, y, z, y):  # E: Duplicate argument "y" in function definition
  pass

lambda x, y, x: ...  # E: Duplicate argument "x" in function definition

[case testNoCrashOnImportFromStar]
from pack import *
[file pack/__init__.py]
from . import *

[case testNoCrashOnImportFromStarNested]

import blamodule
[file blamodule/__init__.py]
from . import command
from . import backends

[file blamodule/backends/__init__.py]
from .Bla import Bla
Bla().method()

[file blamodule/backends/Bla.py]
from .. import *

class Bla:
    def method(self) -> str:
        return command.call()

[file blamodule/command.py]
def call() -> str: pass

[builtins fixtures/module.pyi]

[case testInvalidEscapeSequenceWarningsSuppressed]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
# Test that SyntaxWarnings for invalid escape sequences are suppressed
# when parsing potential type expressions containing regex patterns or
# similar strings. Callable arguments are always potential type expressions.
from typing import TypeForm

def identity(typx: TypeForm) -> TypeForm:
    return typx

# This should not generate SyntaxWarning despite invalid escape sequence
identity(r"re\.match")  # E: Argument 1 to "identity" has incompatible type "str"; expected "TypeForm[Any]"
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-full.pyi]