chore: use str.removeprefix/removesuffix instead of slice (ruff FURB188)#166
Open
alexzhu0 wants to merge 1 commit intoFlowElement-ai:mainfrom
Open
chore: use str.removeprefix/removesuffix instead of slice (ruff FURB188)#166alexzhu0 wants to merge 1 commit intoFlowElement-ai:mainfrom
alexzhu0 wants to merge 1 commit intoFlowElement-ai:mainfrom
Conversation
Ruff rule FURB188 flags hand-rolled `if s.startswith(p): s = s[len(p):]` patterns that are equivalent to `s = s.removeprefix(p)` (and the symmetric `endswith`/`removesuffix` pair). These string methods landed in Python 3.9, and this repo targets `>=3.10`, so they are always available. Only three sites are flagged, all in one file, and each iterates over tuples of *non-empty* literal prefixes/suffixes, so the cosmetic difference at the empty-string boundary does not apply here. Verification: - `uv run ruff check ... --select FURB188` is now clean - ast.parse on every touched file: clean No behavior change.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Ruff rule FURB188 flags hand-rolled
if s.startswith(p): s = s[len(p):]patterns that are equivalent tos = s.removeprefix(p)(and the symmetricendswith/removesuffixpair). These methods landed in Python 3.9 (docs), andpyproject.tomlpinsrequires-python = ">=3.10,<3.14", so they are always available. 3 occurrences in one file, all auto-fixable.Semantic equivalence
The
str.removeprefix(p)/str.removesuffix(s)methods return a copy with the specified prefix/suffix stripped, or the original string unchanged if the prefix/suffix is absent — exactly the same observable behavior as the guarded slice.One pedantic edge case: when the prefix/suffix is
"", the old forms[len(""):] == s[0:]returnss, buts[:-len("")] == s[:0]would return"". The newremoveprefix("")/removesuffix("")both return the original string. In this PR, all three loops iterate over tuples of non-empty literal strings ("the "," inc"," ltd"," corp"," corporation"," llc","a ","an "), so the boundary never fires and the replacement is strictly byte-identical.Scope
1 file, 3 sites, +3 / −6 (guard-and-slice collapses into a single assignment).
coreference/english_coreference/ner_adapter.pyVerification
No behavior change. Generated code under
m_flow/baml_client/is untouched.I affirm that all code in every commit of this pull request conforms to the terms of the M-flow Developer Certificate of Origin