[NumPy-Tickets] [NumPy] #1950: Redundant numeric type classes lead to unreliable behavior of isinstance()

NumPy Trac numpy-tickets@scipy....
Fri Sep 9 10:06:21 CDT 2011


#1950: Redundant numeric type classes lead to unreliable behavior of isinstance()
---------------------------+------------------------------------------------
 Reporter:  stereotype441  |       Owner:  somebody
     Type:  defect         |      Status:  new     
 Priority:  normal         |   Milestone:  1.6.2   
Component:  numpy.core     |     Version:  1.6.0   
 Keywords:                 |  
---------------------------+------------------------------------------------
 This bug was discovered during the development of the Piglit OpenGL test
 suite (see https://bugs.freedesktop.org/show_bug.cgi?id=40697)

 Numpy seems to define multiple classes called "numpy.int32", which are
 considered distinct by Python's isinstance() function.  This makes it
 unreliable to use isinstance() to figure out the type of a numpy numeric
 value.  For example, this is the behavior I see on Windows using using
 numpy 1.6.1:

 {{{
 Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]
 on win32
 Type "help", "copyright", "credits" or "license" for more information.
 >>> import numpy
 >>> numpy.version.version
 '1.6.1'
 >>> x = numpy.int32(5)
 >>> y = numpy.abs(x)
 >>> type(x)
 <type 'numpy.int32'>
 >>> type(y)
 <type 'numpy.int32'>
 >>> isinstance(x, numpy.int32)
 True
 >>> isinstance(y, numpy.int32)
 False
 >>> type(x) == type(y)
 False
 }}}

 I would have expected the last two "False" outputs to be "True".  And when
 I run the same experiment on Fedora Linux (which is currently shipping
 numpy 1.5.1), that is indeed what I see.  Since I'm not running the same
 version of numpy on Linux and Windows, I'm not sure whether this problem
 is linked to numpy version or to platform.

 I have seen similar problems with uint32.

 You can see further evidence that there are multiple redundant numeric
 type classes by typing "help(numpy)" from the command prompt.  When I do
 this on Windows, I see duplicate copies of the following numeric classes:
 complex128, complex192, complex64, float16, float32, float64, float96,
 int16, int32, int64, int8, datetime64, timedelta64, uint16, uint32,
 uint64, and uint8.  Surprisingly, I see similar duplication even on Fedora
 Linux, even though it doesn't exhibit this bug.

 This arose in our project because we were trying to use isinstance() to
 tell the type of a value that might be one of several numpy numeric types,
 or might by a Python built-in type.  We are planning to work around the
 problem by using isinstance(value, numpy.numeric) to detect whether the
 value is a numpy numeric type, and then using value.dtype to figure out
 which numeric type it is.  But it seems like this extra work shouldn't be
 necessary--isinstance(value, numpy.int32) ought to be sufficient to
 determine whether a value is a numpy 32-bit int.

-- 
Ticket URL: <http://projects.scipy.org/numpy/ticket/1950>
NumPy <http://projects.scipy.org/numpy>
My example project


More information about the NumPy-Tickets mailing list