This is an automated email from the ASF dual-hosted git repository.

wesm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new dc80a76  ARROW-2695: [Python] Prevent calling scalar constructors 
directly
dc80a76 is described below

commit dc80a768c0a15e62998ccd32d8353d2035302cb6
Author: Krisztián Szűcs <[email protected]>
AuthorDate: Mon Jun 11 11:27:31 2018 -0400

    ARROW-2695: [Python] Prevent calling scalar constructors directly
    
    Author: Krisztián Szűcs <[email protected]>
    
    Closes #2132 from kszucs/ARROW-2695 and squashes the following commits:
    
    2d996c50 <Krisztián Szűcs> flake8 again
    433382ff <Krisztián Szűcs> flake8
    d20caf88 <Krisztián Szűcs> test array subclasses too
    d04a5a53 <Krisztián Szűcs> prevent calling scalar constructors directly
---
 python/pyarrow/__init__.py           |  6 ++--
 python/pyarrow/array.pxi             |  5 ++--
 python/pyarrow/scalar.pxi            | 26 +++++++++--------
 python/pyarrow/tests/test_misc.py    | 54 +++++++++++++++++++++++++++++++++++-
 python/pyarrow/tests/test_scalars.py | 13 +--------
 5 files changed, 75 insertions(+), 29 deletions(-)

diff --git a/python/pyarrow/__init__.py b/python/pyarrow/__init__.py
index 89dfd03..20254c2 100644
--- a/python/pyarrow/__init__.py
+++ b/python/pyarrow/__init__.py
@@ -80,8 +80,10 @@ from pyarrow.lib import (null, bool_,
                          UInt8Value, UInt16Value, UInt32Value, UInt64Value,
                          HalfFloatValue, FloatValue, DoubleValue, ListValue,
                          BinaryValue, StringValue, FixedSizeBinaryValue,
-                         DecimalValue,
-                         Date32Value, Date64Value, TimestampValue)
+                         DecimalValue, UnionValue, StructValue, 
DictionaryValue,
+                         Date32Value, Date64Value,
+                         Time32Value, Time64Value,
+                         TimestampValue)
 
 # Buffers, allocation
 from pyarrow.lib import (Buffer, ResizableBuffer, foreign_buffer, py_buffer,
diff --git a/python/pyarrow/array.pxi b/python/pyarrow/array.pxi
index f7fd24d..9d14e1e 100644
--- a/python/pyarrow/array.pxi
+++ b/python/pyarrow/array.pxi
@@ -348,8 +348,9 @@ def _restore_array(data):
 cdef class Array:
 
     def __init__(self):
-        raise TypeError("Do not call Array's constructor directly, use one "
-                        "of the `pyarrow.Array.from_*` functions instead.")
+        raise TypeError("Do not call {}'s constructor directly, use one of "
+                        "the `pyarrow.Array.from_*` functions instead."
+                        .format(self.__class__.__name__))
 
     cdef void init(self, const shared_ptr[CArray]& sp_array):
         self.sp_array = sp_array
diff --git a/python/pyarrow/scalar.pxi b/python/pyarrow/scalar.pxi
index adb5fa6..964eadb 100644
--- a/python/pyarrow/scalar.pxi
+++ b/python/pyarrow/scalar.pxi
@@ -42,6 +42,11 @@ NA = NAType()
 
 cdef class ArrayValue(Scalar):
 
+    def __init__(self):
+        raise TypeError("Do not call {}'s constructor directly, use array "
+                        "subscription instead."
+                        .format(self.__class__.__name__))
+
     cdef void init(self, DataType type, const shared_ptr[CArray]& sp_array,
                    int64_t index):
         self.type = type
@@ -51,14 +56,7 @@ cdef class ArrayValue(Scalar):
     cdef void _set_array(self, const shared_ptr[CArray]& sp_array):
         self.sp_array = sp_array
 
-    def _check_null(self):
-        if self.sp_array.get() == NULL:
-            raise ReferenceError(
-                'ArrayValue instance not propertly initialized '
-                '(references NULL pointer)')
-
     def __repr__(self):
-        self._check_null()
         if hasattr(self, 'as_py'):
             return repr(self.as_py())
         else:
@@ -74,7 +72,8 @@ cdef class ArrayValue(Scalar):
                 "Cannot compare Arrow values that don't support as_py()")
 
     def __hash__(self):
