PEP8 remove direct type comparisons

Fixes bug #910763

According to PEP8,
- Object type comparisons should always use isinstance() instead
      of comparing types directly.

        Yes: if isinstance(obj, int):

        No: if type(obj) is type(1):

      When checking if an object is a string, keep in mind that it might be a
      unicode string too! In Python 2.3, str and unicode have a common base
      class, basestring, so you can do:

        if isinstance(obj, basestring):

Change-Id: I7c0fdecf99872f5b8f72b2c2ed4f5c539c33def1
This commit is contained in:
lzyeval
2012-01-02 17:31:36 +08:00
parent 2bb09d22e0
commit 38b630a93a
8 changed files with 12 additions and 13 deletions

View File

@@ -1084,7 +1084,7 @@ class ServiceCommands(object):
{"method": "show_host_resources", {"method": "show_host_resources",
"args": {"host": host}}) "args": {"host": host}})
if type(result) != dict: if not isinstance(result, dict):
print _('An unexpected error has occurred.') print _('An unexpected error has occurred.')
print _('[Result]'), result print _('[Result]'), result
else: else:

View File

@@ -85,7 +85,7 @@ def _clean(attr):
"""Clean attr for insertion into ldap""" """Clean attr for insertion into ldap"""
if attr is None: if attr is None:
return None return None
if type(attr) is unicode: if isinstance(attr, unicode):
return str(attr) return str(attr)
return attr return attr

View File

@@ -962,9 +962,9 @@ class ConfigOpts(object):
:param value: the string value, or list of string values :param value: the string value, or list of string values
:returns: the substituted string(s) :returns: the substituted string(s)
""" """
if type(value) is list: if isinstance(value, list):
return [self._substitute(i) for i in value] return [self._substitute(i) for i in value]
elif type(value) is str: elif isinstance(value, str):
tmpl = string.Template(value) tmpl = string.Template(value)
return tmpl.safe_substitute(self.StrSubWrapper(self)) return tmpl.safe_substitute(self.StrSubWrapper(self))
else: else:

View File

@@ -24,11 +24,11 @@ No fan-out support yet.
""" """
import inspect
import json import json
import sys import sys
import time import time
import traceback import traceback
import types
import uuid import uuid
from carrot import connection as carrot_connection from carrot import connection as carrot_connection
@@ -284,7 +284,7 @@ class AdapterConsumer(Consumer):
try: try:
rval = node_func(context=ctxt, **node_args) rval = node_func(context=ctxt, **node_args)
# Check if the result was a generator # Check if the result was a generator
if isinstance(rval, types.GeneratorType): if inspect.isgenerator(rval):
for x in rval: for x in rval:
ctxt.reply(x, None) ctxt.reply(x, None)
else: else:

View File

@@ -17,9 +17,9 @@
queues. Casts will block, but this is very useful for tests. queues. Casts will block, but this is very useful for tests.
""" """
import inspect
import sys import sys
import traceback import traceback
import types
from nova import context from nova import context
from nova.rpc import common as rpc_common from nova.rpc import common as rpc_common
@@ -60,7 +60,7 @@ class Consumer(object):
# if ending not 'sent'...we might have more data to # if ending not 'sent'...we might have more data to
# return from the function itself # return from the function itself
if not ctxt._done: if not ctxt._done:
if isinstance(rval, types.GeneratorType): if inspect.isgenerator(rval):
for val in rval: for val in rval:
yield val yield val
else: else:

View File

@@ -18,11 +18,11 @@ import kombu
import kombu.entity import kombu.entity
import kombu.messaging import kombu.messaging
import kombu.connection import kombu.connection
import inspect
import itertools import itertools
import sys import sys
import time import time
import traceback import traceback
import types
import uuid import uuid
import eventlet import eventlet
@@ -651,7 +651,7 @@ class ProxyCallback(object):
try: try:
rval = node_func(context=ctxt, **node_args) rval = node_func(context=ctxt, **node_args)
# Check if the result was a generator # Check if the result was a generator
if isinstance(rval, types.GeneratorType): if inspect.isgenerator(rval):
for x in rval: for x in rval:
ctxt.reply(x, None) ctxt.reply(x, None)
else: else:

View File

@@ -21,7 +21,6 @@ Weighing Functions.
import json import json
import operator import operator
import types
import M2Crypto import M2Crypto
@@ -358,7 +357,7 @@ class DistributedScheduler(driver.Scheduler):
return getattr(filters, nm) return getattr(filters, nm)
return [get_itm(itm) for itm in dir(filters) return [get_itm(itm) for itm in dir(filters)
if (type(get_itm(itm)) is types.TypeType) if isinstance(get_itm(itm), type)
and issubclass(get_itm(itm), filters.AbstractHostFilter) and issubclass(get_itm(itm), filters.AbstractHostFilter)
and get_itm(itm) is not filters.AbstractHostFilter] and get_itm(itm) is not filters.AbstractHostFilter]

View File

@@ -140,7 +140,7 @@ class ReadOnlyDict(UserDict.IterableUserDict):
return return
elif isinstance(source, UserDict.UserDict): elif isinstance(source, UserDict.UserDict):
self.data = source.data self.data = source.data
elif isinstance(source, type({})): elif isinstance(source, dict):
self.data = source self.data = source
else: else:
raise TypeError raise TypeError