Remove direct type comparisons

Bug #910763 reported by Zhongyue Luo
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
OpenStack Compute (nova)
Fix Released
Undecided
Zhongyue Luo

Bug Description

Type comparisons should use isinstance with built-in factory functions.

Use "if not isinstance(items, list)" rather than "if not isinstance(items, types.ListType)"

Revision history for this message
Zhongyue Luo (zyluo) wrote :
Changed in nova:
assignee: nobody → LZY (lzyeval)
Revision history for this message
Zhongyue Luo (zyluo) wrote :

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):

Zhongyue Luo (zyluo)
summary: - Remove type comparisons using types.*
+ Remove direct type comparisons
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to nova (master)

Fix proposed to branch: master
Review: https://review.openstack.org/2729

Changed in nova:
status: New → In Progress
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to nova (master)

Reviewed: https://review.openstack.org/2729
Committed: http://github.com/openstack/nova/commit/ae1654bc5910dba5f02f49bc57a49442b9a5f8d6
Submitter: Jenkins
Branch: master

commit ae1654bc5910dba5f02f49bc57a49442b9a5f8d6
Author: lzyeval <email address hidden>
Date: Mon Jan 2 17:31:36 2012 +0800

    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

Changed in nova:
status: In Progress → Fix Committed
Thierry Carrez (ttx)
Changed in nova:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.