Approve functions used as default arguments#4699
Merged
cclauss merged 9 commits intoTheAlgorithms:masterfrom Aug 31, 2021
Merged
Approve functions used as default arguments#4699cclauss merged 9 commits intoTheAlgorithms:masterfrom
cclauss merged 9 commits intoTheAlgorithms:masterfrom
Conversation
14 tasks
dhruvmanila
requested changes
Aug 31, 2021
Member
dhruvmanila
left a comment
There was a problem hiding this comment.
If this is intended, assign the function call to a module-level variable and use that variable as a default value.
Why don't we do what the linter is suggesting us to? I hate adding ignore directives throughout the code. If not, then we should atleast explain in a comment as to why that directive was added.
The default value for **seed** is the result of a function call which is not normally recommended and causes flake8-bugbear to raise a B008 error. However, in this case, it is accptable because `LinearCongruentialGenerator.__init__()` will only be called once per instance and it ensures that each instance will generate a unique sequence of numbers.
The default value for **backend** is the result of a function call which is not normally recommended and causes flake8-bugbear to raise a B008 error. However, in this case, it is accptable because `Aer.get_backend()` is called when the function is definition and that same backend is then reused for function calls.
dhruvmanila
approved these changes
Aug 31, 2021
Member
dhruvmanila
left a comment
There was a problem hiding this comment.
I think the explanation should be a comment instead of adding in a docstring as it is intended only for developers instead of end-users, otherwise LGTM.
Member
Author
|
Comment above function definition or below? If below, before docstring or after? |
Member
|
Above the directive which means above the function definition. |
shermanhui
pushed a commit
to shermanhui/Python
that referenced
this pull request
Oct 22, 2021
* Approve functions used as default argumenets * The default value for **seed** is the result of a function call The default value for **seed** is the result of a function call which is not normally recommended and causes flake8-bugbear to raise a B008 error. However, in this case, it is accptable because `LinearCongruentialGenerator.__init__()` will only be called once per instance and it ensures that each instance will generate a unique sequence of numbers. * The default value for **backend** is the result of a function call The default value for **backend** is the result of a function call which is not normally recommended and causes flake8-bugbear to raise a B008 error. However, in this case, it is accptable because `Aer.get_backend()` is called when the function is definition and that same backend is then reused for function calls. * Update linear_congruential_generator.py * Update ripple_adder_classic.py * Update ripple_adder_classic.py * Update ripple_adder_classic.py * Update ripple_adder_classic.py * Update ripple_adder_classic.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe your change:
Use linter directive
# noqa: B0008for these functions because they set a seed value andBaseBackendis reused across all calls andLinearCongruentialGenerator.__init__()is only called once for each instance.flake8-bugbear B008 Do not perform function calls in argument defaults. The call is performed only once at function definition time. All calls to your function will reuse the result of that definition-time function call. If this is intended, assign the function call to a module-level variable and use that variable as a default value.
Checklist:
Fixes: #{$ISSUE_NO}.