Removes the use of mutables as default args
Passing mutable objects as default args is a known Python pitfall. We'd better avoid this. This commit changes mutable default args with None, then use 'arg = arg or {}', 'arg = arg or []'. For unit code which doesn't use the args , just set with None. This commit also adds hacking check. Change-Id: Ib4f195c9c00ca2c49579f9d62648bff7c8109fcf Closes-Bug: #1327473
This commit is contained in:
parent
456a07f60a
commit
76e1e02e15
|
@ -16,6 +16,7 @@ Octavia Specific Commandments
|
||||||
- [O322] Don't use author tags
|
- [O322] Don't use author tags
|
||||||
- [O323] Change assertEqual(True, A) or assertEqual(False, A) to the more
|
- [O323] Change assertEqual(True, A) or assertEqual(False, A) to the more
|
||||||
specific assertTrue(A) or assertFalse(A)
|
specific assertTrue(A) or assertFalse(A)
|
||||||
|
- [O324] Method's default argument shouldn't be mutable
|
||||||
- [O338] Change assertEqual(A in B, True), assertEqual(True, A in B),
|
- [O338] Change assertEqual(A in B, True), assertEqual(True, A in B),
|
||||||
assertEqual(A in B, False) or assertEqual(False, A in B) to the more
|
assertEqual(A in B, False) or assertEqual(False, A in B) to the more
|
||||||
specific assertIn/NotIn(A, B)
|
specific assertIn/NotIn(A, B)
|
||||||
|
|
|
@ -211,13 +211,13 @@ class SNI(BaseDataModel):
|
||||||
class TLSContainer(BaseDataModel):
|
class TLSContainer(BaseDataModel):
|
||||||
|
|
||||||
def __init__(self, id=None, primary_cn=None, certificate=None,
|
def __init__(self, id=None, primary_cn=None, certificate=None,
|
||||||
private_key=None, passphrase=None, intermediates=[]):
|
private_key=None, passphrase=None, intermediates=None):
|
||||||
self.id = id
|
self.id = id
|
||||||
self.primary_cn = primary_cn
|
self.primary_cn = primary_cn
|
||||||
self.certificate = certificate
|
self.certificate = certificate
|
||||||
self.private_key = private_key
|
self.private_key = private_key
|
||||||
self.passphrase = passphrase
|
self.passphrase = passphrase
|
||||||
self.intermediates = intermediates
|
self.intermediates = intermediates or []
|
||||||
|
|
||||||
|
|
||||||
class Amphora(BaseDataModel):
|
class Amphora(BaseDataModel):
|
||||||
|
|
|
@ -66,6 +66,7 @@ assert_equal_with_true_re = re.compile(
|
||||||
r"assertEqual\(True,")
|
r"assertEqual\(True,")
|
||||||
assert_equal_with_false_re = re.compile(
|
assert_equal_with_false_re = re.compile(
|
||||||
r"assertEqual\(False,")
|
r"assertEqual\(False,")
|
||||||
|
mutable_default_args = re.compile(r"^\s*def .+\((.+=\{\}|.+=\[\])")
|
||||||
|
|
||||||
|
|
||||||
def _directory_to_check_translation(filename):
|
def _directory_to_check_translation(filename):
|
||||||
|
@ -155,6 +156,12 @@ def assert_equal_true_or_false(logical_line):
|
||||||
"sentences not allowed")
|
"sentences not allowed")
|
||||||
|
|
||||||
|
|
||||||
|
def no_mutable_default_args(logical_line):
|
||||||
|
msg = "O324: Method's default argument shouldn't be mutable!"
|
||||||
|
if mutable_default_args.match(logical_line):
|
||||||
|
yield (0, msg)
|
||||||
|
|
||||||
|
|
||||||
def assert_equal_in(logical_line):
|
def assert_equal_in(logical_line):
|
||||||
"""Check for assertEqual(A in B, True), assertEqual(True, A in B),
|
"""Check for assertEqual(A in B, True), assertEqual(True, A in B),
|
||||||
|
|
||||||
|
@ -177,4 +184,5 @@ def factory(register):
|
||||||
register(use_jsonutils)
|
register(use_jsonutils)
|
||||||
register(no_author_tags)
|
register(no_author_tags)
|
||||||
register(assert_equal_true_or_false)
|
register(assert_equal_true_or_false)
|
||||||
|
register(no_mutable_default_args)
|
||||||
register(assert_equal_in)
|
register(assert_equal_in)
|
||||||
|
|
|
@ -205,13 +205,13 @@ def sample_tls_sni_containers_tuple(tls_container=None):
|
||||||
|
|
||||||
|
|
||||||
def sample_tls_container_tuple(id='cont_id_1', certificate=None,
|
def sample_tls_container_tuple(id='cont_id_1', certificate=None,
|
||||||
private_key=None, intermediates=[],
|
private_key=None, intermediates=None,
|
||||||
primary_cn=None):
|
primary_cn=None):
|
||||||
sc = collections.namedtuple(
|
sc = collections.namedtuple(
|
||||||
'tls_container',
|
'tls_container',
|
||||||
'id, certificate, private_key, intermediates, primary_cn')
|
'id, certificate, private_key, intermediates, primary_cn')
|
||||||
return sc(id=id, certificate=certificate, private_key=private_key,
|
return sc(id=id, certificate=certificate, private_key=private_key,
|
||||||
intermediates=intermediates, primary_cn=primary_cn)
|
intermediates=intermediates or [], primary_cn=primary_cn)
|
||||||
|
|
||||||
|
|
||||||
def sample_pool_tuple(proto=None, monitor=True, persistence=True,
|
def sample_pool_tuple(proto=None, monitor=True, persistence=True,
|
||||||
|
|
Loading…
Reference in New Issue