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

Enforce using .setdefault() #1085

Open
sobolevn opened this issue Dec 19, 2019 · 5 comments
Open

Enforce using .setdefault() #1085

sobolevn opened this issue Dec 19, 2019 · 5 comments
Labels
help wanted Extra attention is needed level:starter Good for newcomers rule request Adding a new rule

Comments

@sobolevn
Copy link
Member

Rule request

Thesis

One can write this code:

if "list" not in dictionary:
    dictionary["list"] = []

dictionary["list"].append("list_item")

But, it is an anti-pattern. Instead one should write:

dictionary = {}

dictionary.setdefault("list", []).append("list_item")

Related: https://docs.quantifiedcode.com/python-anti-patterns/correctness/not_using_setdefault_to_initialize_a_dictionary.html

This is a refactoring rule.

@sobolevn sobolevn added rule request Adding a new rule help wanted Extra attention is needed level:starter Good for newcomers labels Dec 19, 2019
@sobolevn sobolevn added this to the Version 0.15 milestone Dec 19, 2019
@adambenali
Copy link
Contributor

Should this be closed for the same reasons as #1084 ?

@sobolevn
Copy link
Member Author

Yes, thank you!

@sobolevn sobolevn reopened this Feb 26, 2020
@sobolevn
Copy link
Member Author

Or should it? Let's leave it open for now. I will have to make a small research.

@ruwaid4
Copy link
Contributor

ruwaid4 commented Apr 3, 2020

@sobolevn Should this rule be implemented? If not, could you recommend us an issue to work on? We have looked at most of them but aren't up to date on what should be implemented or not.

@sobolevn
Copy link
Member Author

sobolevn commented Apr 3, 2020

@ruwaid4 here you go:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed level:starter Good for newcomers rule request Adding a new rule
Projects
None yet
Development

No branches or pull requests

3 participants