Description
Currently, the line number of the function definition is not correct leading to issues such as #3869.
In the Python ast (as well as type-ast) the FunctionDef
node we use to, for example, report missing return annotations gives a line number of the beginning of the first decorator (if any). Therefore it can incorrectly report the line number information we need to report errors.
Our currently logic increments the line number of any decorators, however, we cannot depend on this to be consistent. Consider this problematic (but completely valid!) example:
@decorate(
"multi",
"line"
)
def foo(
a: int,
b: str,
):
pass
The FunctionDef.lineno
would be 1, so mypy will report test.py:2: error: Function is missing a return type annotation
. However, the return should be on line 8! Note that blanks space between the body and end of the function definition node means we cannot decrement based on the first item in the body of the function.
As a first possible solution, we could modify typed_ast
to give the line number of the returns
if the function is not typed (currently it just puts the attribute as None), so that the information is known, but it clearly indicates that the node is empty. This is certainly not an ideal solution.
Corresponding issue opened in python/typed_ast#50