dosisod / refurb

A tool for refurbishing and modernizing Python codebases
GNU General Public License v3.0
2.49k stars 54 forks source link

[Enhancement]: suggest use `str.splitlines` method #301

Closed blablatdinov closed 1 year ago

blablatdinov commented 1 year ago

Overview

Refurb can detect .split('\n') methods and suggest replacing them with .splitlines()

Proposal

We can create a new check for the search str.split method with '\n' argument usage

dosisod commented 1 year ago

Thank you for opening this! Unfortunately, .split("\n") is not the same as .splitlines(), so we can't safely replace all split calls with splitlines.

Take the following for instance:

>>> data = "hello\r\nworld\r\n"
>>> data.split("\n")
['hello\r', 'world\r', '']
>>> data.splitlines()
['hello', 'world']

splitlines() differs in a few ways, primarily in that it splits on all newline-like white space characters (not just newline), and by default, splitlines() doesn't include empty lines in the output. See the docs for a more detailed explanation of the splitlines() function.