This repository was archived by the owner on Jul 27, 2023. It is now read-only.
Include decorators in Class
and FunctionDef
range
#2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR changes the
ClassDef
andFunctionDef
definitions to include the range of their decorators.Example
@test
decorator)Motivation
This change is because these are the only nodes for where the property
parent.range.includes(child.range)
does not hold. The fact that this property is not respected means it is e.g. impossible to use a binary search over the AST to find a node by its range.Downsides
The main downside that I'm aware of is that diagnostics using
range.start
to compute the line index for functions and classes may now incorrectly point to the line of the function's decorator instead of the function definition. I don't know if this is a problem for RustPython. Ruff already uses custom infrastructure to extract the range of the function name only to limit the diagnostic range (and e.g. avoid highlighting the whole function or class including the body)Alternatives
An alternative to this would be to change our AST and introduce a new
DecoratedDefinition
node that includes thedecorator
and theFunction
orClass
definition. This is what tree-sitter does.Related
Function
andClass
definition ranges ruff#4467