Fix mutable default arguments in tests

Python’s default arguments are evaluated only once
when the function is defined, not each time the
function is called. This means that if you use a
mutable default argument (like list and dict) and
mutate it, you will and have mutated that object
for all future calls to the function as well.

More details about this wrong usage here:
http://docs.python-guide.org/en/latest/writing/gotchas/#mutable-default-arguments

In unit tests, most FakeXXX classes' methods take
mutable arguments with default values [] or {}.
We should change them to None.

Change-Id: Iea833b66aa1379829511ad5c6d4432b72f3488e2
Closed-bug: #1550320
This commit is contained in:
Tang Chen
2016-04-15 15:46:19 +08:00
parent b094ec550d
commit 71c042b275

View File

@@ -142,7 +142,7 @@ class FakeModule(object):
class FakeResource(object):
def __init__(self, manager=None, info={}, loaded=False, methods={}):
def __init__(self, manager=None, info=None, loaded=False, methods=None):
"""Set attributes and methods for a resource.
:param manager:
@@ -154,6 +154,9 @@ class FakeResource(object):
:param Dictionary methods:
A dictionary with all methods
"""
info = info or {}
methods = methods or {}
self.__name__ = type(self).__name__
self.manager = manager
self._info = info
@@ -189,9 +192,12 @@ class FakeResource(object):
class FakeResponse(requests.Response):
def __init__(self, headers={}, status_code=200, data=None, encoding=None):
def __init__(self, headers=None, status_code=200,
data=None, encoding=None):
super(FakeResponse, self).__init__()
headers = headers or {}
self.status_code = status_code
self.headers.update(headers)