python / typed_ast

Modified fork of CPython's ast module that parses `# type:` comments
Other
227 stars 54 forks source link

Expose more information about function line numbers #50

Closed ethanhs closed 5 years ago

ethanhs commented 7 years ago

In hopes of solving https://github.com/python/mypy/issues/3871 we probably need to modify typed_ast to expose more information about function line numbers.

If the return type annotation of a function is absent, the FunctionDef node will have returns as None, which means mypy cannot get the line number of where the return type annotation should be. Additionally the actual def line number may be impossible to calculate based on given ast data.

gvanrossum commented 7 years ago

Whatever solution we pick should also be applied to decorated classes.

ethanhs commented 7 years ago

In https://github.com/python/mypy/issues/3871 we seem to be of the opinion that adding class_lineno and def_lineno attributes are the best solution.

ethanhs commented 7 years ago

To be clear, I am adding this to ClassDef, FunctionDef, and AsyncFunctionDef.

ethanhs commented 5 years ago

It appears this was fixed in https://github.com/python/cpython/pull/9731. Perhaps we could backport that change after the update to being based on the 3.7 ast?

ilevkivskyi commented 5 years ago

Taking into account size of the fix, can we backport just the fix now? Supporting 3.7 in typed_ast may take time because of the ASYNC problems, while this fix looks small and well localized.

ethanhs commented 5 years ago

Sure, I'd be happy to make a PR.

ilevkivskyi commented 5 years ago

OK, let's do this then.

gvanrossum commented 5 years ago

So... It looks like we took a different path. In Python 3.8's ast this is fixed, but it was not fixed in Python 3.7's ast. Typed_ast is now based on Python 3.7's ast.

Should we do it or not? I worry that just changing the lineno attribute of FunctionDef and ClassDef nodes will break tons of users' code since it changes where # type: ignore comments would have to go. So maybe let sleeping dogs lie?

gvanrossum commented 5 years ago

Let's leave typed_ast alone. With Python 3.8 we can support # type: ignore backward compatibility, see https://github.com/python/mypy/issues/3871#issuecomment-487427583 and further comments. For 3.7 and before we'll stick with the status quo.