Repository: internetarchive/openlibrary

Issue:
(PYL-W0102) Dangerous default argument
## Description
Mutable Default Arguments: https://docs.python-guide.org/writing/gotchas/

Do not use a mutable like `list` or `dictionary` as a default value to an argument. Python’s default arguments are evaluated once when the function is defined. Using a mutable default argument and mutating it will mutate that object for all future calls to the function as well.

## Occurrences
There are 23 occurrences of this issue in the repository.

See all occurrences on DeepSource &rarr; [deepsource.io/gh/HarshCasper/openlibrary-1/issue/PYL-W0102/occurrences/](https://deepsource.io/gh/HarshCasper/openlibrary-1/issue/PYL-W0102/occurrences/)



Discussion:
@cclauss I would like to work on this issue.
@cclauss If I am not wrong, I need to make the following changes
**Incorrect:**

`def request(self, path, method='GET', query={}):`
**Correct:**
```
def request(self, path, method='GET', query=None):
    if query is None:
        query = {}            #edit: previously I wrote [] by mistake
```
`query = query or {}`  # i.e.: if it is any _falsey_ value like `None`, `False`, `0`, `0.0`, `[]`, `set()`, then we want to set it to `{}`.
> `query = query or {}` # i.e.: if it is any _falsey_ value like `None`, `False`, `0`, `0.0`, `[]`, `set()`, then we want to set it to `{}`.

@cclauss Is this correct?

```
def request(self, path, method='GET', query=None):
    query = query or {}
```
and how to fix this:
`def convert_dict(d, mapping=convertions):`
Please fix the easy ones and leave the complex ones for future pull requests.
@cclauss I'd like to have a go at it.

My sense is that #4228 fix almost all of these issues with the exception of one file.

Oh... okay then
openlibrary/plugins/upstream/adapter.py lines 108 and 124 would close this issue.
@Yashs911 or @captain-panda Could one of you please create a PR on the remaining file?
> @Yashs911 or @captain-panda Could one of you please create a PR on the remaining file?

@cclauss I would like to, but I don't know what all changes do I need to make

will this work?
```
def convert_dict(d, mapping=None):
    mapping = mapping or convertions
```

Resolving patch:
diff --git a/openlibrary/plugins/upstream/adapter.py b/openlibrary/plugins/upstream/adapter.py
--- a/openlibrary/plugins/upstream/adapter.py
+++ b/openlibrary/plugins/upstream/adapter.py
@@ -105,11 +105,13 @@ def process_error(self):
                 d['key'] = unconvert_key(d['key'])
             self.output = simplejson.dumps(d)
 
-def convert_key(key, mapping=convertions):
+
+def convert_key(key, mapping=None):
     """
         >>> convert_key("/authors/OL1A", {'/authors/': '/a/'})
         '/a/OL1A'
     """
+    mapping = mapping or convertions
     if key is None:
         return None
     elif key == '/':
@@ -121,11 +123,13 @@ def convert_key(key, mapping=convertions):
             return key2
     return key
 
-def convert_dict(d, mapping=convertions):
+
+def convert_dict(d, mapping=None):
     """
         >>> convert_dict({'author': {'key': '/authors/OL1A'}}, {'/authors/': '/a/'})
         {'author': {'key': '/a/OL1A'}}
     """
+    mapping = mapping or convertions
     if isinstance(d, dict):
         if 'key' in d:
             d['key'] = convert_key(d['key'], mapping)


Test patch: