MartinThoma / flake8-simplify

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

[SIM904][False-Positive] Adding values to a dictionary using existing values #99

Closed MetRonnie closed 2 years ago

MetRonnie commented 2 years ago

Desired change

Explanation

Sometimes you need to add to a dictionary in a way that cannot be done until after the dictionary is already created.

Example

This is an example where the mentioned rule(s) would currently be suboptimal (unless I'm missing a trick):

my_dict = {
    'foo': [1, 2, 3, 4],
    'bar': [5, 6, 7, 8]
}
my_dict['both'] = [item for _list in my_dict.values() for item in _list]

SIM904 Initialize dictionary 'my_dict' directly

MartinThoma commented 2 years ago

Thank you for the report, I'll fix that today in the evening 🤗

MartinThoma commented 2 years ago
$ astpretty --no-show-offsets /dev/stdin <<< `cat example.txt`
Module(
    body=[
        Assign(
            targets=[Name(id='my_dict', ctx=Store())],
            value=Dict(
                keys=[
                    Constant(value='foo', kind=None),
                    Constant(value='bar', kind=None),
                ],
                values=[
                    List(
                        elts=[
                            Constant(value=1, kind=None),
                            Constant(value=2, kind=None),
                            Constant(value=3, kind=None),
                            Constant(value=4, kind=None),
                        ],
                        ctx=Load(),
                    ),
                    List(
                        elts=[
                            Constant(value=5, kind=None),
                            Constant(value=6, kind=None),
                            Constant(value=7, kind=None),
                            Constant(value=8, kind=None),
                        ],
                        ctx=Load(),
                    ),
                ],
            ),
            type_comment=None,
        ),
        Assign(
            targets=[
                Subscript(
                    value=Name(id='my_dict', ctx=Load()),
                    slice=Constant(value='both', kind=None),
                    ctx=Store(),
                ),
            ],
            value=ListComp(
                elt=Name(id='item', ctx=Load()),
                generators=[
                    comprehension(
                        target=Name(id='_list', ctx=Store()),
                        iter=Call(
                            func=Attribute(
                                value=Name(id='my_dict', ctx=Load()),
                                attr='values',
                                ctx=Load(),
                            ),
                            args=[],
                            keywords=[],
                        ),
                        ifs=[],
                        is_async=0,
                    ),
                    comprehension(
                        target=Name(id='item', ctx=Store()),
                        iter=Name(id='_list', ctx=Load()),
                        ifs=[],
                        is_async=0,
                    ),
                ],
            ),
            type_comment=None,
        ),
    ],
    type_ignores=[],
)