Method's default argument shouldn't be mutable
http://docs.python-guide.org/en/latest/writing/gotchas/ Change-Id: I5e22e66de245da0bac1f35f81460ea9cfdc00772
This commit is contained in:
parent
47620e0e00
commit
49d28d5189
|
@ -10,7 +10,8 @@ Senlin Specific Commandments
|
|||
|
||||
- [S318] Change assertEqual(A, None) or assertEqual(None, A) by optimal assert
|
||||
like assertIsNone(A)
|
||||
- [N319] Ensure that jsonutils.%(fun)s must be used instead of json.%(fun)s
|
||||
- [S319] Ensure that jsonutils.%(fun)s must be used instead of json.%(fun)s
|
||||
- [S320] Method's default argument shouldn't be mutable!
|
||||
|
||||
Working on APIs
|
||||
---------------
|
||||
|
|
|
@ -14,6 +14,7 @@ import re
|
|||
|
||||
asse_equal_end_with_none_re = re.compile(r"assertEqual\(.*?,\s+None\)$")
|
||||
asse_equal_start_with_none_re = re.compile(r"assertEqual\(None,")
|
||||
mutable_default_args = re.compile(r"^\s*def .+\((.+=\{\}|.+=\[\])")
|
||||
|
||||
|
||||
def assert_equal_none(logical_line):
|
||||
|
@ -39,6 +40,13 @@ def use_jsonutils(logical_line, filename):
|
|||
yield (pos, msg % {'fun': f[:-1]})
|
||||
|
||||
|
||||
def no_mutable_default_args(logical_line):
|
||||
msg = "S320: Method's default argument shouldn't be mutable!"
|
||||
if mutable_default_args.match(logical_line):
|
||||
yield (0, msg)
|
||||
|
||||
|
||||
def factory(register):
|
||||
register(assert_equal_none)
|
||||
register(use_jsonutils)
|
||||
register(no_mutable_default_args)
|
||||
|
|
|
@ -16,7 +16,9 @@ import requests
|
|||
|
||||
def create_cluster(client, name, profile_id, desired_capacity,
|
||||
min_size=0, max_size=-1, parent=None,
|
||||
metadata={}, timeout=120):
|
||||
metadata=None, timeout=120):
|
||||
if not metadata:
|
||||
metadata = {}
|
||||
rel_url = 'clusters'
|
||||
status = [202]
|
||||
data = {
|
||||
|
@ -126,7 +128,9 @@ def delete_node(client, node_id):
|
|||
return
|
||||
|
||||
|
||||
def create_profile(client, name, spec, metadata={}):
|
||||
def create_profile(client, name, spec, metadata=None):
|
||||
if not metadata:
|
||||
metadata = {}
|
||||
rel_url = 'profiles'
|
||||
status = [201]
|
||||
data = {
|
||||
|
|
|
@ -38,3 +38,13 @@ class HackingTestCase(base.SenlinTestCase):
|
|||
"jsonx.%s(" % method, "./senlin/engine/cluster.py"))))
|
||||
self.assertEqual(0, len(list(checks.use_jsonutils(
|
||||
"json.dumb", "./senlin/engine/cluster.py"))))
|
||||
|
||||
def test_no_mutable_default_args(self):
|
||||
self.assertEqual(1, len(list(checks.no_mutable_default_args(
|
||||
"def create_cluster(mapping={}, **params)"))))
|
||||
|
||||
self.assertEqual(0, len(list(checks.no_mutable_default_args(
|
||||
"defined = []"))))
|
||||
|
||||
self.assertEqual(0, len(list(checks.no_mutable_default_args(
|
||||
"defined, undefined = [], {}"))))
|
||||
|
|
Loading…
Reference in New Issue