-            return hash(self.as_py())
+        return hash(self.as_py())
+
 
 cdef class BooleanValue(ArrayValue):
 
@@ -402,6 +401,7 @@ cdef class StructValue(ArrayValue):
             zip(child_names, wrapped_arrays)
         }
 
+
 cdef class DictionaryValue(ArrayValue):
 
     def as_py(self):
@@ -457,12 +457,14 @@ cdef dict _scalar_classes = {
 
 cdef object box_scalar(DataType type, const shared_ptr[CArray]& sp_array,
                        int64_t index):
-    cdef ArrayValue val
+    cdef ArrayValue value
+
     if type.type.id() == _Type_NA:
         return NA
     elif sp_array.get().IsNull(index):
         return NA
     else:
-        val = _scalar_classes[type.type.id()]()
-        val.init(type, sp_array, index)
-        return val
+        klass = _scalar_classes[type.type.id()]
+        value = klass.__new__(klass)
+        value.init(type, sp_array, index)
+        return value
diff --git a/python/pyarrow/tests/test_misc.py 
b/python/pyarrow/tests/test_misc.py
index 3b17f4c..26717e1 100644
--- a/python/pyarrow/tests/test_misc.py
+++ b/python/pyarrow/tests/test_misc.py
@@ -55,7 +55,59 @@ def test_cpu_count():
     pa.lib.TimestampType,
     pa.lib.Decimal128Type,
     pa.lib.DictionaryType,
-    pa.lib.FixedSizeBinaryType
+    pa.lib.FixedSizeBinaryType,
+    pa.NullArray,
+    pa.NumericArray,
+    pa.IntegerArray,
+    pa.FloatingPointArray,
+    pa.BooleanArray,
+    pa.Int8Array,
+    pa.Int16Array,
+    pa.Int32Array,
+    pa.Int64Array,
+    pa.UInt8Array,
+    pa.UInt16Array,
+    pa.UInt32Array,
+    pa.UInt64Array,
+    pa.ListArray,
+    pa.UnionArray,
+    pa.BinaryArray,
+    pa.StringArray,
+    pa.FixedSizeBinaryArray,
+    pa.DictionaryArray,
+    pa.Date32Array,
+    pa.Date64Array,
+    pa.TimestampArray,
+    pa.Time32Array,
+    pa.Time64Array,
+    pa.Decimal128Array,
+    pa.StructArray,
+    pa.ArrayValue,
+    pa.BooleanValue,
+    pa.Int8Value,
+    pa.Int16Value,
+    pa.Int32Value,
+    pa.Int64Value,
+    pa.UInt8Value,
+    pa.UInt16Value,
+    pa.UInt32Value,
+    pa.UInt64Value,
+    pa.HalfFloatValue,
+    pa.FloatValue,
+    pa.DoubleValue,
+    pa.DecimalValue,
+    pa.Date32Value,
+    pa.Date64Value,
+    pa.Time32Value,
+    pa.Time64Value,
+    pa.TimestampValue,
+    pa.StringValue,
+    pa.BinaryValue,
+    pa.FixedSizeBinaryValue,
+    pa.ListValue,
+    pa.UnionValue,
+    pa.StructValue,
+    pa.DictionaryValue
 ])
 def test_extension_type_constructor_errors(klass):
     # ARROW-2638: prevent calling extension class constructors directly
diff --git a/python/pyarrow/tests/test_scalars.py 
b/python/pyarrow/tests/test_scalars.py
index 0a5c72a..0b91072 100644
--- a/python/pyarrow/tests/test_scalars.py
+++ b/python/pyarrow/tests/test_scalars.py
@@ -28,20 +28,9 @@ import pyarrow as pa
 class TestScalars(unittest.TestCase):
 
     def test_null_singleton(self):
-        with self.assertRaises(Exception):
+        with pytest.raises(Exception):
             pa.NAType()
 
-    def test_ctor_null_check(self):
-        # ARROW-1155
-        with pytest.raises(ReferenceError):
-            repr(pa.Int16Value())
-
-        with pytest.raises(ReferenceError):
-            str(pa.Int16Value())
-
-        with pytest.raises(ReferenceError):
-            repr(pa.StringValue())
-
     def test_bool(self):
         arr = pa.array([True, None, False, None])
 

-- 
To stop receiving notification emails like this one, please contact
[email protected].

Reply via email to