Skip to content

Conversation

@danparizher
Copy link
Contributor

Fixes #18069

Summary

This PR addresses a bug in the flake8-simplify rule SIM905 (split-static-string) where str.split(maxsplit=0) and str.rsplit(maxsplit=0) produced incorrect results for empty strings or strings starting/ending with whitespace. The fix ensures that the linting rule's suggested replacements now align with Python's native behavior for these specific maxsplit=0 scenarios.

Test Plan

  1. Added new test cases to the existing crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM905.py fixture to cover the scenarios described in issue SIM905 fix breaks "".split(maxsplit=0) and similar expressions #18069.
  2. Ran cargo test -p ruff_linter.
  3. Verified and accepted the updated snapshots for SIM905.py using cargo insta review. The new snapshots confirm the corrected behavior for maxsplit=0.

@github-actions
Copy link
Contributor

github-actions bot commented May 13, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre ntBre added bug Something isn't working fixes Related to suggested fixes for violations labels May 13, 2025
Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thanks! Just a couple of simplification suggestions, but the logic looks great to me. And thanks for all of the test cases!

@danparizher
Copy link
Contributor Author

Thanks for the review! This is definitely a lot better. I can also work on the logic for #18101 and include it in this PR as well. Let me know if that makes the most sense!

@ntBre
Copy link
Contributor

ntBre commented May 14, 2025

Thanks for the review! This is definitely a lot better. I can also work on the logic for #18101 and include it in this PR as well. Let me know if that makes the most sense!

No problem, thanks for handling the suggestions!

I think we can hold off on #18101 for now. I went ahead and put SIM905 in the title since that's the rule I tested, but I think it's likely more widespread, and we might be able to handle it generally rather than in each individual rule (at least I hope so!).

Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Great, thanks!

@ntBre ntBre changed the title [flake8-simplify]: Correct behavior for str.split/rsplit with maxsplit=0 (SIM905) [flake8-simplify] Correct behavior for str.split/rsplit with maxsplit=0 (SIM905) May 14, 2025
@ntBre ntBre changed the title [flake8-simplify] Correct behavior for str.split/rsplit with maxsplit=0 (SIM905) [flake8-simplify] Correct behavior for str.split/rsplit with maxsplit=0 (SIM905) May 14, 2025
@ntBre ntBre merged commit 030a16c into astral-sh:main May 14, 2025
34 checks passed
@danparizher danparizher deleted the fix/issue-18069 branch May 14, 2025 18:24
dcreager added a commit that referenced this pull request May 14, 2025
…rals

* origin/main:
  [ty] Add type-expression syntax link to invalid-type-expression (#18104)
  [`flake8-simplify`] add fix safety section (`SIM103`) (#18086)
  [ty] mypy_primer: fix static-frame setup (#18103)
  [`flake8-simplify`] Correct behavior for `str.split`/`rsplit` with `maxsplit=0` (`SIM905`) (#18075)
  [ty] Fix more generics-related TODOs (#18062)
Glyphack pushed a commit to Glyphack/ruff that referenced this pull request May 21, 2025
…axsplit=0` (`SIM905`) (astral-sh#18075)

Fixes astral-sh#18069

<!--
Thank you for contributing to Ruff/ty! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title? (Please prefix
with `[ty]` for ty pull
  requests.)
- Does this pull request include references to any relevant issues?
-->

## Summary

This PR addresses a bug in the `flake8-simplify` rule `SIM905`
(split-static-string) where `str.split(maxsplit=0)` and
`str.rsplit(maxsplit=0)` produced incorrect results for empty strings or
strings starting/ending with whitespace. The fix ensures that the
linting rule's suggested replacements now align with Python's native
behavior for these specific `maxsplit=0` scenarios.

## Test Plan

1. Added new test cases to the existing
`crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM905.py`
fixture to cover the scenarios described in issue astral-sh#18069.
2.  Ran `cargo test -p ruff_linter`.
3. Verified and accepted the updated snapshots for `SIM905.py` using
`cargo insta review`. The new snapshots confirm the corrected behavior
for `maxsplit=0`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working fixes Related to suggested fixes for violations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SIM905 fix breaks "".split(maxsplit=0) and similar expressions

2 participants