Make Binding::range()
point to the range of a type parameter's name, not the full type parameter
#15935
+29
−44
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.
Summary
A PEP-695 type parameter creates a binding that is tracked by Ruff's semantic model:
But if the type parameter has a bound or constraint, the range of the
Binding
recorded by Ruff's semantic model includes the bounds or constraints of the type parameter:This is problematic, as
S: int
is not a valid Python identifier, and nor isT: (int, str)
. All other bindings in Ruff's semantic model have their range point to the name that is bound, not the full expression/statement that creates the binding. A specific effect that this has is that if you have aBinding
x
that points to a PEP-695 type parameter, callingx.name(source)
does not actually return the name of the PEP-695 type parameter. Instead, ifx
pointed to the binding forT
in the example above, callingx.name(source)
would return"T: int"
, and ifx
pointed to the binding forU
, callingx.name(source)
would return"U: (int, str)"
.This PR changes the range for PEP-695 bindings so that it points to the name that is bound by the type parameter. This makes these
Binding
s consistent with other bindings, makesBinding::name()
work as expected in all situations, allows us to simplify some code in the implementation of the PYI019 rule, and unblocks #15862Test Plan
cargo test -p ruff_linter --lib
Co-authored-by: Brent Westbrook 36778786+ntBre@users.noreply.github.com