From c4d1f7e687fa4f24f799feec81e17a48aa84afac Mon Sep 17 00:00:00 2001 From: zhurong Date: Wed, 16 Dec 2015 23:27:06 +0800 Subject: [PATCH] Remove arguments "{}" and "[]" in function definitions remove mutable default arguments "{}" and "[]" in function definitions refer to:http://docs.python-guide.org/en/latest/writing/gotchas/ Change-Id: Ib03d9820a259b2cbd83aa8bd675ac6153cf78ebd Closes-Bug: #1527080 --- murano/common/policy.py | 5 +++- murano/dsl/exceptions.py | 4 ++- murano/services/actions.py | 4 ++- murano/tests/unit/api/base.py | 27 ++++++++++++++----- .../db/services/test_templates_service.py | 4 ++- murano/tests/unit/engine/system/test_agent.py | 4 ++- murano/tests/unit/utils.py | 4 ++- 7 files changed, 39 insertions(+), 13 deletions(-) diff --git a/murano/common/policy.py b/murano/common/policy.py index 0ac43785..44bde8f4 100644 --- a/murano/common/policy.py +++ b/murano/common/policy.py @@ -62,7 +62,10 @@ def init(default_rule=None, use_conf=True): _ENFORCER.load_rules() -def check(rule, ctxt, target={}, do_raise=True, exc=exceptions.HTTPForbidden): +def check(rule, ctxt, target=None, do_raise=True, + exc=exceptions.HTTPForbidden): + if target is None: + target = {} creds = ctxt.to_dict() try: diff --git a/murano/dsl/exceptions.py b/murano/dsl/exceptions.py index c923ff84..b98ae4e9 100644 --- a/murano/dsl/exceptions.py +++ b/murano/dsl/exceptions.py @@ -44,7 +44,9 @@ class NoMethodFound(Exception): class NoClassFound(Exception): - def __init__(self, name, packages=[]): + def __init__(self, name, packages=None): + if packages is None: + packages = [] packages = ', '.join("{0}/{1}".format(p.name, p.version) for p in packages) super(NoClassFound, self).__init__( diff --git a/murano/services/actions.py b/murano/services/actions.py index 92a2f027..63d73ec8 100644 --- a/murano/services/actions.py +++ b/murano/services/actions.py @@ -67,7 +67,9 @@ class ActionServices(object): return task_id @staticmethod - def execute(action_id, session, unit, token, args={}): + def execute(action_id, session, unit, token, args=None): + if args is None: + args = {} environment = actions_db.get_environment(session, unit) action = ActionServices.find_action(session.description, action_id) if action is None: diff --git a/murano/tests/unit/api/base.py b/murano/tests/unit/api/base.py index 4c5894fd..30419eb4 100644 --- a/murano/tests/unit/api/base.py +++ b/murano/tests/unit/api/base.py @@ -89,11 +89,13 @@ class MuranoApiTestCase(base.MuranoWithDBTestCase, FakeLogMixin): super(MuranoApiTestCase, self).tearDown() timeutils.utcnow.override_time = None - def _stub_uuid(self, values=[]): + def _stub_uuid(self, values=None): class FakeUUID(object): def __init__(self, v): self.hex = v + if values is None: + values = [] mock_uuid4 = mock.patch('uuid.uuid4').start() mock_uuid4.side_effect = [FakeUUID(v) for v in values] return mock_uuid4 @@ -128,7 +130,9 @@ class ControllerTest(object): self._policy_check_expectations = [] self._actual_policy_checks = [] - def wrap_policy_check(rule, ctxt, target={}, **kwargs): + def wrap_policy_check(rule, ctxt, target=None, **kwargs): + if target is None: + target = {} self._actual_policy_checks.append((rule, target)) return real_policy_check(rule, ctxt, target=target, **kwargs) @@ -184,13 +188,16 @@ class ControllerTest(object): def _delete(self, path, params=None, user=DEFAULT_USER, tenant=DEFAULT_TENANT): - params = params or {} + if params is None: + params = {} return self._simple_request(path, params=params, method='DELETE', user=user, tenant=tenant) def _data_request(self, path, data, content_type='application/json', - method='POST', params={}, + method='POST', params=None, user=DEFAULT_USER, tenant=DEFAULT_TENANT): + if params is None: + params = {} environ = self._environ(path) environ['REQUEST_METHOD'] = method @@ -207,20 +214,26 @@ class ControllerTest(object): return req - def _post(self, path, data, content_type='application/json', params={}, + def _post(self, path, data, content_type='application/json', params=None, user=DEFAULT_USER, tenant=DEFAULT_TENANT): + if params is None: + params = {} return self._data_request(path, data, content_type, params=params, user=user, tenant=tenant) - def _put(self, path, data, content_type='application/json', params={}, + def _put(self, path, data, content_type='application/json', params=None, user=DEFAULT_USER, tenant=DEFAULT_TENANT): + if params is None: + params = {} return self._data_request(path, data, content_type, method='PUT', params=params, user=user, tenant=tenant) def _set_policy_rules(self, rules): policy.set_rules(rules, default_rule='default') - def expect_policy_check(self, action, target={}): + def expect_policy_check(self, action, target=None): + if target is None: + target = {} self._policy_check_expectations.append((action, target)) def _assert_policy_checks(self): diff --git a/murano/tests/unit/db/services/test_templates_service.py b/murano/tests/unit/db/services/test_templates_service.py index 6861d930..833e262f 100644 --- a/murano/tests/unit/db/services/test_templates_service.py +++ b/murano/tests/unit/db/services/test_templates_service.py @@ -85,11 +85,13 @@ class TestTemplateServices(base.MuranoWithDBTestCase, self.test_create_template() self.template_services.delete("template_id") - def _stub_uuid(self, values=[]): + def _stub_uuid(self, values=None): class FakeUUID(object): def __init__(self, v): self.hex = v + if values is None: + values = [] mock_uuid4 = mock.patch('uuid.uuid4').start() mock_uuid4.side_effect = [FakeUUID(v) for v in values] return mock_uuid4 diff --git a/murano/tests/unit/engine/system/test_agent.py b/murano/tests/unit/engine/system/test_agent.py index db41e4c8..5f0b8624 100644 --- a/murano/tests/unit/engine/system/test_agent.py +++ b/murano/tests/unit/engine/system/test_agent.py @@ -300,11 +300,13 @@ class TestExecutionPlan(base.MuranoTestCase): 'Version': '1.0.0' } - def _stub_uuid(self, values=[]): + def _stub_uuid(self, values=None): class FakeUUID(object): def __init__(self, v): self.hex = v + if values is None: + values = [] mock_uuid4 = mock.patch('uuid.uuid4').start() mock_uuid4.side_effect = [FakeUUID(v) for v in values] return mock_uuid4 diff --git a/murano/tests/unit/utils.py b/murano/tests/unit/utils.py index a3d88c35..5e4af318 100644 --- a/murano/tests/unit/utils.py +++ b/murano/tests/unit/utils.py @@ -18,8 +18,10 @@ from murano.db import session def dummy_context(user='test_username', tenant_id='test_tenant_id', - password='password', roles=[], user_id=None, + password='password', roles=None, user_id=None, is_admin=False, request_id='dummy-request'): + if roles is None: + roles = [] # NOTE(kzaitsev) passing non-False value by default to request_id, to # prevent generation during tests. return context.RequestContext.from_dict({