Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refurb] Implement slice-to-remove-prefix-or-suffix (FURB188) #13256

Merged
merged 26 commits into from
Sep 9, 2024

Conversation

dylwil3
Copy link
Contributor

@dylwil3 dylwil3 commented Sep 5, 2024

This PR implements FURB188 which suggests the use of removeprefix/removesuffix in order to remove prefixes and suffixes from strings, rather than slicing the string conditionally upon the output of startswith/endswith.

Tested using same test suite as refurb.

Moves the needle on #1348.

@dylwil3
Copy link
Contributor Author

dylwil3 commented Sep 5, 2024

Not super happy with how gnarly the implementation is - so open to any feedback/tips to clean it up. I've tried to add comments wherever possible so hopefully it is at least readable.

Definitely found myself wishing that Rust supported matching patterns involving structs with heavily nested Boxes... (rust-lang/rust#87121)

Copy link
Contributor

github-actions bot commented Sep 5, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+7 -0 violations, +0 -0 fixes in 3 projects; 51 projects unchanged)

bokeh/bokeh (+2 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview --select ALL

+ src/bokeh/util/compiler.py:511:9: FURB188 [*] Prefer `removesuffix` over conditionally replacing with slice.
+ tests/support/plugins/file_server.py:63:9: FURB188 [*] Prefer `removeprefix` over conditionally replacing with slice.

rotki/rotki (+2 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ rotkehlchen/assets/converters.py:57:5: FURB188 [*] Prefer `removesuffix` over conditionally replacing with slice.
+ rotkehlchen/exchanges/bitfinex.py:575:9: FURB188 [*] Prefer `removeprefix` over conditionally replacing with slice.

indico/indico (+3 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ indico/cli/devserver.py:220:9: FURB188 [*] Prefer `removeprefix` over conditionally replacing with slice.
+ indico/core/db/sqlalchemy/colors.py:31:13: FURB188 [*] Prefer `removeprefix` over conditionally replacing with slice.
+ indico/modules/networks/fields.py:44:9: FURB188 [*] Prefer `removeprefix` over conditionally replacing with slice.

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
FURB188 7 7 0 0 0

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I can see that a lot of work went into this

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only skimmed over the rust code and left a few NIT comments.

@MichaReiser MichaReiser added rule Implementing or modifying a lint rule preview Related to preview mode features labels Sep 8, 2024
@dylwil3
Copy link
Contributor Author

dylwil3 commented Sep 9, 2024

Wow @AlexWaygood and @MichaReiser - what a fantastic and thorough code review! Thanks so much! Learning a lot from you

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice indeed! Basically just nits remaining, though there is one other location where I think you should use match_builtin_expr() for correctness

@dylwil3
Copy link
Contributor Author

dylwil3 commented Sep 9, 2024

Thanks again! I think I picked the remaining nits.

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fab! Some tiny last remaining comments:

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@AlexWaygood AlexWaygood enabled auto-merge (squash) September 9, 2024 15:03
@AlexWaygood AlexWaygood merged commit b04948f into astral-sh:main Sep 9, 2024
19 checks passed
@dylwil3 dylwil3 deleted the remove-affix branch September 9, 2024 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
preview Related to preview mode features rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants