From df1b41d3d3887e1e8fe3c04aaef09c92bf299f0d Mon Sep 17 00:00:00 2001 From: Marius Gedminas Date: Thu, 27 Aug 2015 09:25:51 +0300 Subject: [PATCH] Avoid types.NoneType types.NoneType was removed in Python 3. None is a singleton in Python, so 'x is None' is equivalent to 'isinstance(x, NoneType)'. --- contrib/inventory/rax.py | 2 +- lib/ansible/playbook/helpers.py | 2 -- lib/ansible/playbook/role/__init__.py | 13 ++++++------- lib/ansible/plugins/action/include_vars.py | 2 -- lib/ansible/template/__init__.py | 3 +-- 5 files changed, 8 insertions(+), 14 deletions(-) diff --git a/contrib/inventory/rax.py b/contrib/inventory/rax.py index a42bbfcfef..c6b25f596e 100755 --- a/contrib/inventory/rax.py +++ b/contrib/inventory/rax.py @@ -245,7 +245,7 @@ def _list_into_cache(regions): if cs is None: warnings.warn( 'Connecting to Rackspace region "%s" has caused Pyrax to ' - 'return a NoneType. Is this a valid region?' % region, + 'return None. Is this a valid region?' % region, RuntimeWarning) continue for server in cs.servers.list(): diff --git a/lib/ansible/playbook/helpers.py b/lib/ansible/playbook/helpers.py index 98bef15e2a..98c9ffae30 100644 --- a/lib/ansible/playbook/helpers.py +++ b/lib/ansible/playbook/helpers.py @@ -20,8 +20,6 @@ __metaclass__ = type import os -from types import NoneType - from ansible.errors import AnsibleParserError from ansible.parsing.yaml.objects import AnsibleBaseYAMLObject, AnsibleSequence diff --git a/lib/ansible/playbook/role/__init__.py b/lib/ansible/playbook/role/__init__.py index 540cc6f57e..c406bd9404 100644 --- a/lib/ansible/playbook/role/__init__.py +++ b/lib/ansible/playbook/role/__init__.py @@ -25,7 +25,6 @@ import inspect import os from hashlib import sha1 -from types import NoneType from ansible.errors import AnsibleError, AnsibleParserError from ansible.parsing import DataLoader @@ -184,16 +183,16 @@ class Role(Base, Become, Conditional, Taggable): # vars and default vars are regular dictionaries self._role_vars = self._load_role_yaml('vars') - if not isinstance(self._role_vars, (dict, NoneType)): - raise AnsibleParserError("The vars/main.yml file for role '%s' must contain a dictionary of variables" % self._role_name) - elif self._role_vars is None: + if self._role_vars is None: self._role_vars = dict() + elif not isinstance(self._role_vars, dict): + raise AnsibleParserError("The vars/main.yml file for role '%s' must contain a dictionary of variables" % self._role_name) self._default_vars = self._load_role_yaml('defaults') - if not isinstance(self._default_vars, (dict, NoneType)): - raise AnsibleParserError("The default/main.yml file for role '%s' must contain a dictionary of variables" % self._role_name) - elif self._default_vars is None: + if self._default_vars is None: self._default_vars = dict() + elif not isinstance(self._default_vars, dict): + raise AnsibleParserError("The default/main.yml file for role '%s' must contain a dictionary of variables" % self._role_name) def _load_role_yaml(self, subdir): file_path = os.path.join(self._role_path, subdir) diff --git a/lib/ansible/plugins/action/include_vars.py b/lib/ansible/plugins/action/include_vars.py index 31d93e7acc..e0c1c088b8 100644 --- a/lib/ansible/plugins/action/include_vars.py +++ b/lib/ansible/plugins/action/include_vars.py @@ -19,8 +19,6 @@ __metaclass__ = type import os -from types import NoneType - from ansible.errors import AnsibleError from ansible.parsing import DataLoader from ansible.plugins.action import ActionBase diff --git a/lib/ansible/template/__init__.py b/lib/ansible/template/__init__.py index 1f4f401923..1793a2f43e 100644 --- a/lib/ansible/template/__init__.py +++ b/lib/ansible/template/__init__.py @@ -37,7 +37,6 @@ from ansible.template.vars import AnsibleJ2Vars from ansible.utils.debug import debug from numbers import Number -from types import NoneType __all__ = ['Templar'] @@ -188,7 +187,7 @@ class Templar: resolved_val = self._available_variables[var_name] if isinstance(resolved_val, NON_TEMPLATED_TYPES): return resolved_val - elif isinstance(resolved_val, NoneType): + elif resolved_val is None: return C.DEFAULT_NULL_REPRESENTATION result = self._do_template(variable, preserve_trailing_newlines=preserve_trailing_newlines, fail_on_undefined=fail_on_undefined, overrides=overrides)