ruff
a23e489c - [`refurb`] Correctly handle lengths of literal strings in `slice-to-remove-prefix-or-suffix` (`FURB188`) (#16237)

Commit
210 days ago
[`refurb`] Correctly handle lengths of literal strings in `slice-to-remove-prefix-or-suffix` (`FURB188`) (#16237) Fixes false negative when slice bound uses length of string literal. We were meant to check the following, for example. Given: ```python text[:bound] if text.endswith(suffix) else text ``` We want to know whether: - `suffix` is a string literal and `bound` is a number literal - `suffix` is an expression and `bound` is exactly `-len(suffix)` (as AST nodes, prior to evaluation.) The issue is that negative number literals like `-10` are stored as unary operators applied to a number literal in the AST. So when `suffix` was a string literal but `bound` was `-len(suffix)` we were getting caught in the match arm where `bound` needed to be a number. This is now fixed with a guard. Closes #16231
Author
Parents
Loading