Skip to content

Commit 3602a95

Browse files
patrickw276gvanrossum
authored andcommitted
Use argument and return type line numbers for error messages (#3395)
Fixes #3177
1 parent 38453c0 commit 3602a95

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

mypy/fastparse.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,8 @@ def do_func_def(self, n: Union[ast3.FunctionDef, ast3.AsyncFunctionDef],
343343
return_type = AnyType()
344344
else:
345345
arg_types = [a.type_annotation for a in args]
346-
return_type = TypeConverter(self.errors, line=n.lineno).visit(n.returns)
346+
return_type = TypeConverter(self.errors, line=n.returns.lineno
347+
if n.returns else n.lineno).visit(n.returns)
347348

348349
for arg, arg_type in zip(args, arg_types):
349350
self.set_type_optional(arg_type, arg.initializer)
@@ -410,7 +411,7 @@ def make_argument(arg: ast3.arg, default: Optional[ast3.expr], kind: int) -> Arg
410411
self.fail(messages.DUPLICATE_TYPE_SIGNATURES, arg.lineno, arg.col_offset)
411412
arg_type = None
412413
if arg.annotation is not None:
413-
arg_type = TypeConverter(self.errors, line=line).visit(arg.annotation)
414+
arg_type = TypeConverter(self.errors, line=arg.lineno).visit(arg.annotation)
414415
elif arg.type_comment is not None:
415416
arg_type = parse_type_comment(arg.type_comment, arg.lineno, self.errors)
416417
return Argument(Var(arg.arg), arg_type, self.visit(default), kind)

test-data/unit/check-functions.test

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2044,3 +2044,26 @@ bad = make_list() # E: Need type annotation for variable
20442044
def foo(__b: int, x: int, y: int) -> int: pass
20452045
foo(x=2, y=2) # E: Missing positional argument
20462046
foo(y=2) # E: Missing positional arguments
2047+
2048+
[case testReturnTypeLineNumberWithDecorator]
2049+
def dec(f): pass
2050+
2051+
@dec
2052+
def test(a: str) -> (str,): # E: Invalid tuple literal type
2053+
return None
2054+
2055+
[case testReturnTypeLineNumberNewLine]
2056+
def fn(a: str
2057+
) -> badtype: # E: Name 'badtype' is not defined
2058+
pass
2059+
2060+
[case testArgumentTypeLineNumberWithDecorator]
2061+
def dec(f): pass
2062+
2063+
@dec
2064+
def some_method(self: badtype): pass # E: Name 'badtype' is not defined
2065+
2066+
[case TestArgumentTypeLineNumberNewline]
2067+
def fn(
2068+
a: badtype) -> None: # E: Name 'badtype' is not defined
2069+
pass

test-data/unit/semanal-errors.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ from typing import overload
8787
class A: pass
8888
@overload
8989
def f(): pass
90-
@overload # E: "A" expects no type arguments, but 1 given
91-
def f(x: A[int]) -> None: pass
90+
@overload
91+
def f(x: A[int]) -> None: pass # E: "A" expects no type arguments, but 1 given
9292
def f(*args): pass
9393
[out]
9494

0 commit comments

Comments
 (0)