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

[SIM905] Use list of strings instead of splitting a constant string #86

Closed
MartinThoma opened this issue Jan 18, 2022 · 2 comments · Fixed by #98
Closed

[SIM905] Use list of strings instead of splitting a constant string #86

MartinThoma opened this issue Jan 18, 2022 · 2 comments · Fixed by #98
Assignees
Labels
enhancement New feature or request

Comments

@MartinThoma
Copy link
Owner

MartinThoma commented Jan 18, 2022

Explanation

You want a list of strings? Create one directly:

  • A list of strings can be formatted by black.
  • Your editor can sort the lines
  • You don't have to worry about how to keep leading / intermediate / trailing spaces or how to add newlines to the strings

Example

# Bad
domains = "de com net org".split()

# Good
domains = ["de", "com", "net", "org"]
@MartinThoma MartinThoma added the enhancement New feature or request label Jan 18, 2022
@MartinThoma MartinThoma self-assigned this Jan 18, 2022
@MartinThoma
Copy link
Owner Author

$ astpretty --no-show-offsets /dev/stdin <<< `cat example.txt`
Module(
    body=[
        Assign(
            targets=[Name(id='domains', ctx=Store())],
            value=Call(
                func=Attribute(
                    value=Constant(value='de com net org', kind=None),
                    attr='split',
                    ctx=Load(),
                ),
                args=[],
                keywords=[],
            ),
            type_comment=None,
        ),
    ],
    type_ignores=[],
)

@MartinThoma
Copy link
Owner Author

MartinThoma commented Feb 12, 2022

The relevant part is:

Call(
    func=Attribute(
    value=Constant(value='de com net org', kind=None),
    attr='split',
    ctx=Load(),
)

@MartinThoma MartinThoma changed the title [New Rule] Use list of strings instead of splitting a constant string [SIM905] Use list of strings instead of splitting a constant string Feb 13, 2022
MartinThoma added a commit that referenced this issue Feb 13, 2022
No string splitting anymore.

This will be SIM225 once the trial period is over

Closes #86
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant