-
Notifications
You must be signed in to change notification settings - Fork 7
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
Fix "dangerous default" mutable paramters and enable pylint warning for future. #273
Conversation
Reviewer's Guide by SourceryThis pull request addresses the issue of 'dangerous default' mutable parameters in the codebase. The changes involve replacing default mutable parameters (like lists and dictionaries) with None and initializing them within the method if they are None. This ensures that a new object is created each time the function is called, preventing unintended side effects. Additionally, the pull request enables pylint warnings to catch such issues in the future. File-Level Changes
Tips
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @myxie - I've reviewed your changes and they look great!
Here's what I looked at during the review
- 🟡 General issues: 3 issues found
- 🟢 Security: all looks good
- 🟡 Testing: 1 issue found
- 🟢 Complexity: all looks good
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.
@@ -243,7 +244,7 @@ def depthFirstTraverse(node: "AbstractDROP", visited=[]): | |||
|
|||
This implementation is recursive. | |||
""" | |||
|
|||
visited = visited if visited else [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (code-quality): Replace if-expression with or
(or-if-exp-identity
)
visited = visited if visited else [] | |
visited = visited or [] |
Explanation
Here we find ourselves setting a value if it evaluates toTrue
, and otherwiseusing a default.
The 'After' case is a bit easier to read and avoids the duplication of
input_currency
.
It works because the left-hand side is evaluated first. If it evaluates to
true then currency
will be set to this and the right-hand side will not be
evaluated. If it evaluates to false the right-hand side will be evaluated and
currency
will be set to DEFAULT_CURRENCY
.
Hi @awicenec, another small PR for you when you have the time. |
@awicenec if you're happy with these changes I will go ahead and merge :) |
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
If tests are passing good to merge. |
Fix "dangerous default" mutable paramters and enable pylint warning for future.
Issue
Our code has a few instances of initialising empty
list
/dict
s in the method parameters:The problem here is that in Python, the default value for param is initialised when the function is first interpreted, and it is mutable. This means that if we do not pass in values for
param
as an argument, the following happens:We are adding to the same default-initalised
param
object, rather than creating a new default each time the function is called.This has been a common 'gotcha' in Python for many years: https://web.archive.org/web/20200221224620id_/http://effbot.org/zone/default-values.htm
Solution
The Pythonic solution to this is to make sure that the default for a parameterised mutable is
None
, and then initialise an emptydata structure in the function:
Summary by Sourcery
This pull request addresses the issue of mutable default parameters in function definitions by initializing them to None and setting them within the function. Additionally, it enables a pylint warning to prevent future occurrences of this issue.