-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
check-kwargs.test
262 lines (234 loc) · 7.77 KB
/
check-kwargs.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
-- Test cases for keyword arguments.
[case testTypeErrorInKeywordArgument]
import typing
def f(o: object) -> None: pass
f(o=None()) # E: None not callable
[case testSimpleKeywordArgument]
import typing
def f(a: 'A') -> None: pass
f(a=A())
f(a=object()) # E: Argument 1 to "f" has incompatible type "object"; expected "A"
class A: pass
[case testTwoKeywordArgumentsNotInOrder]
import typing
def f(a: 'A', b: 'B') -> None: pass
f(b=A(), a=A()) # E: Argument 1 to "f" has incompatible type "A"; expected "A"
f(b=B(), a=B()) # E: Argument 2 to "f" has incompatible type "B"; expected "B"
f(a=A(), b=B())
f(b=B(), a=A())
class A: pass
class B: pass
[case testOneOfSeveralOptionalKeywordArguments]
import typing
def f(a: 'A' = None, b: 'B' = None, c: 'C' = None) -> None: pass
f(a=A())
f(b=B())
f(c=C())
f(b=B(), c=C())
f(a=B()) # E: Argument 1 to "f" has incompatible type "B"; expected "A"
f(b=A()) # E: Argument 1 to "f" has incompatible type "A"; expected "A"
f(c=B()) # E: Argument 1 to "f" has incompatible type "B"; expected "A"
f(b=B(), c=A()) # E: Argument 2 to "f" has incompatible type "A"; expected "B"
class A: pass
class B: pass
class C: pass
[case testBothPositionalAndKeywordArguments]
import typing
def f(a: 'A', b: 'B') -> None: pass
f(A(), b=A()) # E: Argument 2 to "f" has incompatible type "A"; expected "B"
f(A(), b=B())
class A: pass
class B: pass
[case testContextSensitiveTypeInferenceForKeywordArg]
from typing import List
def f(a: 'A', b: 'List[A]') -> None: pass
f(b=[], a=A())
class A: pass
[builtins fixtures/list.py]
[case testGivingSameKeywordArgumentTwice]
import typing
def f(a: 'A', b: 'B') -> None: pass
f(a=A(), b=B(), a=A()) # E: "f" gets multiple values for keyword argument "a"
f(b=B(), a=A(), b=B()) # E: "f" gets multiple values for keyword argument "b"
class A: pass
class B: pass
[case testGivingArgumentAsPositionalAndKeywordArg]
import typing
def f(a: 'A', b: 'B' = None) -> None: pass
f(A(), a=A()) # E: "f" gets multiple values for keyword argument "a"
class A: pass
class B: pass
[case testGivingArgumentAsPositionalAndKeywordArg2]
import typing
def f(a: 'A' = None, b: 'B' = None) -> None: pass
f(A(), a=A()) # E: "f" gets multiple values for keyword argument "a"
class A: pass
class B: pass
[case testInvalidKeywordArgument]
import typing
def f(a: 'A') -> None: pass
f(b=object()) # E: Unexpected keyword argument "b" for "f"
class A: pass
[case testKeywordArgumentsWithDynamicallyTypedCallable]
from typing import Undefined, Any
f = Undefined # type: Any
f(x=f(), z=None()) # E: None not callable
f(f, zz=None()) # E: None not callable
f(x=None)
[case testDuplicateKeywordArgumentWithDynamicallyTypedCallable]
from typing import Undefined, Any
f = Undefined # type: Any
# Postpone checking to runtime
f(x=None, x=None)
[case testKeywordArgumentWithFunctionObject]
from typing import Undefined, Function
f = Undefined # type: Function[[A, B], None]
f(a=A(), b=B())
f(A(), b=B())
class A: pass
class B: pass
[out]
main, line 3: Unexpected keyword argument "a"
main, line 3: Unexpected keyword argument "b"
main, line 4: Unexpected keyword argument "b"
[case testKeywordOnlyArguments]
import typing
def f(a: 'A', *, b: 'B' = None) -> None: pass
f(A(), b=B())
f(b=B(), a=A())
f(A())
f(A(), B()) # E: Too many positional arguments for "f"
class A: pass
class B: pass
[case testKeywordArgAfterVarArgs]
import typing
def f(*a: 'A', b: 'B' = None,) -> None: pass
f()
f(A())
f(A(), A())
f(b=B())
f(A(), b=B())
f(A(), A(), b=B())
f(B()) # E: Argument 1 to "f" has incompatible type "B"; expected "A"
f(A(), B()) # E: Argument 2 to "f" has incompatible type "B"; expected "B"
f(b=A()) # E: Argument 1 to "f" has incompatible type "A"; expected "A"
class A: pass
class B: pass
[builtins fixtures/list.py]
[case testKeywordArgAfterVarArgsWithBothCallerAndCalleeVarArgs]
from typing import Undefined, List
def f(*a: 'A', b: 'B' = None) -> None: pass
a = Undefined # type: List[A]
f(*a)
f(A(), *a)
f(b=B())
f(*a, b=B())
f(A(), *a, b=B())
f(A(), B()) # E: Argument 2 to "f" has incompatible type "B"; expected "B"
f(A(), b=A()) # E: Argument 2 to "f" has incompatible type "A"; expected "B"
f(*a, b=A()) # E: Argument 2 to "f" has incompatible type "A"; expected "B"
class A: pass
class B: pass
[builtins fixtures/list.py]
[case testCallingDynamicallyTypedFunctionWithKeywordArgs]
import typing
def f(x, y=A()): pass
f(x=A(), y=A())
f(y=A(), x=A())
f(y=A()) # E: Missing positional argument "x" in call to "f"
f(A(), z=A()) # E: Unexpected keyword argument "z" for "f"
class A: pass
[case testKwargsArgumentInFunctionBody]
from typing import Dict, Any
def f( **kwargs: 'A') -> None:
d1 = kwargs # type: Dict[str, A]
d2 = kwargs # type: Dict[A, Any] # E: Incompatible types in assignment (expression has type Dict[str, A], variable has type Dict[A, Any])
d3 = kwargs # type: Dict[Any, str] # E: Incompatible types in assignment (expression has type Dict[str, A], variable has type Dict[Any, str])
class A: pass
[builtins fixtures/dict.py]
[out]
main: In function "f":
[case testKwargsArgumentInFunctionBodyWithImplicitAny]
from typing import Dict, Any
def f(**kwargs) -> None:
d1 = kwargs # type: Dict[str, A]
d2 = kwargs # type: Dict[str, str]
d3 = kwargs # type: Dict[A, Any] # E: Incompatible types in assignment (expression has type Dict[str, Any], variable has type Dict[A, Any])
class A: pass
[builtins fixtures/dict.py]
[out]
main: In function "f":
[case testCallingFunctionThatAcceptsVarKwargs]
import typing
def f( **kwargs: 'A') -> None: pass
f()
f(x=A())
f(y=A(), z=A())
f(x=B()) # E: Argument 1 to "f" has incompatible type "B"; expected "A"
f(A()) # E: Too many arguments for "f"
# Perhaps a better message would be "Too many *positional* arguments..."
class A: pass
class B: pass
[builtins fixtures/dict.py]
[case testCallingFunctionWithKeywordVarArgs]
from typing import Undefined, Dict
def f( **kwargs: 'A') -> None: pass
d = Undefined # type: Dict[str, A]
f(**d)
f(x=A(), **d)
d2 = Undefined # type: Dict[str, B]
f(**d2) # E: Argument 1 to "f" has incompatible type Dict[str, B]; expected "A"
f(x=A(), **d2) # E: Argument 2 to "f" has incompatible type Dict[str, B]; expected "A"
class A: pass
class B: pass
[builtins fixtures/dict.py]
[case testInvalidTypeForKeywordVarArg]
from typing import Undefined, Dict
def f( **kwargs: 'A') -> None: pass
d = Undefined # type: Dict[A, A]
f(**d) # E: Keywords must be strings
f(**A()) # E: Argument after ** must be a dictionary
class A: pass
[builtins fixtures/dict.py]
[case testPassingKeywordVarArgsToNonVarArgsFunction]
from typing import Undefined, Any, Dict
def f(a: 'A', b: 'B') -> None: pass
d = Undefined # type: Dict[str, Any]
f(**d)
d2 = Undefined # type: Dict[str, A]
f(**d2) # E: Argument 1 to "f" has incompatible type Dict[str, A]; expected "A"
class A: pass
class B: pass
[builtins fixtures/dict.py]
[case testBothKindsOfVarArgs]
from typing import Undefined, Any, List, Dict
def f(a: 'A', b: 'A') -> None: pass
l = Undefined # type: List[Any]
d = Undefined # type: Dict[Any, Any]
f(*l, **d)
class A: pass
[builtins fixtures/dict.py]
[case testKeywordArgumentAndCommentSignature]
import typing
def f(x): # type: (int) -> str
pass
f(x='') # E: Argument 1 to "f" has incompatible type "str"; expected "int"
f(x=0)
f(y=0) # E: Unexpected keyword argument "y" for "f"
[case testKeywordArgumentAndCommentSignature2]
import typing
class A:
def f(self, x): # type: (int) -> str
pass
A().f(x='') # E: Argument 1 to "f" of "A" has incompatible type "str"; expected "int"
A().f(x=0)
A().f(y=0) # E: Unexpected keyword argument "y" for "f" of "A"
[case testKeywordVarArgsAndCommentSignature]
import typing
def f(**kwargs): # type: (**int) -> None
pass
f(z=1)
f(x=1, y=1)
f(x='', y=1) # E: Argument 1 to "f" has incompatible type "str"; expected "int"
f(x=1, y='') # E: Argument 2 to "f" has incompatible type "str"; expected "int"
[builtins fixtures/dict.py]