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
commit 99d4f5bab4
38 changed files with 195 additions and 89 deletions

View file

@ -32,7 +32,7 @@ except ImportError:
from jinja2.exceptions import UndefinedError
from ansible import constants as C
from ansible.errors import AnsibleError, AnsibleParserError, AnsibleUndefinedVariable, AnsibleFileNotFound
from ansible.errors import AnsibleError, AnsibleParserError, AnsibleUndefinedVariable, AnsibleFileNotFound, AnsibleAssertionError
from ansible.inventory.host import Host
from ansible.inventory.helpers import sort_groups, get_group_vars
from ansible.module_utils._text import to_native
@ -132,7 +132,8 @@ class VariableManager:
@extra_vars.setter
def extra_vars(self, value):
''' ensures a clean copy of the extra_vars are used to set the value '''
assert isinstance(value, MutableMapping), "the type of 'value' for extra_vars should be a MutableMapping, but is a %s" % type(value)
if not isinstance(value, MutableMapping):
raise AnsibleAssertionError("the type of 'value' for extra_vars should be a MutableMapping, but is a %s" % type(value))
self._extra_vars = value.copy()
def set_inventory(self, inventory):
@ -146,7 +147,8 @@ class VariableManager:
@options_vars.setter
def options_vars(self, value):
''' ensures a clean copy of the options_vars are used to set the value '''
assert isinstance(value, dict), "the type of 'value' for options_vars should be a dict, but is a %s" % type(value)
if not isinstance(value, dict):
raise AnsibleAssertionError("the type of 'value' for options_vars should be a dict, but is a %s" % type(value))
self._options_vars = value.copy()
def _preprocess_vars(self, a):
@ -592,7 +594,8 @@ class VariableManager:
Sets or updates the given facts for a host in the fact cache.
'''
assert isinstance(facts, dict), "the type of 'facts' to set for host_facts should be a dict but is a %s" % type(facts)
if not isinstance(facts, dict):
raise AnsibleAssertionError("the type of 'facts' to set for host_facts should be a dict but is a %s" % type(facts))
if host.name not in self._fact_cache:
self._fact_cache[host.name] = facts
@ -607,7 +610,8 @@ class VariableManager:
Sets or updates the given facts for a host in the fact cache.
'''
assert isinstance(facts, dict), "the type of 'facts' to set for nonpersistent_facts should be a dict but is a %s" % type(facts)
if not isinstance(facts, dict):
raise AnsibleAssertionError("the type of 'facts' to set for nonpersistent_facts should be a dict but is a %s" % type(facts))
if host.name not in self._nonpersistent_fact_cache:
self._nonpersistent_fact_cache[host.name] = facts