| 1 |
-- Test cases for keyword arguments. |
| 2 |
|
| 3 |
|
| 4 |
[case testTypeErrorInKeywordArgument] |
| 5 |
import typing |
| 6 |
def f(o: object) -> None: pass |
| 7 |
f(o=None()) # E: "None" not callable |
| 8 |
|
| 9 |
[case testSimpleKeywordArgument] |
| 10 |
import typing |
| 11 |
class A: pass |
| 12 |
def f(a: 'A') -> None: pass |
| 13 |
f(a=A()) |
| 14 |
f(a=object()) # E: Argument "a" to "f" has incompatible type "object"; expected "A" |
| 15 |
|
| 16 |
[case testTwoKeywordArgumentsNotInOrder] |
| 17 |
import typing |
| 18 |
class A: pass |
| 19 |
class B: pass |
| 20 |
def f(a: 'A', b: 'B') -> None: pass |
| 21 |
f(b=A(), a=A()) # E: Argument "b" to "f" has incompatible type "A"; expected "B" |
| 22 |
f(b=B(), a=B()) # E: Argument "a" to "f" has incompatible type "B"; expected "A" |
| 23 |
f(a=A(), b=B()) |
| 24 |
f(b=B(), a=A()) |
| 25 |
|
| 26 |
[case testOneOfSeveralOptionalKeywordArguments] |
| 27 |
# flags: --implicit-optional |
| 28 |
import typing |
| 29 |
class A: pass |
| 30 |
class B: pass |
| 31 |
class C: pass |
| 32 |
def f(a: 'A' = None, b: 'B' = None, c: 'C' = None) -> None: pass |
| 33 |
f(a=A()) |
| 34 |
f(b=B()) |
| 35 |
f(c=C()) |
| 36 |
f(b=B(), c=C()) |
| 37 |
f(a=B()) # E: Argument "a" to "f" has incompatible type "B"; expected "A | None" |
| 38 |
f(b=A()) # E: Argument "b" to "f" has incompatible type "A"; expected "B | None" |
| 39 |
f(c=B()) # E: Argument "c" to "f" has incompatible type "B"; expected "C | None" |
| 40 |
f(b=B(), c=A()) # E: Argument "c" to "f" has incompatible type "A"; expected "C | None" |
| 41 |
[case testBothPositionalAndKeywordArguments] |
| 42 |
import typing |
| 43 |
class A: pass |
| 44 |
class B: pass |
| 45 |
def f(a: 'A', b: 'B') -> None: pass |
| 46 |
f(A(), b=A()) # E: Argument "b" to "f" has incompatible type "A"; expected "B" |
| 47 |
f(A(), b=B()) |
| 48 |
|
| 49 |
[case testContextSensitiveTypeInferenceForKeywordArg] |
| 50 |
from typing import List |
| 51 |
class A: pass |
| 52 |
def f(a: 'A', b: 'List[A]') -> None: pass |
| 53 |
f(b=[], a=A()) |
| 54 |
[builtins fixtures/list.pyi] |
| 55 |
|
| 56 |
[case testGivingArgumentAsPositionalAndKeywordArg] |
| 57 |
# flags: --no-strict-optional |
| 58 |
import typing |
| 59 |
class A: pass |
| 60 |
class B: pass |
| 61 |
def f(a: 'A', b: 'B' = None) -> None: pass |
| 62 |
f(A(), a=A()) # E: "f" gets multiple values for keyword argument "a" |
| 63 |
|
| 64 |
[case testGivingArgumentAsPositionalAndKeywordArg2] |
| 65 |
# flags: --no-strict-optional |
| 66 |
import typing |
| 67 |
class A: pass |
| 68 |
class B: pass |
| 69 |
def f(a: 'A' = None, b: 'B' = None) -> None: pass |
| 70 |
f(A(), a=A()) # E: "f" gets multiple values for keyword argument "a" |
| 71 |
|
| 72 |
[case testPositionalAndKeywordForSameArg] |
| 73 |
# This used to crash in check_argument_count(). See #1095. |
| 74 |
def f(a: int): pass |
| 75 |
def g(): f(0, a=1) |
| 76 |
[out] |
| 77 |
|
| 78 |
[case testInvalidKeywordArgument] |
| 79 |
import typing |
| 80 |
def f(a: 'A') -> None: pass |
| 81 |
f(b=object()) # E: Unexpected keyword argument "b" for "f" |
| 82 |
class A: pass |
| 83 |
|
| 84 |
[case testKeywordMisspelling] |
| 85 |
class A: pass |
| 86 |
def f(other: 'A') -> None: pass |
| 87 |
f(otter=A()) # E: Unexpected keyword argument "otter" for "f"; did you mean "other"? |
| 88 |
|
| 89 |
[case testMultipleKeywordsForMisspelling] |
| 90 |
class A: pass |
| 91 |
class B: pass |
| 92 |
def f(thing : 'A', other: 'A', atter: 'A', btter: 'B') -> None: pass |
| 93 |
f(otter=A()) # E: Unexpected keyword argument "otter" for "f"; did you mean "atter" or "other"? |
| 94 |
|
| 95 |
[case testKeywordMisspellingDifferentType] |
| 96 |
class A: pass |
| 97 |
class B: pass |
| 98 |
def f(other: 'A') -> None: pass |
| 99 |
f(otter=B()) # E: Unexpected keyword argument "otter" for "f"; did you mean "other"? |
| 100 |
|
| 101 |
[case testKeywordMisspellingInheritance] |
| 102 |
class A: pass |
| 103 |
class B(A): pass |
| 104 |
class C: pass |
| 105 |
def f(atter: 'A', btter: 'B', ctter: 'C') -> None: pass |
| 106 |
f(otter=B()) # E: Unexpected keyword argument "otter" for "f"; did you mean "atter" or "btter"? |
| 107 |
|
| 108 |
[case testKeywordMisspellingFloatInt] |
| 109 |
def f(atter: float, btter: int) -> None: pass |
| 110 |
x: int = 5 |
| 111 |
f(otter=x) # E: Unexpected keyword argument "otter" for "f"; did you mean "atter" or "btter"? |
| 112 |
|
| 113 |
[case testKeywordMisspellingVarArgs] |
| 114 |
class A: pass |
| 115 |
def f(other: 'A', *atter: 'A') -> None: pass |
| 116 |
f(otter=A()) # E: Unexpected keyword argument "otter" for "f"; did you mean "other"? |
| 117 |
[builtins fixtures/tuple.pyi] |
| 118 |
|
| 119 |
[case testKeywordMisspellingOnlyVarArgs] |
| 120 |
class A: pass |
| 121 |
def f(*other: 'A') -> None: pass |
| 122 |
f(otter=A()) # E: Unexpected keyword argument "otter" for "f" |
| 123 |
[builtins fixtures/tuple.pyi] |
| 124 |
|
| 125 |
[case testKeywordMisspellingVarArgsDifferentTypes] |
| 126 |
class A: pass |
| 127 |
class B: pass |
| 128 |
def f(other: 'B', *atter: 'A') -> None: pass |
| 129 |
f(otter=A()) # E: Unexpected keyword argument "otter" for "f"; did you mean "other"? |
| 130 |
[builtins fixtures/tuple.pyi] |
| 131 |
|
| 132 |
[case testKeywordMisspellingVarKwargs] |
| 133 |
class A: pass |
| 134 |
def f(other: 'A', **atter: 'A') -> None: pass |
| 135 |
f(otter=A()) # E: Missing positional argument "other" in call to "f" |
| 136 |
[builtins fixtures/dict.pyi] |
| 137 |
|
| 138 |
[case testKeywordArgumentsWithDynamicallyTypedCallable] |
| 139 |
from typing import Any |
| 140 |
f: Any |
| 141 |
f(x=f(), z=None()) # E: "None" not callable |
| 142 |
f(f, zz=None()) # E: "None" not callable |
| 143 |
f(x=None) |
| 144 |
|
| 145 |
[case testKeywordArgumentWithFunctionObject] |
| 146 |
from typing import Callable |
| 147 |
class A: pass |
| 148 |
class B: pass |
| 149 |
f: Callable[[A, B], None] |
| 150 |
f(a=A(), b=B()) # E: Unexpected keyword argument "a" # E: Unexpected keyword argument "b" |
| 151 |
f(A(), b=B()) # E: Unexpected keyword argument "b" |
| 152 |
|
| 153 |
[case testKeywordOnlyArguments] |
| 154 |
# flags: --no-strict-optional |
| 155 |
import typing |
| 156 |
class A: pass |
| 157 |
class B: pass |
| 158 |
def f(a: 'A', *, b: 'B' = None) -> None: pass |
| 159 |
def g(a: 'A', *, b: 'B') -> None: pass |
| 160 |
def h(a: 'A', *, b: 'B', aa: 'A') -> None: pass |
| 161 |
def i(a: 'A', *, b: 'B', aa: 'A' = None) -> None: pass |
| 162 |
f(A(), b=B()) |
| 163 |
f(b=B(), a=A()) |
| 164 |
f(A()) |
| 165 |
f(A(), B()) # E: Too many positional arguments for "f" |
| 166 |
g(A(), b=B()) |
| 167 |
g(b=B(), a=A()) |
| 168 |
g(A()) # E: Missing named argument "b" for "g" |
| 169 |
g(A(), B()) # E: Too many positional arguments for "g" |
| 170 |
h(A()) # E: Missing named argument "b" for "h" # E: Missing named argument "aa" for "h" |
| 171 |
h(A(), b=B()) # E: Missing named argument "aa" for "h" |
| 172 |
h(A(), aa=A()) # E: Missing named argument "b" for "h" |
| 173 |
h(A(), b=B(), aa=A()) |
| 174 |
h(A(), aa=A(), b=B()) |
| 175 |
i(A()) # E: Missing named argument "b" for "i" |
| 176 |
i(A(), b=B()) |
| 177 |
i(A(), aa=A()) # E: Missing named argument "b" for "i" |
| 178 |
i(A(), b=B(), aa=A()) |
| 179 |
i(A(), aa=A(), b=B()) |
| 180 |
|
| 181 |
[case testKeywordOnlyArgumentsFastparse] |
| 182 |
# flags: --no-strict-optional |
| 183 |
import typing |
| 184 |
|
| 185 |
class A: pass |
| 186 |
class B: pass |
| 187 |
|
| 188 |
def f(a: 'A', *, b: 'B' = None) -> None: pass |
| 189 |
def g(a: 'A', *, b: 'B') -> None: pass |
| 190 |
def h(a: 'A', *, b: 'B', aa: 'A') -> None: pass |
| 191 |
def i(a: 'A', *, b: 'B', aa: 'A' = None) -> None: pass |
| 192 |
f(A(), b=B()) |
| 193 |
f(b=B(), a=A()) |
| 194 |
f(A()) |
| 195 |
f(A(), B()) # E: Too many positional arguments for "f" |
| 196 |
g(A(), b=B()) |
| 197 |
g(b=B(), a=A()) |
| 198 |
g(A()) # E: Missing named argument "b" for "g" |
| 199 |
g(A(), B()) # E: Too many positional arguments for "g" |
| 200 |
h(A()) # E: Missing named argument "b" for "h" # E: Missing named argument "aa" for "h" |
| 201 |
h(A(), b=B()) # E: Missing named argument "aa" for "h" |
| 202 |
h(A(), aa=A()) # E: Missing named argument "b" for "h" |
| 203 |
h(A(), b=B(), aa=A()) |
| 204 |
h(A(), aa=A(), b=B()) |
| 205 |
i(A()) # E: Missing named argument "b" for "i" |
| 206 |
i(A(), b=B()) |
| 207 |
i(A(), aa=A()) # E: Missing named argument "b" for "i" |
| 208 |
i(A(), b=B(), aa=A()) |
| 209 |
i(A(), aa=A(), b=B()) |
| 210 |
[case testKwargsAfterBareArgs] |
| 211 |
from typing import Tuple, Any |
| 212 |
def f(a, *, b=None) -> None: pass |
| 213 |
a = None # type: Any |
| 214 |
b = None # type: Any |
| 215 |
f(a, **b) |
| 216 |
|
| 217 |
[builtins fixtures/dict.pyi] |
| 218 |
|
| 219 |
[case testKeywordArgAfterVarArgs] |
| 220 |
# flags: --implicit-optional |
| 221 |
import typing |
| 222 |
class A: pass |
| 223 |
class B: pass |
| 224 |
def f(*a: 'A', b: 'B' = None) -> None: pass |
| 225 |
f() |
| 226 |
f(A()) |
| 227 |
f(A(), A()) |
| 228 |
f(b=B()) |
| 229 |
f(A(), b=B()) |
| 230 |
f(A(), A(), b=B()) |
| 231 |
f(B()) # E: Argument 1 to "f" has incompatible type "B"; expected "A" |
| 232 |
f(A(), B()) # E: Argument 2 to "f" has incompatible type "B"; expected "A" |
| 233 |
f(b=A()) # E: Argument "b" to "f" has incompatible type "A"; expected "B | None" |
| 234 |
[builtins fixtures/list.pyi] |
| 235 |
|
| 236 |
[case testKeywordArgAfterVarArgsWithBothCallerAndCalleeVarArgs] |
| 237 |
# flags: --implicit-optional --no-strict-optional |
| 238 |
from typing import List |
| 239 |
class A: pass |
| 240 |
class B: pass |
| 241 |
def f(*a: 'A', b: 'B' = None) -> None: pass |
| 242 |
a = None # type: List[A] |
| 243 |
f(*a) |
| 244 |
f(A(), *a) |
| 245 |
f(b=B()) |
| 246 |
f(*a, b=B()) |
| 247 |
f(A(), *a, b=B()) |
| 248 |
f(A(), B()) # E: Argument 2 to "f" has incompatible type "B"; expected "A" |
| 249 |
f(A(), b=A()) # E: Argument "b" to "f" has incompatible type "A"; expected "B | None" |
| 250 |
f(*a, b=A()) # E: Argument "b" to "f" has incompatible type "A"; expected "B | None" |
| 251 |
[builtins fixtures/list.pyi] |
| 252 |
|
| 253 |
[case testCallingDynamicallyTypedFunctionWithKeywordArgs] |
| 254 |
import typing |
| 255 |
class A: pass |
| 256 |
def f(x, y=A()): pass |
| 257 |
f(x=A(), y=A()) |
| 258 |
f(y=A(), x=A()) |
| 259 |
f(y=A()) # E: Missing positional argument "x" in call to "f" |
| 260 |
f(A(), z=A()) # E: Unexpected keyword argument "z" for "f" |
| 261 |
|
| 262 |
[case testKwargsArgumentInFunctionBody] |
| 263 |
from typing import Dict, Any |
| 264 |
def f( **kwargs: 'A') -> None: |
| 265 |
d1 = kwargs # type: Dict[str, A] |
| 266 |
d2 = kwargs # type: Dict[A, Any] # E: Incompatible types in assignment (expression has type "dict[str, A]", variable has type "dict[A, Any]") |
| 267 |
d3 = kwargs # type: Dict[Any, str] # E: Incompatible types in assignment (expression has type "dict[str, A]", variable has type "dict[Any, str]") |
| 268 |
class A: pass |
| 269 |
[builtins fixtures/dict.pyi] |
| 270 |
[out] |
| 271 |
|
| 272 |
[case testKwargsArgumentInFunctionBodyWithImplicitAny] |
| 273 |
from typing import Dict, Any |
| 274 |
def f(**kwargs) -> None: |
| 275 |
d1 = kwargs # type: Dict[str, A] |
| 276 |
d2 = kwargs # type: Dict[str, str] |
| 277 |
d3 = kwargs # type: Dict[A, Any] # E: Incompatible types in assignment (expression has type "dict[str, Any]", variable has type "dict[A, Any]") |
| 278 |
class A: pass |
| 279 |
[builtins fixtures/dict.pyi] |
| 280 |
[out] |
| 281 |
|
| 282 |
[case testCallingFunctionThatAcceptsVarKwargs] |
| 283 |
import typing |
| 284 |
class A: pass |
| 285 |
class B: pass |
| 286 |
def f( **kwargs: 'A') -> None: pass |
| 287 |
f() |
| 288 |
f(x=A()) |
| 289 |
f(y=A(), z=A()) |
| 290 |
f(x=B()) # E: Argument "x" to "f" has incompatible type "B"; expected "A" |
| 291 |
f(A()) # E: Too many arguments for "f" |
| 292 |
# Perhaps a better message would be "Too many *positional* arguments..." |
| 293 |
[builtins fixtures/dict.pyi] |
| 294 |
|
| 295 |
[case testCallingFunctionWithKeywordVarArgs] |
| 296 |
from typing import Dict |
| 297 |
class A: pass |
| 298 |
class B: pass |
| 299 |
def f( **kwargs: 'A') -> None: pass |
| 300 |
d: Dict[str, A] |
| 301 |
f(**d) |
| 302 |
f(x=A(), **d) |
| 303 |
d2: Dict[str, B] |
| 304 |
f(**d2) # E: Argument 1 to "f" has incompatible type "**dict[str, B]"; expected "A" |
| 305 |
f(x=A(), **d2) # E: Argument 2 to "f" has incompatible type "**dict[str, B]"; expected "A" |
| 306 |
f(**{'x': B()}) # E: Argument 1 to "f" has incompatible type "**dict[str, B]"; expected "A" |
| 307 |
[builtins fixtures/dict.pyi] |
| 308 |
|
| 309 |
[case testKwargsAllowedInDunderCall] |
| 310 |
class Formatter: |
| 311 |
def __call__(self, message: str, bold: bool = False) -> str: |
| 312 |
pass |
| 313 |
|
| 314 |
formatter = Formatter() |
| 315 |
formatter("test", bold=True) |
| 316 |
reveal_type(formatter.__call__) # N: Revealed type is "def (message: builtins.str, bold: builtins.bool =) -> builtins.str" |
| 317 |
[builtins fixtures/bool.pyi] |
| 318 |
[out] |
| 319 |
|
| 320 |
[case testKwargsAllowedInDunderCallKwOnly] |
| 321 |
class Formatter: |
| 322 |
def __call__(self, message: str, *, bold: bool = False) -> str: |
| 323 |
pass |
| 324 |
|
| 325 |
formatter = Formatter() |
| 326 |
formatter("test", bold=True) |
| 327 |
reveal_type(formatter.__call__) # N: Revealed type is "def (message: builtins.str, *, bold: builtins.bool =) -> builtins.str" |
| 328 |
[builtins fixtures/bool.pyi] |
| 329 |
[out] |
| 330 |
|
| 331 |
[case testPassingMappingForKeywordVarArg] |
| 332 |
from typing import Mapping |
| 333 |
def f(**kwargs: 'A') -> None: pass |
| 334 |
b: Mapping |
| 335 |
d: Mapping[A, A] |
| 336 |
m: Mapping[str, A] |
| 337 |
f(**d) # E: Argument after ** must have string keys |
| 338 |
f(**m) |
| 339 |
f(**b) |
| 340 |
class A: pass |
| 341 |
[builtins fixtures/dict.pyi] |
| 342 |
|
| 343 |
[case testPassingMappingSubclassForKeywordVarArg] |
| 344 |
from typing import Mapping |
| 345 |
class MappingSubclass(Mapping[str, str]): pass |
| 346 |
def f(**kwargs: 'A') -> None: pass |
| 347 |
d: MappingSubclass |
| 348 |
f(**d) # E: Argument 1 to "f" has incompatible type "**MappingSubclass"; expected "A" |
| 349 |
class A: pass |
| 350 |
[builtins fixtures/dict.pyi] |
| 351 |
|
| 352 |
[case testInvalidTypeForKeywordVarArg] |
| 353 |
from typing import Dict, Any, Optional |
| 354 |
class A: pass |
| 355 |
def f(**kwargs: 'A') -> None: pass |
| 356 |
d = {} # type: Dict[A, A] |
| 357 |
f(**d) # E: Argument after ** must have string keys |
| 358 |
f(**A()) # E: Argument after ** must be a mapping, not "A" |
| 359 |
kwargs: Optional[Any] |
| 360 |
f(**kwargs) # E: Argument after ** must be a mapping, not "Any | None" |
| 361 |
|
| 362 |
def g(a: int) -> None: pass |
| 363 |
g(a=1, **4) # E: Argument after ** must be a mapping, not "int" |
| 364 |
|
| 365 |
def main(f: Any) -> None: |
| 366 |
f(**3) # E: Argument after ** must be a mapping, not "int" |
| 367 |
[builtins fixtures/dict.pyi] |
| 368 |
|
| 369 |
[case testPassingKeywordVarArgsToNonVarArgsFunction] |
| 370 |
from typing import Any, Dict |
| 371 |
def f(a: 'A', b: 'B') -> None: pass |
| 372 |
d: Dict[str, Any] |
| 373 |
f(**d) |
| 374 |
d2: Dict[str, A] |
| 375 |
f(**d2) # E: Argument 1 to "f" has incompatible type "**dict[str, A]"; expected "B" |
| 376 |
class A: pass |
| 377 |
class B: pass |
| 378 |
[builtins fixtures/dict.pyi] |
| 379 |
|
| 380 |
[case testBothKindsOfVarArgs] |
| 381 |
from typing import Any, List, Dict |
| 382 |
def f(a: 'A', b: 'A') -> None: pass |
| 383 |
l: List[Any] |
| 384 |
d: Dict[Any, Any] |
| 385 |
f(*l, **d) |
| 386 |
class A: pass |
| 387 |
[builtins fixtures/dict.pyi] |
| 388 |
|
| 389 |
[case testPassingMultipleKeywordVarArgs] |
| 390 |
from typing import Any, Dict |
| 391 |
def f1(a: 'A', b: 'A') -> None: pass |
| 392 |
def f2(a: 'A') -> None: pass |
| 393 |
def f3(a: 'A', **kwargs: 'A') -> None: pass |
| 394 |
def f4(**kwargs: 'A') -> None: pass |
| 395 |
d: Dict[Any, Any] |
| 396 |
d2: Dict[Any, Any] |
| 397 |
f1(**d, **d2) |
| 398 |
f2(**d, **d2) |
| 399 |
f3(**d, **d2) |
| 400 |
f4(**d, **d2) |
| 401 |
class A: pass |
| 402 |
[builtins fixtures/dict.pyi] |
| 403 |
|
| 404 |
[case testPassingKeywordVarArgsToVarArgsOnlyFunction] |
| 405 |
from typing import Any, Dict |
| 406 |
def f(*args: 'A') -> None: pass |
| 407 |
d: Dict[Any, Any] |
| 408 |
f(**d) |
| 409 |
class A: pass |
| 410 |
[builtins fixtures/dict.pyi] |
| 411 |
|
| 412 |
[case testKeywordArgumentAndCommentSignature] |
| 413 |
import typing |
| 414 |
def f(x): # type: (int) -> str |
| 415 |
pass |
| 416 |
f(x='') # E: Argument "x" to "f" has incompatible type "str"; expected "int" |
| 417 |
f(x=0) |
| 418 |
f(y=0) # E: Unexpected keyword argument "y" for "f" |
| 419 |
|
| 420 |
[case testKeywordArgumentAndCommentSignature2] |
| 421 |
import typing |
| 422 |
class A: |
| 423 |
def f(self, x): # type: (int) -> str |
| 424 |
pass |
| 425 |
A().f(x='') # E: Argument "x" to "f" of "A" has incompatible type "str"; expected "int" |
| 426 |
A().f(x=0) |
| 427 |
A().f(y=0) # E: Unexpected keyword argument "y" for "f" of "A" |
| 428 |
|
| 429 |
[case testKeywordVarArgsAndCommentSignature] |
| 430 |
import typing |
| 431 |
def f(**kwargs): # type: (**int) -> None |
| 432 |
pass |
| 433 |
f(z=1) |
| 434 |
f(x=1, y=1) |
| 435 |
f(x='', y=1) # E: Argument "x" to "f" has incompatible type "str"; expected "int" |
| 436 |
f(x=1, y='') # E: Argument "y" to "f" has incompatible type "str"; expected "int" |
| 437 |
[builtins fixtures/dict.pyi] |
| 438 |
|
| 439 |
[case testCallsWithStars] |
| 440 |
def f(a: int) -> None: |
| 441 |
pass |
| 442 |
|
| 443 |
s = ('',) |
| 444 |
f(*s) # E: Argument 1 to "f" has incompatible type "*tuple[str]"; expected "int" |
| 445 |
|
| 446 |
a = {'': 0} |
| 447 |
f(a) # E: Argument 1 to "f" has incompatible type "dict[str, int]"; expected "int" |
| 448 |
f(**a) # okay |
| 449 |
|
| 450 |
b = {'': ''} |
| 451 |
f(b) # E: Argument 1 to "f" has incompatible type "dict[str, str]"; expected "int" |
| 452 |
f(**b) # E: Argument 1 to "f" has incompatible type "**dict[str, str]"; expected "int" |
| 453 |
|
| 454 |
c = {0: 0} |
| 455 |
f(**c) # E: Argument after ** must have string keys |
| 456 |
[builtins fixtures/dict.pyi] |
| 457 |
|
| 458 |
[case testCallStar2WithStar] |
| 459 |
def f(**k): pass |
| 460 |
f(*(1, 2)) # E: Too many arguments for "f" |
| 461 |
[builtins fixtures/dict.pyi] |
| 462 |
|
| 463 |
[case testUnexpectedMethodKwargInNestedClass] |
| 464 |
class A: |
| 465 |
class B: |
| 466 |
def __init__(self) -> None: |
| 467 |
pass |
| 468 |
A.B(x=1) # E: Unexpected keyword argument "x" for "B" |
| 469 |
|
| 470 |
[case testUnexpectedMethodKwargFromOtherModule] |
| 471 |
import m |
| 472 |
m.A(x=1) # E: Unexpected keyword argument "x" for "A" \ |
| 473 |
# N: "A" defined in "m" |
| 474 |
[file m.py] |
| 475 |
1+'asdf' # E: Unsupported operand types for + ("int" and "str") |
| 476 |
class A: |
| 477 |
def __init__(self) -> None: |
| 478 |
pass |
| 479 |
|
| 480 |
[case testMissingNamedArgumentFromOtherModule] |
| 481 |
import m |
| 482 |
m.f(1) |
| 483 |
m.f(a=1) |
| 484 |
[file m.py] |
| 485 |
def f(a: int, *, b: str) -> None: |
| 486 |
pass |
| 487 |
[out] |
| 488 |
main:2: error: Missing named argument "b" for "f" |
| 489 |
main:2: note: "f" defined in "m" |
| 490 |
main:3: error: Missing named argument "b" for "f" |
| 491 |
main:3: note: "f" defined in "m" |
| 492 |
|
| 493 |
[case testMissingNamedArgumentForSameModule] |
| 494 |
def f(a: int, *, b: str) -> None: |
| 495 |
pass |
| 496 |
f(1) # E: Missing named argument "b" for "f" |
| 497 |
|
| 498 |
[case testTooManyPositionalArgumentsFromOtherModule] |
| 499 |
import m |
| 500 |
m.f(1, 2) |
| 501 |
[file m.py] |
| 502 |
def f(a: int, *, b: int) -> None: |
| 503 |
pass |
| 504 |
[out] |
| 505 |
main:2: error: Too many positional arguments for "f" |
| 506 |
main:2: note: "f" defined in "m" |
| 507 |
|
| 508 |
[case testTooManyPositionalArgumentsForSameModule] |
| 509 |
def f(a: int, *, b: int) -> None: |
| 510 |
pass |
| 511 |
f(1, 2) # E: Too many positional arguments for "f" |
| 512 |
|
| 513 |
[case testStarArgsAndKwArgsSpecialCase] |
| 514 |
from typing import Dict, Mapping |
| 515 |
|
| 516 |
def f(*vargs: int, **kwargs: object) -> None: |
| 517 |
pass |
| 518 |
|
| 519 |
def g(arg: int = 0, **kwargs: object) -> None: |
| 520 |
pass |
| 521 |
|
| 522 |
d = {} # type: Dict[str, object] |
| 523 |
f(**d) |
| 524 |
g(**d) # E: Argument 1 to "g" has incompatible type "**dict[str, object]"; expected "int" |
| 525 |
|
| 526 |
m = {} # type: Mapping[str, object] |
| 527 |
f(**m) |
| 528 |
g(**m) # E: Argument 1 to "g" has incompatible type "**Mapping[str, object]"; expected "int" |
| 529 |
[builtins fixtures/dict.pyi] |
| 530 |
|
| 531 |
[case testPassingEmptyDictWithStars] |
| 532 |
def f(): pass |
| 533 |
def g(x=1): pass |
| 534 |
|
| 535 |
f(**{}) |
| 536 |
g(**{}) |
| 537 |
[builtins fixtures/dict.pyi] |
| 538 |
|
| 539 |
[case testKeywordUnpackWithDifferentTypes] |
| 540 |
# https://github.com/python/mypy/issues/11144 |
| 541 |
from typing import Dict, Generic, TypeVar, Mapping, Iterable |
| 542 |
|
| 543 |
T = TypeVar("T") |
| 544 |
T2 = TypeVar("T2") |
| 545 |
|
| 546 |
class A(Dict[T, T2]): |
| 547 |
... |
| 548 |
|
| 549 |
class B(Mapping[T, T2]): |
| 550 |
... |
| 551 |
|
| 552 |
class C(Generic[T, T2]): |
| 553 |
... |
| 554 |
|
| 555 |
class D: |
| 556 |
... |
| 557 |
|
| 558 |
class E: |
| 559 |
def keys(self) -> Iterable[str]: |
| 560 |
... |
| 561 |
def __getitem__(self, key: str) -> float: |
| 562 |
... |
| 563 |
|
| 564 |
def foo(**i: float) -> float: |
| 565 |
... |
| 566 |
|
| 567 |
a: A[str, str] |
| 568 |
b: B[str, str] |
| 569 |
c: C[str, float] |
| 570 |
d: D |
| 571 |
e: E |
| 572 |
f = {"a": "b"} |
| 573 |
|
| 574 |
foo(k=1.5) |
| 575 |
foo(**a) |
| 576 |
foo(**b) |
| 577 |
foo(**c) |
| 578 |
foo(**d) |
| 579 |
foo(**e) |
| 580 |
foo(**f) |
| 581 |
|
| 582 |
# Correct: |
| 583 |
|
| 584 |
class Good(Mapping[str, float]): |
| 585 |
... |
| 586 |
|
| 587 |
good1: Good |
| 588 |
good2: A[str, float] |
| 589 |
good3: B[str, float] |
| 590 |
foo(**good1) |
| 591 |
foo(**good2) |
| 592 |
foo(**good3) |
| 593 |
[out] |
| 594 |
main:36: error: Argument 1 to "foo" has incompatible type "**A[str, str]"; expected "float" |
| 595 |
main:37: error: Argument 1 to "foo" has incompatible type "**B[str, str]"; expected "float" |
| 596 |
main:38: error: Argument after ** must be a mapping, not "C[str, float]" |
| 597 |
main:39: error: Argument after ** must be a mapping, not "D" |
| 598 |
main:41: error: Argument 1 to "foo" has incompatible type "**dict[str, str]"; expected "float" |
| 599 |
[builtins fixtures/dict.pyi] |
| 600 |
|
| 601 |
[case testLiteralKwargs] |
| 602 |
from typing import Any, Literal |
| 603 |
kw: dict[Literal["a", "b"], Any] |
| 604 |
def func(a, b): ... |
| 605 |
func(**kw) |
| 606 |
|
| 607 |
badkw: dict[Literal["one", 1], Any] |
| 608 |
func(**badkw) # E: Argument after ** must have string keys |
| 609 |
[builtins fixtures/dict.pyi] |