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

New Rule + fixer: Use a list instead of calling split with string literals #13944

Closed
Avasam opened this issue Oct 28, 2024 · 3 comments · Fixed by #14008
Closed

New Rule + fixer: Use a list instead of calling split with string literals #13944

Avasam opened this issue Oct 28, 2024 · 3 comments · Fixed by #14008
Labels
accepted Ready for implementation rule Implementing or modifying a lint rule

Comments

@Avasam
Copy link

Avasam commented Oct 28, 2024

I've seen this pattern a handful of time:

"""
	itemA
	itemB
	itemC
""".split()

or

"a,b,c,d".split(",")

And I'd like a rule to transform usages of join with literals into actual inline lists without the overhead of calling "join". My two examples above would become:

[
	"itemA",
	"itemB",
	"itemC",
]

or

["a", "b", "c", "d"]

I don't think this rule exists in any linters that I'm aware of.

The exact details of how this gets formatted isn't important to this rule. Probably just rewrite everything on a single line and let formatting/other rules take care of the rest.

@dhruvmanila dhruvmanila added rule Implementing or modifying a lint rule needs-decision Awaiting a decision from a maintainer labels Oct 28, 2024
@Avasam Avasam changed the title New Rule + fixer: Use a list instead of calling join with string literals New Rule + fixer: Use a list instead of calling split with string literals Oct 28, 2024
@MichaReiser
Copy link
Member

This seems reasonable. Although I'm somewhat surprised that people do this :D

The comma pattern seems easy enough to detect. The split without a separator needs some extra care to ensure we apply the exact same logic.

@MichaReiser MichaReiser added accepted Ready for implementation and removed needs-decision Awaiting a decision from a maintainer labels Oct 30, 2024
@sbrugman
Copy link
Contributor

For posterity, @AlexWaygood noticed that this rule exists in flake8-simplify:
MartinThoma/flake8-simplify#86 (SIM905)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Ready for implementation rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants