From 6e3cd06aa1badc13c76c49f914433a41c3b46fa9 Mon Sep 17 00:00:00 2001 From: Patrick Williams Date: Mon, 22 May 2017 11:14:30 -0700 Subject: [PATCH] Use argument and return type line numbers for error messages --- mypy/fastparse.py | 5 +++-- test-data/unit/check-functions.test | 23 +++++++++++++++++++++++ test-data/unit/semanal-errors.test | 4 ++-- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/mypy/fastparse.py b/mypy/fastparse.py index fe31e4ae0fb2..bbf20c14be16 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -343,7 +343,8 @@ def do_func_def(self, n: Union[ast3.FunctionDef, ast3.AsyncFunctionDef], return_type = AnyType() else: arg_types = [a.type_annotation for a in args] - return_type = TypeConverter(self.errors, line=n.lineno).visit(n.returns) + return_type = TypeConverter(self.errors, line=n.returns.lineno + if n.returns else n.lineno).visit(n.returns) for arg, arg_type in zip(args, arg_types): 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 self.fail(messages.DUPLICATE_TYPE_SIGNATURES, arg.lineno, arg.col_offset) arg_type = None if arg.annotation is not None: - arg_type = TypeConverter(self.errors, line=line).visit(arg.annotation) + arg_type = TypeConverter(self.errors, line=arg.lineno).visit(arg.annotation) elif arg.type_comment is not None: arg_type = parse_type_comment(arg.type_comment, arg.lineno, self.errors) return Argument(Var(arg.arg), arg_type, self.visit(default), kind) diff --git a/test-data/unit/check-functions.test b/test-data/unit/check-functions.test index 8ac35e052659..e4a155a748ed 100644 --- a/test-data/unit/check-functions.test +++ b/test-data/unit/check-functions.test @@ -2044,3 +2044,26 @@ bad = make_list() # E: Need type annotation for variable def foo(__b: int, x: int, y: int) -> int: pass foo(x=2, y=2) # E: Missing positional argument foo(y=2) # E: Missing positional arguments + +[case testReturnTypeLineNumberWithDecorator] +def dec(f): pass + +@dec +def test(a: str) -> (str,): # E: Invalid tuple literal type + return None + +[case testReturnTypeLineNumberNewLine] +def fn(a: str + ) -> badtype: # E: Name 'badtype' is not defined + pass + +[case testArgumentTypeLineNumberWithDecorator] +def dec(f): pass + +@dec +def some_method(self: badtype): pass # E: Name 'badtype' is not defined + +[case TestArgumentTypeLineNumberNewline] +def fn( + a: badtype) -> None: # E: Name 'badtype' is not defined + pass diff --git a/test-data/unit/semanal-errors.test b/test-data/unit/semanal-errors.test index 99584e77a977..57dbb6ca3905 100644 --- a/test-data/unit/semanal-errors.test +++ b/test-data/unit/semanal-errors.test @@ -87,8 +87,8 @@ from typing import overload class A: pass @overload def f(): pass -@overload # E: "A" expects no type arguments, but 1 given -def f(x: A[int]) -> None: pass +@overload +def f(x: A[int]) -> None: pass # E: "A" expects no type arguments, but 1 given def f(*args): pass [out]