Skip to content

Commit

Permalink
ENH: add support for python ABCs
Browse files Browse the repository at this point in the history
Beginning with version 2.6, python supports abstract base classes,
which contain a class hierarchy for numbers. This class hierarchy is
very similar to the one of numpy, so it is very easy to register
the numpy type hierarchy with the python type hierarchy.

This patch adds those registrations and also adds unit tests for it.
  • Loading branch information
Martin Teichmann committed Mar 25, 2014
1 parent 8409b4c commit 2d73ff3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
6 changes: 6 additions & 0 deletions doc/release/1.9.0-notes.rst
Expand Up @@ -105,6 +105,12 @@ for ``tostring`` which exports arrays as ``bytes``. This is more consistent
in Python 3 where ``str`` and ``bytes`` are not the same.


compatibility to python ``numbers`` module
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All numerical numpy types are now registered with the type hierarchy in
the python ``numbers`` module.


Improvements
============

Expand Down
7 changes: 7 additions & 0 deletions numpy/core/numerictypes.py
Expand Up @@ -98,6 +98,7 @@
import types as _types
import sys
from numpy.compat import bytes, long
import numbers

# we don't export these for import *, but we do want them accessible
# as numerictypes.bool, etc.
Expand Down Expand Up @@ -960,6 +961,12 @@ def _can_coerce_all(dtypelist, start=0):
thisind += 1
return None

def _register_types():
numbers.Integral.register(integer)
numbers.Complex.register(inexact)
numbers.Real.register(floating)
_register_types()

def find_common_type(array_types, scalar_types):
"""
Determine common type following standard coercion rules.
Expand Down
45 changes: 45 additions & 0 deletions numpy/core/tests/test_abc.py
@@ -0,0 +1,45 @@
from __future__ import division, absolute_import, print_function

import numpy as np
from numpy.testing import TestCase, assert_

import numbers
from numpy.core.numerictypes import sctypes

class ABC(TestCase):
def test_floats(self):
for t in sctypes['float']:
assert_(isinstance(t(), numbers.Real),
"{0} is not instance of Real".format(t.__name__))
assert_(issubclass(t, numbers.Real),
"{0} is not subclass of Real".format(t.__name__))
assert_(not isinstance(t(), numbers.Rational),
"{0} is instance of Rational".format(t.__name__))
assert_(not issubclass(t, numbers.Rational),
"{0} is subclass of Rational".format(t.__name__))

def test_complex(self):
for t in sctypes['complex']:
assert_(isinstance(t(), numbers.Complex),
"{0} is not instance of Complex".format(t.__name__))
assert_(issubclass(t, numbers.Complex),
"{0} is not subclass of Complex".format(t.__name__))
assert_(not isinstance(t(), numbers.Real),
"{0} is instance of Real".format(t.__name__))
assert_(not issubclass(t, numbers.Real),
"{0} is subclass of Real".format(t.__name__))

def test_int(self):
for t in sctypes['int']:
assert_(isinstance(t(), numbers.Integral),
"{0} is not instance of Integral".format(t.__name__))
assert_(issubclass(t, numbers.Integral),
"{0} is not subclass of Integral".format(t.__name__))

def test_uint(self):
for t in sctypes['uint']:
assert_(isinstance(t(), numbers.Integral),
"{0} is not instance of Integral".format(t.__name__))
assert_(issubclass(t, numbers.Integral),
"{0} is not subclass of Integral".format(t.__name__))

0 comments on commit 2d73ff3

Please sign in to comment.