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
|
[case testIncorrectDispatchArgumentWhenDoesntMatchFallback]
from functools import singledispatch
class A: pass
class B(A): pass
@singledispatch
def fun(arg: A) -> None:
pass
@fun.register
def fun_b(arg: B) -> None:
pass
fun(1) # E: Argument 1 to "fun" has incompatible type "int"; expected "A"
# probably won't be required after singledispatch is special cased
[builtins fixtures/args.pyi]
[case testMultipleUnderscoreFunctionsIsntError]
from functools import singledispatch
@singledispatch
def fun(arg) -> None:
pass
@fun.register
def _(arg: str) -> None:
pass
@fun.register
def _(arg: int) -> None:
pass
[builtins fixtures/args.pyi]
[case testCheckNonDispatchArgumentsWithTypeAlwaysTheSame]
from functools import singledispatch
class A: pass
class B(A): pass
@singledispatch
def f(arg: A, arg2: str) -> None:
pass
@f.register
def g(arg: B, arg2: str) -> None:
pass
f(A(), 'a')
f(A(), 5) # E: Argument 2 to "f" has incompatible type "int"; expected "str"
f(B(), 'a')
f(B(), 1) # E: Argument 2 to "f" has incompatible type "int"; expected "str"
[builtins fixtures/args.pyi]
[case testImplementationHasSameDispatchTypeAsFallback-xfail]
from functools import singledispatch
# TODO: differentiate between fallback and other implementations in error message
@singledispatch
def f(arg: int) -> None: # E: singledispatch implementation 1 will never be used: implementation 2's dispatch type is the same
pass
@f.register
def g(arg: int) -> None:
pass
[builtins fixtures/args.pyi]
[case testRegisterHasDifferentTypeThanTypeSignature-xfail]
from functools import singledispatch
@singledispatch
def f(arg) -> None:
pass
@f.register(str)
def g(arg: int) -> None: # E: Argument to register "str" is incompatible with type "int" in function signature
pass
[builtins fixtures/args.pyi]
[case testTypePassedAsArgumentToRegister]
from functools import singledispatch
@singledispatch
def f(arg: int) -> None:
pass
@f.register(str)
def g(arg) -> None: # E: Dispatch type "str" must be subtype of fallback function first argument "int"
pass
[builtins fixtures/args.pyi]
[case testCustomClassPassedAsTypeToRegister]
from functools import singledispatch
class A: pass
@singledispatch
def f(arg: int) -> None:
pass
@f.register(A)
def g(arg) -> None: # E: Dispatch type "A" must be subtype of fallback function first argument "int"
pass
[builtins fixtures/args.pyi]
[case testMultiplePossibleImplementationsForKnownType]
from functools import singledispatch
from typing import Union
class A: pass
class B(A): pass
class C: pass
@singledispatch
def f(arg: Union[A, C]) -> None:
pass
@f.register
def g(arg: B) -> None:
pass
@f.register
def h(arg: C) -> None:
pass
x: Union[B, C]
f(x)
[builtins fixtures/args.pyi]
[case testOnePartOfUnionDoesNotHaveCorrespondingImplementation]
from functools import singledispatch
from typing import Union
class A: pass
class B(A): pass
class C: pass
@singledispatch
def f(arg: Union[A, C]) -> None:
pass
@f.register
def g(arg: B) -> None:
pass
@f.register
def h(arg: C) -> None:
pass
x: Union[B, C, int]
f(x) # E: Argument 1 to "f" has incompatible type "Union[B, C, int]"; expected "Union[A, C]"
[builtins fixtures/args.pyi]
[case testABCAllowedAsDispatchType]
from functools import singledispatch
from collections.abc import Mapping
@singledispatch
def f(arg) -> None:
pass
@f.register
def g(arg: Mapping) -> None:
pass
[builtins fixtures/dict.pyi]
[case testIncorrectArgumentsInSingledispatchFunctionDefinition]
from functools import singledispatch
@singledispatch
def f() -> None: # E: Singledispatch function requires at least one argument
pass
@singledispatch
def g(**kwargs) -> None: # E: First argument to singledispatch function must be a positional argument
pass
@singledispatch
def h(*, x) -> None: # E: First argument to singledispatch function must be a positional argument
pass
@singledispatch
def i(*, x=1) -> None: # E: First argument to singledispatch function must be a positional argument
pass
[builtins fixtures/args.pyi]
[case testDispatchTypeIsNotASubtypeOfFallbackFirstArgument]
from functools import singledispatch
class A: pass
class B(A): pass
class C: pass
@singledispatch
def f(arg: A) -> None:
pass
@f.register
def g(arg: B) -> None:
pass
@f.register
def h(arg: C) -> None: # E: Dispatch type "C" must be subtype of fallback function first argument "A"
pass
[builtins fixtures/args.pyi]
[case testMultipleSingledispatchFunctionsIntermixed]
from functools import singledispatch
class A: pass
class B(A): pass
class C: pass
@singledispatch
def f(arg: A) -> None:
pass
@singledispatch
def h(arg: C) -> None:
pass
@f.register
def g(arg: B) -> None:
pass
[builtins fixtures/args.pyi]
[case testAnyInConstructorArgsWithClassPassedToRegister]
from functools import singledispatch
from typing import Any
class Base: pass
class ConstExpr:
def __init__(self, **kwargs: Any) -> None: pass
@singledispatch
def f(arg: Base) -> ConstExpr:
pass
@f.register(ConstExpr)
def g(arg: ConstExpr) -> ConstExpr: # E: Dispatch type "ConstExpr" must be subtype of fallback function first argument "Base"
pass
[builtins fixtures/args.pyi]
[case testRegisteredImplementationUsedBeforeDefinition]
from functools import singledispatch
from typing import Union
class Node: pass
class MypyFile(Node): pass
class Missing: pass
@singledispatch
def f(a: Union[Node, Missing]) -> None:
pass
@f.register
def g(a: MypyFile) -> None:
x: Missing
f(x)
@f.register
def h(a: Missing) -> None:
pass
[builtins fixtures/args.pyi]
[case testIncorrectArgumentTypeWhenCallingRegisteredImplDirectly]
from functools import singledispatch
@singledispatch
def f(arg, arg2: str) -> bool:
return False
@f.register
def g(arg: int, arg2: str) -> bool:
pass
@f.register(str)
def h(arg, arg2: str) -> bool:
pass
g('a', 'a') # E: Argument 1 to "g" has incompatible type "str"; expected "int"
g(1, 1) # E: Argument 2 to "g" has incompatible type "int"; expected "str"
# don't show errors for incorrect first argument here, because there's no type annotation for the
# first argument
h(1, 'a')
h('a', 1) # E: Argument 2 to "h" has incompatible type "int"; expected "str"
[builtins fixtures/args.pyi]
[case testDontCrashWhenRegisteringAfterError]
import functools
a = functools.singledispatch('a') # E: Need type annotation for "a" # E: Argument 1 to "singledispatch" has incompatible type "str"; expected "Callable[..., Never]"
@a.register(int)
def default(val) -> int:
return 3
[builtins fixtures/args.pyi]
|