Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to Python 3 #150

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/readability/do_not_compare_types_use_isinstance.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ The function ``isinstance`` is the best-equipped to handle type checking because
Anti-pattern
------------

The ``if`` statement below uses the pattern ``if type(OBJECT) is types.TYPE`` to compare a ``Rectangle`` object to a built-in type (``ListType`` in this example). This is not the preferred pattern for comparing types.
The ``if`` statement below uses the pattern ``if type(OBJECT) is typing.TYPE`` to compare a ``Rectangle`` object to a built-in type (``List`` in this example). This is not the preferred pattern for comparing types.

.. code:: python

import types
import typing

class Rectangle(object):
def __init__(self, width, height):
Expand All @@ -20,14 +20,14 @@ The ``if`` statement below uses the pattern ``if type(OBJECT) is types.TYPE`` to
r = Rectangle(3, 4)

# bad
if type(r) is types.ListType:
if type(r) is typing.List:
print("object r is a list")

Note that the following situation will not raise the error, although it should.

.. code:: python

import types
import typing

class Rectangle(object):
def __init__(self, width, height):
Expand Down Expand Up @@ -55,7 +55,7 @@ The preferred pattern for comparing types is the built-in function ``isinstance`

.. code:: python

import types
import typing

class Rectangle(object):
def __init__(self, width, height):
Expand All @@ -65,13 +65,13 @@ The preferred pattern for comparing types is the built-in function ``isinstance`
r = Rectangle(3, 4)

# good
if isinstance(r, types.ListType):
if isinstance(r, typing.List):
print("object r is a list")

References
----------

- `Stack Overflow: Differences between isinstance() and type() in Python <http://stackoverflow.com/questions/1549801/differences-between-isinstance-and-type-in-python>`_

- `typing <https://docs.python.org/3/library/typing.html>`_ — Support for type hints (since Python 3.5)