Skip to content

Commit

Permalink
address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche committed Apr 8, 2024
1 parent 3518d80 commit a641017
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
4 changes: 2 additions & 2 deletions docs/source/python/extending_types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ under the hood, you can implement the following methods on those objects:

- ``__arrow_c_schema__`` for schema or type-like objects.
- ``__arrow_c_array__`` for arrays and record batches (contiguous tables).
- ``__arrow_c_stream__`` for chunked arrays or tables, or streams of data.
- ``__arrow_c_stream__`` for chunked arrays, tables and streams of data.

Those methods return `PyCapsule <https://docs.python.org/3/c-api/capsule.html>`__
objects, and more details on the exact semantics can be found in the
Expand All @@ -57,7 +57,7 @@ For consuming data through this protocol with PyArrow, the following constructor
can be used to create the various PyArrow objects:

+----------------------------+-----------------------------------------------+--------------------+
| Result class | Mapped Arrow type | Supported protocol |
| Result class | PyArrow constructor | Supported protocol |
+============================+===============================================+====================+
| :class:`Array` | :func:`pyarrow.array` | array |
+----------------------------+-----------------------------------------------+--------------------+
Expand Down
4 changes: 3 additions & 1 deletion python/pyarrow/tests/test_cffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,5 +701,7 @@ def test_roundtrip_chunked_array_capsule_requested_schema():

requested_type = pa.int64()
requested_capsule = requested_type.__arrow_c_schema__()
with pytest.raises(ValueError):
with pytest.raises(
ValueError, match="Could not cast string to requested type int64"
):
chunked.__arrow_c_stream__(requested_capsule)
23 changes: 23 additions & 0 deletions python/pyarrow/tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,14 @@ def test_recordbatch_dunder_init():
pa.RecordBatch()


class SchemaWrapper:
def __init__(self, schema):
self.schema = schema

def __arrow_c_schema__(self):
return self.schema.__arrow_c_schema__()


def test_chunked_array_c_array_interface():
class ArrayWrapper:
def __init__(self, array):
Expand All @@ -513,6 +521,9 @@ def __arrow_c_array__(self, requested_schema=None):
result = pa.chunked_array(wrapper, type=pa.int16())
assert result == chunked.cast(pa.int16())

result = pa.chunked_array(wrapper, type=SchemaWrapper(pa.int16()))
assert result == chunked.cast(pa.int16())


def test_chunked_array_c_stream_interface():
class ChunkedArrayWrapper:
Expand All @@ -533,6 +544,9 @@ def __arrow_c_stream__(self, requested_schema=None):
result = pa.chunked_array(wrapper, type=pa.int16())
assert result == data.cast(pa.int16())

result = pa.chunked_array(wrapper, type=SchemaWrapper(pa.int16()))
assert result == data.cast(pa.int16())


def test_recordbatch_c_array_interface():
class BatchWrapper:
Expand Down Expand Up @@ -561,6 +575,9 @@ def __arrow_c_array__(self, requested_schema=None):
], names=['a'])
assert result == expected

result = pa.record_batch(wrapper, schema=SchemaWrapper(castable_schema))
assert result == expected


def test_table_c_array_interface():
class BatchWrapper:
Expand Down Expand Up @@ -590,6 +607,9 @@ def __arrow_c_array__(self, requested_schema=None):
})
assert result == expected

result = pa.table(wrapper, schema=SchemaWrapper(castable_schema))
assert result == expected


def test_table_c_stream_interface():
class StreamWrapper:
Expand Down Expand Up @@ -621,6 +641,9 @@ def __arrow_c_stream__(self, requested_schema=None):
result = pa.table(wrapper, schema=good_schema)
assert result == expected.cast(good_schema)

result = pa.table(wrapper, schema=SchemaWrapper(good_schema))
assert result == expected.cast(good_schema)

# If schema doesn't match, raises NotImplementedError
with pytest.raises(
pa.lib.ArrowTypeError, match="Field 0 cannot be cast"
Expand Down

0 comments on commit a641017

Please sign in to comment.