Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions mypy/errorcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,13 @@ def __hash__(self) -> int:
# This is a catch-all for remaining uncategorized errors.
MISC: Final = ErrorCode("misc", "Miscellaneous other checks", "General")

CALL_ARG_MISC: Final = ErrorCode(
"call-arg", "Check number, names and kinds of arguments in calls", "General", sub_code_of=MISC
)
# CALL_ARG_MISC reuses the "call-arg" code string, so keep CALL_ARG as the canonical
# code that "call-arg" resolves to in the registry.
error_codes[CALL_ARG.code] = CALL_ARG

OVERLOAD_CANNOT_MATCH: Final = ErrorCode(
"overload-cannot-match",
"Warn if an @overload signature can never be matched",
Expand Down
9 changes: 6 additions & 3 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -970,8 +970,9 @@ def too_many_positional_arguments(self, callee: CallableType, context: Context)
msg = "Too many positional arguments"
else:
msg = "Too many positional arguments" + for_function(callee)
self.fail(msg, context)
self.fail(msg, context, code=codes.CALL_ARG_MISC)
self.maybe_note_about_special_args(callee, context)
self.note_defined_here(callee, context, code=codes.CALL_ARG_MISC)

def maybe_note_about_special_args(self, callee: CallableType, context: Context) -> None:
if self.prefer_simple_messages():
Expand Down Expand Up @@ -1018,7 +1019,9 @@ def unexpected_keyword_argument(
)
self.note_defined_here(callee, context)

def note_defined_here(self, callee: CallableType, context: Context) -> None:
def note_defined_here(
self, callee: CallableType, context: Context, code: ErrorCode = codes.CALL_ARG
) -> None:
module = find_defining_module(self.modules, callee)
if (
module
Expand All @@ -1031,7 +1034,7 @@ def note_defined_here(self, callee: CallableType, context: Context) -> None:
fname = "Called function"
else:
fname = fname.split(" of ")[0] # use short method names in the note
self.note(f'{fname} defined in "{module.fullname}"', context, code=codes.CALL_ARG)
self.note(f'{fname} defined in "{module.fullname}"', context, code=code)

def duplicate_argument_value(self, callee: CallableType, index: int, context: Context) -> None:
self.fail(
Expand Down
2 changes: 1 addition & 1 deletion mypy/test/testtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def test_typeddict_type_constructor_signature(self) -> None:
assert closed.is_closed

with self.assertRaises(TypeError):
TypedDictType( # type: ignore[misc]
TypedDictType( # type: ignore[call-arg]
{"x": self.fx.o}, {"x"}, set(), self.fx.a, 10, 20, True
)

Expand Down
11 changes: 11 additions & 0 deletions test-data/unit/check-errorcodes.test
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,17 @@ def h(x: int, y: int, z: int) -> None: pass
h(y=1, z=1) # E: Missing positional argument "x" in call to "h" [call-arg]
h(y=1) # E: Missing positional arguments "x", "z" in call to "h" [call-arg]

[case testTooManyPositionalArgumentsErrorCode]
def f(a: int, *, b: int) -> None: pass
f(1, 2) # E: Too many positional arguments for "f" [call-arg]
f(1, 2) # type: ignore[call-arg]
f(1, 2) # type: ignore[misc]

[case testTooManyPositionalArgumentsCoveredByMiscUnused]
# flags: --warn-unused-ignores
def f(a: int, *, b: int) -> None: pass
f(1, 2) # type: ignore[misc] # E: Unused "type: ignore" comment, use narrower [call-arg] instead of [misc] code [unused-ignore]

[case testErrorCodeArgType]
def f(x: int) -> None: pass
f('') # E: Argument 1 to "f" has incompatible type "str"; expected "int" [arg-type]
Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/check-kwargs.test
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,21 @@ 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

Expand Down
1 change: 1 addition & 0 deletions test-data/unit/check-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ main:5: error: Argument "rename" to "namedtuple" has incompatible type "str"; ex
main:6: error: Unexpected keyword argument "unrecognized_arg" for "namedtuple"
main:6: note: "namedtuple" defined in "collections"
main:7: error: Too many positional arguments for "namedtuple"
main:7: note: "namedtuple" defined in "collections"

[case testNamedTupleDefaults]
from collections import namedtuple
Expand Down
1 change: 1 addition & 0 deletions test-data/unit/check-type-aliases.test
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,7 @@ reveal_type(t3) # N: Revealed type is "Any"

T4 = TypeAliasType("T4") # E: Missing positional argument "value" in call to "TypeAliasType"
T5 = TypeAliasType("T5", int, str) # E: Too many positional arguments for "TypeAliasType" \
# N: "TypeAliasType" defined in "typing_extensions" \
# E: Argument 3 to "TypeAliasType" has incompatible type "type[str]"; expected "tuple[TypeVar? | ParamSpec? | TypeVarTuple?, ...]"
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-full.pyi]
Expand Down
Loading