Skip to content

Use argument and return type line numbers for error messages #3395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2017
Merged
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
5 changes: 3 additions & 2 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test where the ) -> ... part is on a separate line?

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imilar with multiple arguments across multiple lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated PR with additional tests.


[case TestArgumentTypeLineNumberNewline]
def fn(
a: badtype) -> None: # E: Name 'badtype' is not defined
pass
4 changes: 2 additions & 2 deletions test-data/unit/semanal-errors.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down