Update Indexer to use new f-string tokens#7325
Conversation
d7e004e to
d018b5b
Compare
6c4f8c2 to
44f3155
Compare
d018b5b to
79c5a1a
Compare
MichaReiser
left a comment
There was a problem hiding this comment.
Can you add tests for the upstream functions that use the metadata extracted by the Indexer to ensure they are working as intended.
Nested f-strings:Pre 3.12, nested f-strings were possible but up to an extent. For example, f"outside {f'inside'} outside"
# or
f'outside {f"inside"} outside'The rules which performs checks on strings don’t support nested f-strings. For example, f"{f"\y"}"For the above code snippet, the F-string ranges:The Currently, the f-string range is only being utilized to get the f-string quote style but this would be incorrect in the case of nested f-strings. For example, in The question is: do we want to proceed with how the current implementation is (use the outermost f-string range) or have separate ranges for each f-strings even if it’s nested? Triple-quoted string ranges:Currently, if a triple-quoted f-string is encountered, then the range is only recorded as a triple-quoted string but not as a f-string i.e., it’s only being inserted to This is only being utilized in Another challenge here is that with f-strings even the triple-quoted strings can be nested: f"""first level
of triple-quoted {f"""second level
of triple-quoted"""}
f-strings"""This again brings up the question of how granular we want the triple-quoted string ranges to be? I'm leaning towards keeping the behavior same and we can iterate to fix the issues around nested f-strings. But, if we're sure on how to handle nested f-strings I can make the changes here. |
Technically, I think we can solve this by using a
More specifically, it seems this is only used to guess the appropriate quote style for an fstring. This logic won't be needed for Python 3.12+ because we can pick any quote style (ideally, the default quote style). It might be okay to leave the implementation as is but we should document the limitation and its intended use case. Not that more call sites are introduced that incorrectly assume it supports nested FStrings correctly.
I don't think this was intentional.
I wonder if it would be easier to rewrite that rule to a token-based rule rather than trying to extract all the data at a granularity level close to the individual tokens. Except if we plan to to re-use the multiline-string ranges for ruff/crates/ruff/src/directives.rs Lines 90 to 114 in fc89976 I would be interested to get @charliermarsh's opinion on this, who is more familiar with the use cases (I only know the |
e53db6c to
8e92f64
Compare
CodSpeed Performance ReportMerging #7325 will not alter performanceComparing Summary
|
|
Could you rebase your PRs so that we get clear benchmark numbers? This all looks worrying but they're probably false positives |
641c4a7 to
c496d9c
Compare
de2bd87 to
ec9a331
Compare
ec9a331 to
0b2e6bb
Compare
## Summary This PR updates the `Indexer` to use the new f-string tokens to compute the `f_string_ranges` for f-strings. It adds a new abstraction which exposes two methods to support extracting the range for the surrounding innermost and outermost f-string. It uses the builder pattern to build the f-string ranges which is similar to how the comment ranges are built. ## Test Plan Add new test cases for f-strings for: * Tab indentation rule * Line continuation detection in the indexer * To get the innermost / outermost f-string range * All detected f-string ranges fixes: #7290
## Summary This PR updates the `Indexer` to use the new f-string tokens to compute the `f_string_ranges` for f-strings. It adds a new abstraction which exposes two methods to support extracting the range for the surrounding innermost and outermost f-string. It uses the builder pattern to build the f-string ranges which is similar to how the comment ranges are built. ## Test Plan Add new test cases for f-strings for: * Tab indentation rule * Line continuation detection in the indexer * To get the innermost / outermost f-string range * All detected f-string ranges fixes: #7290
## Summary This PR updates the `Indexer` to use the new f-string tokens to compute the `f_string_ranges` for f-strings. It adds a new abstraction which exposes two methods to support extracting the range for the surrounding innermost and outermost f-string. It uses the builder pattern to build the f-string ranges which is similar to how the comment ranges are built. ## Test Plan Add new test cases for f-strings for: * Tab indentation rule * Line continuation detection in the indexer * To get the innermost / outermost f-string range * All detected f-string ranges fixes: #7290
## Summary This PR updates the `Indexer` to use the new f-string tokens to compute the `f_string_ranges` for f-strings. It adds a new abstraction which exposes two methods to support extracting the range for the surrounding innermost and outermost f-string. It uses the builder pattern to build the f-string ranges which is similar to how the comment ranges are built. ## Test Plan Add new test cases for f-strings for: * Tab indentation rule * Line continuation detection in the indexer * To get the innermost / outermost f-string range * All detected f-string ranges fixes: #7290
## Summary This PR updates the `Indexer` to use the new f-string tokens to compute the `f_string_ranges` for f-strings. It adds a new abstraction which exposes two methods to support extracting the range for the surrounding innermost and outermost f-string. It uses the builder pattern to build the f-string ranges which is similar to how the comment ranges are built. ## Test Plan Add new test cases for f-strings for: * Tab indentation rule * Line continuation detection in the indexer * To get the innermost / outermost f-string range * All detected f-string ranges fixes: #7290
## Summary This PR updates the `Indexer` to use the new f-string tokens to compute the `f_string_ranges` for f-strings. It adds a new abstraction which exposes two methods to support extracting the range for the surrounding innermost and outermost f-string. It uses the builder pattern to build the f-string ranges which is similar to how the comment ranges are built. ## Test Plan Add new test cases for f-strings for: * Tab indentation rule * Line continuation detection in the indexer * To get the innermost / outermost f-string range * All detected f-string ranges fixes: #7290
## Summary This PR updates the `Indexer` to use the new f-string tokens to compute the `f_string_ranges` for f-strings. It adds a new abstraction which exposes two methods to support extracting the range for the surrounding innermost and outermost f-string. It uses the builder pattern to build the f-string ranges which is similar to how the comment ranges are built. ## Test Plan Add new test cases for f-strings for: * Tab indentation rule * Line continuation detection in the indexer * To get the innermost / outermost f-string range * All detected f-string ranges fixes: #7290
## Summary This PR updates the `Indexer` to use the new f-string tokens to compute the `f_string_ranges` for f-strings. It adds a new abstraction which exposes two methods to support extracting the range for the surrounding innermost and outermost f-string. It uses the builder pattern to build the f-string ranges which is similar to how the comment ranges are built. ## Test Plan Add new test cases for f-strings for: * Tab indentation rule * Line continuation detection in the indexer * To get the innermost / outermost f-string range * All detected f-string ranges fixes: #7290
## Summary This PR updates the `Indexer` to use the new f-string tokens to compute the `f_string_ranges` for f-strings. It adds a new abstraction which exposes two methods to support extracting the range for the surrounding innermost and outermost f-string. It uses the builder pattern to build the f-string ranges which is similar to how the comment ranges are built. ## Test Plan Add new test cases for f-strings for: * Tab indentation rule * Line continuation detection in the indexer * To get the innermost / outermost f-string range * All detected f-string ranges fixes: #7290
## Summary This PR updates the `Indexer` to use the new f-string tokens to compute the `f_string_ranges` for f-strings. It adds a new abstraction which exposes two methods to support extracting the range for the surrounding innermost and outermost f-string. It uses the builder pattern to build the f-string ranges which is similar to how the comment ranges are built. ## Test Plan Add new test cases for f-strings for: * Tab indentation rule * Line continuation detection in the indexer * To get the innermost / outermost f-string range * All detected f-string ranges fixes: #7290

Summary
This PR updates the
Indexerto use the new f-string tokens to compute thef_string_rangesfor f-strings. It adds a new abstraction which exposes two methods to support extracting the range for the surrounding innermost and outermost f-string. It uses the builder pattern to build the f-string ranges which is similar to how the comment ranges are built.Test Plan
Add new test cases for f-strings for:
fixes: #7290