Debian Codegraph @

test-data/unit/check-singledispatch.test

raw
1 [case testIncorrectDispatchArgumentWhenDoesntMatchFallback]
2 from functools import singledispatch
3
4 class A: pass
5 class B(A): pass
6
7 @singledispatch
8 def fun(arg: A) -> None:
9 pass
10 @fun.register
11 def fun_b(arg: B) -> None:
12 pass
13
14 fun(1) # E: Argument 1 to "fun" has incompatible type "int"; expected "A"
15
16 # probably won't be required after singledispatch is special cased
17 [builtins fixtures/args.pyi]
18
19 [case testMultipleUnderscoreFunctionsIsntError]
20 from functools import singledispatch
21
22 @singledispatch
23 def fun(arg) -> None:
24 pass
25 @fun.register
26 def _(arg: str) -> None:
27 pass
28 @fun.register
29 def _(arg: int) -> None:
30 pass
31
32 [builtins fixtures/args.pyi]
33
34 [case testCheckNonDispatchArgumentsWithTypeAlwaysTheSame]
35 from functools import singledispatch
36
37 class A: pass
38 class B(A): pass
39
40 @singledispatch
41 def f(arg: A, arg2: str) -> None:
42 pass
43
44 @f.register
45 def g(arg: B, arg2: str) -> None:
46 pass
47
48 f(A(), 'a')
49 f(A(), 5) # E: Argument 2 to "f" has incompatible type "int"; expected "str"
50
51 f(B(), 'a')
52 f(B(), 1) # E: Argument 2 to "f" has incompatible type "int"; expected "str"
53
54 [builtins fixtures/args.pyi]
55
56 [case testImplementationHasSameDispatchTypeAsFallback-xfail]
57 from functools import singledispatch
58
59 # TODO: differentiate between fallback and other implementations in error message
60 @singledispatch
61 def f(arg: int) -> None: # E: singledispatch implementation 1 will never be used: implementation 2's dispatch type is the same
62 pass
63
64 @f.register
65 def g(arg: int) -> None:
66 pass
67
68 [builtins fixtures/args.pyi]
69
70 [case testRegisterHasDifferentTypeThanTypeSignature-xfail]
71 from functools import singledispatch
72
73 @singledispatch
74 def f(arg) -> None:
75 pass
76
77 @f.register(str)
78 def g(arg: int) -> None: # E: Argument to register "str" is incompatible with type "int" in function signature
79 pass
80
81 [builtins fixtures/args.pyi]
82
83 [case testTypePassedAsArgumentToRegister]
84 from functools import singledispatch
85
86 @singledispatch
87 def f(arg: int) -> None:
88 pass
89 @f.register(str)
90 def g(arg) -> None: # E: Dispatch type "str" must be subtype of fallback function first argument "int"
91 pass
92
93 [builtins fixtures/args.pyi]
94
95 [case testCustomClassPassedAsTypeToRegister]
96 from functools import singledispatch
97 class A: pass
98
99 @singledispatch
100 def f(arg: int) -> None:
101 pass
102 @f.register(A)
103 def g(arg) -> None: # E: Dispatch type "A" must be subtype of fallback function first argument "int"
104 pass
105
106 [builtins fixtures/args.pyi]
107
108 [case testMultiplePossibleImplementationsForKnownType]
109 from functools import singledispatch
110 from typing import Union
111
112 class A: pass
113 class B(A): pass
114 class C: pass
115
116 @singledispatch
117 def f(arg: Union[A, C]) -> None:
118 pass
119
120 @f.register
121 def g(arg: B) -> None:
122 pass
123
124 @f.register
125 def h(arg: C) -> None:
126 pass
127
128 x: Union[B, C]
129 f(x)
130
131 [builtins fixtures/args.pyi]
132
133 [case testOnePartOfUnionDoesNotHaveCorrespondingImplementation]
134 from functools import singledispatch
135 from typing import Union
136
137 class A: pass
138 class B(A): pass
139 class C: pass
140
141 @singledispatch
142 def f(arg: Union[A, C]) -> None:
143 pass
144
145 @f.register
146 def g(arg: B) -> None:
147 pass
148
149 @f.register
150 def h(arg: C) -> None:
151 pass
152
153 x: Union[B, C, int]
154 f(x) # E: Argument 1 to "f" has incompatible type "B | C | int"; expected "A | C"
155
156 [builtins fixtures/args.pyi]
157
158 [case testABCAllowedAsDispatchType]
159 from functools import singledispatch
160 from collections.abc import Mapping
161
162 @singledispatch
163 def f(arg) -> None:
164 pass
165
166 @f.register
167 def g(arg: Mapping) -> None:
168 pass
169 [builtins fixtures/dict.pyi]
170
171 [case testIncorrectArgumentsInSingledispatchFunctionDefinition]
172 from functools import singledispatch
173
174 @singledispatch
175 def f() -> None: # E: Singledispatch function requires at least one argument
176 pass
177
178 @singledispatch
179 def g(**kwargs) -> None: # E: First argument to singledispatch function must be a positional argument
180 pass
181
182 @singledispatch
183 def h(*, x) -> None: # E: First argument to singledispatch function must be a positional argument
184 pass
185
186 @singledispatch
187 def i(*, x=1) -> None: # E: First argument to singledispatch function must be a positional argument
188 pass
189
190 [builtins fixtures/args.pyi]
191
192 [case testDispatchTypeIsNotASubtypeOfFallbackFirstArgument]
193 from functools import singledispatch
194
195 class A: pass
196 class B(A): pass
197 class C: pass
198
199 @singledispatch
200 def f(arg: A) -> None:
201 pass
202
203 @f.register
204 def g(arg: B) -> None:
205 pass
206
207 @f.register
208 def h(arg: C) -> None: # E: Dispatch type "C" must be subtype of fallback function first argument "A"
209 pass
210
211 [builtins fixtures/args.pyi]
212
213 [case testMultipleSingledispatchFunctionsIntermixed]
214 from functools import singledispatch
215
216 class A: pass
217 class B(A): pass
218 class C: pass
219
220 @singledispatch
221 def f(arg: A) -> None:
222 pass
223
224 @singledispatch
225 def h(arg: C) -> None:
226 pass
227
228 @f.register
229 def g(arg: B) -> None:
230 pass
231
232 [builtins fixtures/args.pyi]
233
234 [case testAnyInConstructorArgsWithClassPassedToRegister]
235 from functools import singledispatch
236 from typing import Any
237
238 class Base: pass
239 class ConstExpr:
240 def __init__(self, **kwargs: Any) -> None: pass
241
242 @singledispatch
243 def f(arg: Base) -> ConstExpr:
244 pass
245
246 @f.register(ConstExpr)
247 def g(arg: ConstExpr) -> ConstExpr: # E: Dispatch type "ConstExpr" must be subtype of fallback function first argument "Base"
248 pass
249
250
251 [builtins fixtures/args.pyi]
252
253 [case testRegisteredImplementationUsedBeforeDefinition]
254 from functools import singledispatch
255 from typing import Union
256
257 class Node: pass
258 class MypyFile(Node): pass
259 class Missing: pass
260
261 @singledispatch
262 def f(a: Union[Node, Missing]) -> None:
263 pass
264
265 @f.register
266 def g(a: MypyFile) -> None:
267 x: Missing
268 f(x)
269
270 @f.register
271 def h(a: Missing) -> None:
272 pass
273
274 [builtins fixtures/args.pyi]
275
276 [case testIncorrectArgumentTypeWhenCallingRegisteredImplDirectly]
277 from functools import singledispatch
278
279 @singledispatch
280 def f(arg, arg2: str) -> bool:
281 return False
282
283 @f.register
284 def g(arg: int, arg2: str) -> bool:
285 pass
286
287 @f.register(str)
288 def h(arg, arg2: str) -> bool:
289 pass
290
291 g('a', 'a') # E: Argument 1 to "g" has incompatible type "str"; expected "int"
292 g(1, 1) # E: Argument 2 to "g" has incompatible type "int"; expected "str"
293
294 # don't show errors for incorrect first argument here, because there's no type annotation for the
295 # first argument
296 h(1, 'a')
297 h('a', 1) # E: Argument 2 to "h" has incompatible type "int"; expected "str"
298
299 [builtins fixtures/args.pyi]
300
301 [case testDontCrashWhenRegisteringAfterError]
302 import functools
303 a = functools.singledispatch('a') # E: Need type annotation for "a" # E: Argument 1 to "singledispatch" has incompatible type "str"; expected "Callable[..., Never]"
304
305 @a.register(int)
306 def default(val) -> int:
307 return 3
308
309 [builtins fixtures/args.pyi]