Skip to content

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

@dscorbett

Description

@dscorbett

Summary

The fix for split-static-string (SIM905) changes the behavior of str.split with sep=None (the default) and maxsplit=0 if the string is empty or begins with white space. The output of split should not contain an empty string or a string beginning with white space. Similarly, the fix breaks str.rsplit if the string is empty or ends with white space.

$ cat >sim905.py <<'# EOF'
print("".split(maxsplit=0))
print(" ".split(maxsplit=0))
print(" x ".split(maxsplit=0))
print("".rsplit(maxsplit=0))
print(" ".rsplit(maxsplit=0))
print(" x ".rsplit(maxsplit=0))
# EOF

$ python sim905.py
[]
[]
['x ']
[]
[]
[' x']

$ ruff --isolated check sim905.py --select SIM905 --fix
Found 6 errors (6 fixed, 0 remaining).

$ python sim905.py
['']
[' ']
[' x ']
['']
[' ']
[' x ']

Version

ruff 0.11.9 (2370297 2025-05-09)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixesRelated to suggested fixes for violations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions