Remove uses of assert in production code (#32079)

* Remove uses of assert in production code

* Fix assertion

* Add code smell test for assertions, currently limited to lib/ansible

* Fix assertion

* Add docs for no-assert

* Remove new assert from enos

* Fix assert in module_utils.connection
This commit is contained in:
Matt Martz
2017-11-13 10:51:18 -06:00
committed by ansibot
parent 464ded80f5
commit 99d4f5bab4
38 changed files with 195 additions and 89 deletions

View File

@@ -98,7 +98,8 @@ def get_connection(module):
def to_commands(module, commands):
assert isinstance(commands, list), 'argument must be of type <list>'
if not isinstance(commands, list):
raise AssertionError('argument must be of type <list>')
transform = EntityCollection(module, command_spec)
commands = transform(commands)

View File

@@ -2248,7 +2248,8 @@ class AnsibleModule(object):
def fail_json(self, **kwargs):
''' return from the module, with an error message '''
assert 'msg' in kwargs, "implementation error -- msg to explain the error is required"
if 'msg' not in kwargs:
raise AssertionError("implementation error -- msg to explain the error is required")
kwargs['failed'] = True
# add traceback if debug or high verbosity and it is missing

View File

@@ -95,7 +95,8 @@ class ConnectionError(Exception):
class Connection:
def __init__(self, socket_path):
assert socket_path is not None, 'socket_path must be a value'
if socket_path is None:
raise AssertionError('socket_path must be a value')
self.socket_path = socket_path
def __getattr__(self, name):

View File

@@ -115,7 +115,8 @@ def get_config(module, flags=None):
def to_commands(module, commands):
assert isinstance(commands, list), 'argument must be of type <list>'
if not isinstance(commands, list):
raise AssertionError('argument must be of type <list>')
transform = EntityCollection(module, command_spec)
commands = transform(commands)

View File

@@ -67,7 +67,8 @@ def get_connection(module):
def to_commands(module, commands):
assert isinstance(commands, list), 'argument must be of type <list>'
if not isinstance(commands, list):
raise AssertionError('argument must be of type <list>')
transform = EntityCollection(module, command_spec)
commands = transform(commands)

View File

@@ -97,7 +97,8 @@ class ConfigLine(object):
return len(self._parents) > 0
def add_child(self, obj):
assert isinstance(obj, ConfigLine), 'child must be of type `ConfigLine`'
if not isinstance(obj, ConfigLine):
raise AssertionError('child must be of type `ConfigLine`')
self._children.append(obj)
@@ -263,7 +264,8 @@ class NetworkConfig(object):
return item
def get_block(self, path):
assert isinstance(path, list), 'path argument must be a list object'
if not isinstance(path, list):
raise AssertionError('path argument must be a list object')
obj = self.get_object(path)
if not obj:
raise ValueError('path does not exist in config')

View File

@@ -222,8 +222,10 @@ def dict_diff(base, comparable):
:returns: new dict object with differences
"""
assert isinstance(base, dict), "`base` must be of type <dict>"
assert isinstance(comparable, dict), "`comparable` must be of type <dict>"
if not isinstance(base, dict):
raise AssertionError("`base` must be of type <dict>")
if not isinstance(comparable, dict):
raise AssertionError("`comparable` must be of type <dict>")
updates = dict()
@@ -257,8 +259,10 @@ def dict_merge(base, other):
:returns: new combined dict object
"""
assert isinstance(base, dict), "`base` must be of type <dict>"
assert isinstance(other, dict), "`other` must be of type <dict>"
if not isinstance(base, dict):
raise AssertionError("`base` must be of type <dict>")
if not isinstance(other, dict):
raise AssertionError("`other` must be of type <dict>")
combined = dict()
@@ -306,7 +310,8 @@ def conditional(expr, val, cast=None):
op, arg = match.groups()
else:
op = 'eq'
assert (' ' not in str(expr)), 'invalid expression: cannot contain spaces'
if ' ' in str(expr):
raise AssertionError('invalid expression: cannot contain spaces')
arg = expr
if cast is None and val is not None:

View File

@@ -273,7 +273,8 @@ def umc_module_for_edit(module, object_dn, superordinate=None):
def create_containers_and_parents(container_dn):
"""Create a container and if needed the parents containers"""
import univention.admin.uexceptions as uexcp
assert container_dn.startswith("cn=")
if not container_dn.startswith("cn="):
raise AssertionError()
try:
parent = ldap_dn_tree_parent(container_dn)
obj = umc_module_for_add(