MartinThoma / flake8-simplify

❄ A flake8 plugin that helps you to simplify code
MIT License
185 stars 19 forks source link

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

Closed MartinThoma closed 2 years ago

MartinThoma commented 2 years ago

Explanation

You want a list of strings? Create one directly:

Example

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

# Good
domains = ["de", "com", "net", "org"]
MartinThoma commented 2 years ago
$ 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 commented 2 years ago

The relevant part is:

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