-- 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 class A: pass def f(a: 'A') -> None: pass f(a=A()) f(a=object()) # E: Argument "a" to "f" has incompatible type "object"; expected "A" [case testTwoKeywordArgumentsNotInOrder] import typing class A: pass class B: pass def f(a: 'A', b: 'B') -> None: pass f(b=A(), a=A()) # E: Argument "b" to "f" has incompatible type "A"; expected "B" f(b=B(), a=B()) # E: Argument "a" to "f" has incompatible type "B"; expected "A" f(a=A(), b=B()) f(b=B(), a=A()) [case testOneOfSeveralOptionalKeywordArguments] # flags: --implicit-optional import typing class A: pass class B: pass class C: pass 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 "a" to "f" has incompatible type "B"; expected "A | None" f(b=A()) # E: Argument "b" to "f" has incompatible type "A"; expected "B | None" f(c=B()) # E: Argument "c" to "f" has incompatible type "B"; expected "C | None" f(b=B(), c=A()) # E: Argument "c" to "f" has incompatible type "A"; expected "C | None" [case testBothPositionalAndKeywordArguments] import typing class A: pass class B: pass def f(a: 'A', b: 'B') -> None: pass f(A(), b=A()) # E: Argument "b" to "f" has incompatible type "A"; expected "B" f(A(), b=B()) [case testContextSensitiveTypeInferenceForKeywordArg] from typing import List class A: pass def f(a: 'A', b: 'List[A]') -> None: pass f(b=[], a=A()) [builtins fixtures/list.pyi] [case testGivingArgumentAsPositionalAndKeywordArg] # flags: --no-strict-optional import typing class A: pass class B: pass def f(a: 'A', b: 'B' = None) -> None: pass f(A(), a=A()) # E: "f" gets multiple values for keyword argument "a" [case testGivingArgumentAsPositionalAndKeywordArg2] # flags: --no-strict-optional import typing class A: pass class B: pass def f(a: 'A' = None, b: 'B' = None) -> None: pass f(A(), a=A()) # E: "f" gets multiple values for keyword argument "a" [case testPositionalAndKeywordForSameArg] # This used to crash in check_argument_count(). See #1095. def f(a: int): pass def g(): f(0, a=1) [out] [case testInvalidKeywordArgument] import typing def f(a: 'A') -> None: pass f(b=object()) # E: Unexpected keyword argument "b" for "f" class A: pass [case testKeywordMisspelling] class A: pass def f(other: 'A') -> None: pass f(otter=A()) # E: Unexpected keyword argument "otter" for "f"; did you mean "other"? [case testMultipleKeywordsForMisspelling] class A: pass class B: pass def f(thing : 'A', other: 'A', atter: 'A', btter: 'B') -> None: pass f(otter=A()) # E: Unexpected keyword argument "otter" for "f"; did you mean "atter" or "other"? [case testKeywordMisspellingDifferentType] class A: pass class B: pass def f(other: 'A') -> None: pass f(otter=B()) # E: Unexpected keyword argument "otter" for "f"; did you mean "other"? [case testKeywordMisspellingInheritance] class A: pass class B(A): pass class C: pass def f(atter: 'A', btter: 'B', ctter: 'C') -> None: pass f(otter=B()) # E: Unexpected keyword argument "otter" for "f"; did you mean "atter" or "btter"? [case testKeywordMisspellingFloatInt] def f(atter: float, btter: int) -> None: pass x: int = 5 f(otter=x) # E: Unexpected keyword argument "otter" for "f"; did you mean "atter" or "btter"? [case testKeywordMisspellingVarArgs] class A: pass def f(other: 'A', *atter: 'A') -> None: pass f(otter=A()) # E: Unexpected keyword argument "otter" for "f"; did you mean "other"? [builtins fixtures/tuple.pyi] [case testKeywordMisspellingOnlyVarArgs] class A: pass def f(*other: 'A') -> None: pass f(otter=A()) # E: Unexpected keyword argument "otter" for "f" [builtins fixtures/tuple.pyi] [case testKeywordMisspellingVarArgsDifferentTypes] class A: pass class B: pass def f(other: 'B', *atter: 'A') -> None: pass f(otter=A()) # E: Unexpected keyword argument "otter" for "f"; did you mean "other"? [builtins fixtures/tuple.pyi] [case testKeywordMisspellingVarKwargs] class A: pass def f(other: 'A', **atter: 'A') -> None: pass f(otter=A()) # E: Missing positional argument "other" in call to "f" [builtins fixtures/dict.pyi] [case testKeywordArgumentsWithDynamicallyTypedCallable] from typing import Any f: Any f(x=f(), z=None()) # E: "None" not callable f(f, zz=None()) # E: "None" not callable f(x=None) [case testKeywordArgumentWithFunctionObject] from typing import Callable class A: pass class B: pass f: Callable[[A, B], None] f(a=A(), b=B()) # E: Unexpected keyword argument "a" # E: Unexpected keyword argument "b" f(A(), b=B()) # E: Unexpected keyword argument "b" [case testKeywordOnlyArguments] # flags: --no-strict-optional import typing class A: pass class B: pass def f(a: 'A', *, b: 'B' = None) -> None: pass def g(a: 'A', *, b: 'B') -> None: pass def h(a: 'A', *, b: 'B', aa: 'A') -> None: pass def i(a: 'A', *, b: 'B', aa: 'A' = None) -> None: pass f(A(), b=B()) f(b=B(), a=A()) f(A()) f(A(), B()) # E: Too many positional arguments for "f" g(A(), b=B()) g(b=B(), a=A()) g(A()) # E: Missing named argument "b" for "g" g(A(), B()) # E: Too many positional arguments for "g" h(A()) # E: Missing named argument "b" for "h" # E: Missing named argument "aa" for "h" h(A(), b=B()) # E: Missing named argument "aa" for "h" h(A(), aa=A()) # E: Missing named argument "b" for "h" h(A(), b=B(), aa=A()) h(A(), aa=A(), b=B()) i(A()) # E: Missing named argument "b" for "i" i(A(), b=B()) i(A(), aa=A()) # E: Missing named argument "b" for "i" i(A(), b=B(), aa=A()) i(A(), aa=A(), b=B()) [case testKeywordOnlyArgumentsFastparse] # flags: --no-strict-optional import typing class A: pass class B: pass def f(a: 'A', *, b: 'B' = None) -> None: pass def g(a: 'A', *, b: 'B') -> None: pass def h(a: 'A', *, b: 'B', aa: 'A') -> None: pass def i(a: 'A', *, b: 'B', aa: 'A' = None) -> None: pass f(A(), b=B()) f(b=B(), a=A()) f(A()) f(A(), B()) # E: Too many positional arguments for "f" g(A(), b=B()) g(b=B(), a=A()) g(A()) # E: Missing named argument "b" for "g" g(A(), B()) # E: Too many positional arguments for "g" h(A()) # E: Missing named argument "b" for "h" # E: Missing named argument "aa" for "h" h(A(), b=B()) # E: Missing named argument "aa" for "h" h(A(), aa=A()) # E: Missing named argument "b" for "h" h(A(), b=B(), aa=A()) h(A(), aa=A(), b=B()) i(A()) # E: Missing named argument "b" for "i" i(A(), b=B()) i(A(), aa=A()) # E: Missing named argument "b" for "i" i(A(), b=B(), aa=A()) i(A(), aa=A(), b=B()) [case testKwargsAfterBareArgs] from typing import Tuple, Any def f(a, *, b=None) -> None: pass a = None # type: Any b = None # type: Any f(a, **b) [builtins fixtures/dict.pyi] [case testKeywordArgAfterVarArgs] # flags: --implicit-optional import typing class A: pass class B: pass 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 "A" f(b=A()) # E: Argument "b" to "f" has incompatible type "A"; expected "B | None" [builtins fixtures/list.pyi] [case testKeywordArgAfterVarArgsWithBothCallerAndCalleeVarArgs] # flags: --implicit-optional --no-strict-optional from typing import List class A: pass class B: pass def f(*a: 'A', b: 'B' = None) -> None: pass a = None # 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 "A" f(A(), b=A()) # E: Argument "b" to "f" has incompatible type "A"; expected "B | None" f(*a, b=A()) # E: Argument "b" to "f" has incompatible type "A"; expected "B | None" [builtins fixtures/list.pyi] [case testCallingDynamicallyTypedFunctionWithKeywordArgs] import typing class A: pass 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" [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.pyi] [out] [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.pyi] [out] [case testCallingFunctionThatAcceptsVarKwargs] import typing class A: pass class B: pass def f( **kwargs: 'A') -> None: pass f() f(x=A()) f(y=A(), z=A()) f(x=B()) # E: Argument "x" 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..." [builtins fixtures/dict.pyi] [case testCallingFunctionWithKeywordVarArgs] from typing import Dict class A: pass class B: pass def f( **kwargs: 'A') -> None: pass d: Dict[str, A] f(**d) f(x=A(), **d) d2: 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" f(**{'x': B()}) # E: Argument 1 to "f" has incompatible type "**dict[str, B]"; expected "A" [builtins fixtures/dict.pyi] [case testKwargsAllowedInDunderCall] class Formatter: def __call__(self, message: str, bold: bool = False) -> str: pass formatter = Formatter() formatter("test", bold=True) reveal_type(formatter.__call__) # N: Revealed type is "def (message: builtins.str, bold: builtins.bool =) -> builtins.str" [builtins fixtures/bool.pyi] [out] [case testKwargsAllowedInDunderCallKwOnly] class Formatter: def __call__(self, message: str, *, bold: bool = False) -> str: pass formatter = Formatter() formatter("test", bold=True) reveal_type(formatter.__call__) # N: Revealed type is "def (message: builtins.str, *, bold: builtins.bool =) -> builtins.str" [builtins fixtures/bool.pyi] [out] [case testPassingMappingForKeywordVarArg] from typing import Mapping def f(**kwargs: 'A') -> None: pass b: Mapping d: Mapping[A, A] m: Mapping[str, A] f(**d) # E: Argument after ** must have string keys f(**m) f(**b) class A: pass [builtins fixtures/dict.pyi] [case testPassingMappingSubclassForKeywordVarArg] from typing import Mapping class MappingSubclass(Mapping[str, str]): pass def f(**kwargs: 'A') -> None: pass d: MappingSubclass f(**d) # E: Argument 1 to "f" has incompatible type "**MappingSubclass"; expected "A" class A: pass [builtins fixtures/dict.pyi] [case testInvalidTypeForKeywordVarArg] from typing import Dict, Any, Optional class A: pass def f(**kwargs: 'A') -> None: pass d = {} # type: Dict[A, A] f(**d) # E: Argument after ** must have string keys f(**A()) # E: Argument after ** must be a mapping, not "A" kwargs: Optional[Any] f(**kwargs) # E: Argument after ** must be a mapping, not "Any | None" def g(a: int) -> None: pass g(a=1, **4) # E: Argument after ** must be a mapping, not "int" def main(f: Any) -> None: f(**3) # E: Argument after ** must be a mapping, not "int" [builtins fixtures/dict.pyi] [case testPassingKeywordVarArgsToNonVarArgsFunction] from typing import Any, Dict def f(a: 'A', b: 'B') -> None: pass d: Dict[str, Any] f(**d) d2: Dict[str, A] f(**d2) # E: Argument 1 to "f" has incompatible type "**dict[str, A]"; expected "B" class A: pass class B: pass [builtins fixtures/dict.pyi] [case testBothKindsOfVarArgs] from typing import Any, List, Dict def f(a: 'A', b: 'A') -> None: pass l: List[Any] d: Dict[Any, Any] f(*l, **d) class A: pass [builtins fixtures/dict.pyi] [case testPassingMultipleKeywordVarArgs] from typing import Any, Dict def f1(a: 'A', b: 'A') -> None: pass def f2(a: 'A') -> None: pass def f3(a: 'A', **kwargs: 'A') -> None: pass def f4(**kwargs: 'A') -> None: pass d: Dict[Any, Any] d2: Dict[Any, Any] f1(**d, **d2) f2(**d, **d2) f3(**d, **d2) f4(**d, **d2) class A: pass [builtins fixtures/dict.pyi] [case testPassingKeywordVarArgsToVarArgsOnlyFunction] from typing import Any, Dict def f(*args: 'A') -> None: pass d: Dict[Any, Any] f(**d) class A: pass [builtins fixtures/dict.pyi] [case testKeywordArgumentAndCommentSignature] import typing def f(x): # type: (int) -> str pass f(x='') # E: Argument "x" 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 "x" 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 "x" to "f" has incompatible type "str"; expected "int" f(x=1, y='') # E: Argument "y" to "f" has incompatible type "str"; expected "int" [builtins fixtures/dict.pyi] [case testCallsWithStars] def f(a: int) -> None: pass s = ('',) f(*s) # E: Argument 1 to "f" has incompatible type "*tuple[str]"; expected "int" a = {'': 0} f(a) # E: Argument 1 to "f" has incompatible type "dict[str, int]"; expected "int" f(**a) # okay b = {'': ''} f(b) # E: Argument 1 to "f" has incompatible type "dict[str, str]"; expected "int" f(**b) # E: Argument 1 to "f" has incompatible type "**dict[str, str]"; expected "int" c = {0: 0} f(**c) # E: Argument after ** must have string keys [builtins fixtures/dict.pyi] [case testCallStar2WithStar] def f(**k): pass f(*(1, 2)) # E: Too many arguments for "f" [builtins fixtures/dict.pyi] [case testUnexpectedMethodKwargInNestedClass] class A: class B: def __init__(self) -> None: pass A.B(x=1) # E: Unexpected keyword argument "x" for "B" [case testUnexpectedMethodKwargFromOtherModule] import m m.A(x=1) # E: Unexpected keyword argument "x" for "A" \ # N: "A" defined in "m" [file m.py] 1+'asdf' # E: Unsupported operand types for + ("int" and "str") class A: def __init__(self) -> None: pass [case testMissingNamedArgumentFromOtherModule] import m m.f(1) m.f(a=1) [file m.py] def f(a: int, *, b: str) -> None: pass [out] main:2: error: Missing named argument "b" for "f" main:2: note: "f" defined in "m" main:3: error: Missing named argument "b" for "f" main:3: note: "f" defined in "m" [case testMissingNamedArgumentForSameModule] def f(a: int, *, b: str) -> None: pass f(1) # E: Missing named argument "b" for "f" [case testTooManyPositionalArgumentsFromOtherModule] import m m.f(1, 2) [file m.py] def f(a: int, *, b: int) -> None: pass [out] main:2: error: Too many positional arguments for "f" main:2: note: "f" defined in "m" [case testTooManyPositionalArgumentsForSameModule] def f(a: int, *, b: int) -> None: pass f(1, 2) # E: Too many positional arguments for "f" [case testStarArgsAndKwArgsSpecialCase] from typing import Dict, Mapping def f(*vargs: int, **kwargs: object) -> None: pass def g(arg: int = 0, **kwargs: object) -> None: pass d = {} # type: Dict[str, object] f(**d) g(**d) # E: Argument 1 to "g" has incompatible type "**dict[str, object]"; expected "int" m = {} # type: Mapping[str, object] f(**m) g(**m) # E: Argument 1 to "g" has incompatible type "**Mapping[str, object]"; expected "int" [builtins fixtures/dict.pyi] [case testPassingEmptyDictWithStars] def f(): pass def g(x=1): pass f(**{}) g(**{}) [builtins fixtures/dict.pyi] [case testKeywordUnpackWithDifferentTypes] # https://github.com/python/mypy/issues/11144 from typing import Dict, Generic, TypeVar, Mapping, Iterable T = TypeVar("T") T2 = TypeVar("T2") class A(Dict[T, T2]): ... class B(Mapping[T, T2]): ... class C(Generic[T, T2]): ... class D: ... class E: def keys(self) -> Iterable[str]: ... def __getitem__(self, key: str) -> float: ... def foo(**i: float) -> float: ... a: A[str, str] b: B[str, str] c: C[str, float] d: D e: E f = {"a": "b"} foo(k=1.5) foo(**a) foo(**b) foo(**c) foo(**d) foo(**e) foo(**f) # Correct: class Good(Mapping[str, float]): ... good1: Good good2: A[str, float] good3: B[str, float] foo(**good1) foo(**good2) foo(**good3) [out] main:36: error: Argument 1 to "foo" has incompatible type "**A[str, str]"; expected "float" main:37: error: Argument 1 to "foo" has incompatible type "**B[str, str]"; expected "float" main:38: error: Argument after ** must be a mapping, not "C[str, float]" main:39: error: Argument after ** must be a mapping, not "D" main:41: error: Argument 1 to "foo" has incompatible type "**dict[str, str]"; expected "float" [builtins fixtures/dict.pyi] [case testLiteralKwargs] from typing import Any, Literal kw: dict[Literal["a", "b"], Any] def func(a, b): ... func(**kw) badkw: dict[Literal["one", 1], Any] func(**badkw) # E: Argument after ** must have string keys [builtins fixtures/dict.pyi]