MartinThoma / flake8-simplify

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

[SIM122] Use 'key in some_dict' instead of 'key in some_dict.keys()' #51

Closed MartinThoma closed 3 years ago

MartinThoma commented 3 years ago

Explanation

The first one is shorter and extremely wide-spread

Example

# Bad
key in some_dict.keys()

# Good
key in some_dict
MartinThoma commented 3 years ago
$ astpretty --no-show-offsets /dev/stdin <<< `cat example.txt`
Module(
    body=[
        Expr(
            value=Compare(
                left=Name(id='key', ctx=Load()),
                ops=[In()],
                comparators=[
                    Call(
                        func=Attribute(
                            value=Name(id='some_dict', ctx=Load()),
                            attr='keys',
                            ctx=Load(),
                        ),
                        args=[],
                        keywords=[],
                    ),
                ],
            ),
        ),
    ],
    type_ignores=[],
)
MartinThoma commented 3 years ago

Duplicate of SIM118