Change end line and column for untyped function defs to line of function definition#17006
Change end line and column for untyped function defs to line of function definition#17006mschoettle wants to merge 11 commits intopython:masterfrom
Conversation
This comment has been minimized.
This comment has been minimized.
|
|
||
| # set end line and column to same as start of context for function definitions | ||
| # this avoids errors being reported in IDEs for the whole function | ||
| # TODO: figure out if it's possible to find the end of the function definition line |
There was a problem hiding this comment.
One approach for this could be to add a new attribute to the FuncDef class and set it when we're creating the FuncDef object in fastparse.py.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
1 similar comment
This comment has been minimized.
This comment has been minimized.
809ac75 to
5a7c0ec
Compare
This comment has been minimized.
This comment has been minimized.
| self.docstring: str | None = None | ||
| # track the end of the function definition itself | ||
| self.def_end_line: int | None = None | ||
| self.def_end_column: int | None = None |
There was a problem hiding this comment.
I think the new attributes will also need to be serialized, or they may get lost in incremental mode.
There was a problem hiding this comment.
I checked in nodes.py and no node serializes end_line or end_column. The same for line and column with the exception of the TypeAlias node (it's unclear to me why).
I did a quick test invoking mypy with --incremental --disallow-untyped-defs --show-error-end on the same file twice which resulted in consistent results.
| ) -> int: | ||
| x = 1 | ||
| [out] | ||
| main:2:1:4:11: error: Function is missing a return type annotation |
There was a problem hiding this comment.
It's unfortunate that the closing ): isn't included in the range. I imagine that's hard to fix though.
There was a problem hiding this comment.
I agree. I unfortunately don't know how this could be fixed since it is unavailable in the AST.
For reference: I did find ast.get_source_segment. However, it also returns the source code for a function definition node including the body.
There was a problem hiding this comment.
I understand. I'm still struggling with this case (as well as the one above where if the function has no annotations at all, we emit a one-character range); I'm worried this PR may lead to more confusing output in some cases. However, on balance it's probably fine and we can apply this change, but consider reverting it if there's a lot of negative feedback.
We could also consider looking at the first start position in the body of the function, but then the whitespace between the colon and the first statement would be included in the range.
This comment has been minimized.
This comment has been minimized.
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Fixes #16746
Add extra attribures to
FuncDefto keep track of a function definition's end excluding the body (end_lineandend_columninclude the function body).The definition's end is determined by either the return type hint's end or the last argument's end. Otherwise, the fall back is the function start (since it otherwise affects the output of
--pretty). Potential alternatives (if desired) are either the end of the body or to include the length of the function name.Before
After