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] Simplify "in dict.keys()" to "in dict" #40

Closed
Skylion007 opened this issue Feb 19, 2021 · 1 comment · Fixed by #42
Closed

[New Rule] Simplify "in dict.keys()" to "in dict" #40

Skylion007 opened this issue Feb 19, 2021 · 1 comment · Fixed by #42
Assignees
Labels
enhancement New feature or request

Comments

@Skylion007
Copy link
Contributor

Explanation

Most of the discussion of the motivation about this rule can be found in this very detailed StackOverflow post

Example

# Bad
key in dict.keys()

# Good
key in dict

Basically, the later way of doing this pythonic and slightly more performant and arguably more Readable. The previous method only exists for legacy code (<= python 2.2). So there is no reason to use the former in modern codebases.

@Skylion007 Skylion007 added the enhancement New feature or request label Feb 19, 2021
@MartinThoma
Copy link
Owner

$ 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='dict', ctx=Load()),
                            attr='keys',
                            ctx=Load(),
                        ),
                        args=[],
                        keywords=[],
                    ),
                ],
            ),
        ),
    ],
    type_ignores=[],
)

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.

2 participants