question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggestion: Ignore B006 if the variable in question is immediately copied

See original GitHub issue

I 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:open
  • Created 3 years ago
  • Reactions:1
  • Comments:14 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
rupertnashcommented, Mar 24, 2021
  1. The benefit here is that I only have to pass the optional mapping type when I need to. I agree this is tiny for this example, but with multiple “const qualified” optional args in the function signature, this becomes a major convenience to the caller and the implementor

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.

  1. no comment

  2. This is generally a good rule, I want to keep it on. Mypy will catch some of these errors but not e.g.:

def whatever(self, map: MutableMapping[str, str] = {}) -> None:
    map["key"] ="value"

I have just taken a fork and will see if I can create something…

2reactions
eric-wiesercommented, Mar 24, 2021

and then at some stage mutated

If I am missing something please explain what I am missing in how mypy makes this safe.

Mypy will detect any attempt to mutate the dict and emit an error

def evil(d : MutableMapping[str, str]):
   d[2] = True

def whatever(self, optional_map: Mapping[str, str] = {}) -> None:
    optional_map[1] = True  # mypy errors here, `Mapping` does not implement `__setitem__`.
    evil(optional_map)  # mypy errors here too, `Mapping` is not a `MutableMapping`
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found