Skip to content
This repository was archived by the owner on Dec 8, 2023. It is now read-only.

Add pattern to replace if-else block with dict.get() when possible#21

Merged
morgante merged 2 commits intogetgrit:mainfrom
vlopezferrando:dict-get
Sep 19, 2023
Merged

Add pattern to replace if-else block with dict.get() when possible#21
morgante merged 2 commits intogetgrit:mainfrom
vlopezferrando:dict-get

Conversation

@vlopezferrando
Copy link
Contributor

Join multiple with statements into a single one. Rule SIM401 from flake8-simplify.

Caveat: if either the key or the default value are functions, they will be called a different
number of times in the generated code. We may need to enforce that $key and $default don't
call any function.

engine marzano(0.1)
language python

// NOTE: if $key or $default call functions with side effects, this transform is not safe
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's handle that in the code. You can use a where clause.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, I have added:

 where {
    !$key <: contains call(),
    !$default <: contains call(),
}

Of course, in Python it is tricky to 100% enforce this, as an operator could be overloaded with anything.

@morgante morgante merged commit be9ee4d into getgrit:main Sep 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants