Suggestion: Ignore B006 if the variable in question is immediately copied
See original GitHub issueI would argue that these two functions:
def foo(x=[]):
x = list(x)
x.reverse()
return x
def bar(x={}):
x = dict(x)
x.pop('k', None)
return x
are far more readable than the way B006 forces me to write them
def foo_bad(x=None):
if x is None:
x = []
x.reverse()
return x
def bar_bad(x=None):
if x is None:
x = {}
x.pop('k', None)
return x
Furthermore, the rewrite encouraged by B006 now actually leads to a bug - x = [1]; foo_bad(x) leads to x being mutated.
Would it be possible/sensible to add an exemption to B006 along the lines of “if the only use of the mutable default is to pass into a non-mutating function call, then emit no warning”?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:14 (1 by maintainers)
Top Results From Across the Web
Why does PyCharm warn about mutable default arguments ...
This inspection detects when a mutable value as list or dictionary is detected in a default value for an argument. Default argument values...
Read more >Explaining the Variable Effects of Social Support on Work ...
Our model proposes that the buffering effect of the totality of support perceived to be received from close others on the relationship between...
Read more >Preparing for the ACT® Test
A good strategy is to answer the easy questions and skip the questions you find difficult. ... copy of the multiple-choice test questions...
Read more >© 2021, American Psychological Association. This paper is ...
on a different issue. It has recently been suggested (e.g. in Oasksford & Chater, 2020a, 2020b; van Rooij & Schulz, 2019) that relevance...
Read more >index-d.xml
... -a-vpn-client-cannot-perform-name-resolution-queries-immediately-after-the ... https://support.microsoft.com/en-us/topic/students-export-a-copy-of-your- ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Mypy will then check that callers pass only types that implement the (read only) mapping protocol (e.g. builtin dict) and that the function only uses that type in a read only way.
no comment
This is generally a good rule, I want to keep it on. Mypy will catch some of these errors but not e.g.:
I have just taken a fork and will see if I can create something…
Mypy will detect any attempt to mutate the dict and emit an error