Skip to content

Commit 837052e

Browse files
authored
Add function definition notes for missing_named_argument errors (#20794)
This PR helps improve error messages for call argument errors by adding a note `"foo" is defined in "bar"` to indicates where that called function is defined Currently we add it for this error: - missing named arguments In later PRs, I will add this note to the following errors in separate iterations: - too few arguments - too many arguments - too many positional arguments
1 parent 2171b6f commit 837052e

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

mypy/messages.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,7 @@ def too_few_arguments(
941941
def missing_named_argument(self, callee: CallableType, context: Context, name: str) -> None:
942942
msg = f'Missing named argument "{name}"' + for_function(callee)
943943
self.fail(msg, context, code=codes.CALL_ARG)
944+
self.note_defined_here(callee, context)
944945

945946
def too_many_arguments(self, callee: CallableType, context: Context) -> None:
946947
if self.prefer_simple_messages():
@@ -1011,6 +1012,9 @@ def unexpected_keyword_argument(
10111012
self.unexpected_keyword_argument_for_function(
10121013
for_function(callee), name, context, matches=matches
10131014
)
1015+
self.note_defined_here(callee, context)
1016+
1017+
def note_defined_here(self, callee: CallableType, context: Context) -> None:
10141018
module = find_defining_module(self.modules, callee)
10151019
if (
10161020
module

test-data/unit/check-kwargs.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,24 @@ tmp/m.py:1: error: Unsupported operand types for + ("int" and "str")
480480
main:2: error: Unexpected keyword argument "x" for "A"
481481
main:2: note: "A" defined in "m"
482482

483+
[case testMissingNamedArgumentFromOtherModule]
484+
import m
485+
m.f(1)
486+
m.f(a=1)
487+
[file m.py]
488+
def f(a: int, *, b: str) -> None:
489+
pass
490+
[out]
491+
main:2: error: Missing named argument "b" for "f"
492+
main:2: note: "f" defined in "m"
493+
main:3: error: Missing named argument "b" for "f"
494+
main:3: note: "f" defined in "m"
495+
496+
[case testMissingNamedArgumentForSameModule]
497+
def f(a: int, *, b: str) -> None:
498+
pass
499+
f(1) # E: Missing named argument "b" for "f"
500+
483501
[case testStarArgsAndKwArgsSpecialCase]
484502
from typing import Dict, Mapping
485503

0 commit comments

Comments
 (0)