jcrocholl/pep8 — Issue #47: Warn type comparisons using the types module.
Author: lzyeval
Created: 2012-01-02 09:51:35+00:00
State: open

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

Many developers tend to import the types module and do something like "type(XXX) is types.IntType".

Looking at the types.py source code there are only type definitions and internal functions.

An Error should be triggered when the types module is imported.