Skip to content

Commit bc94a18

Browse files
committed
Accept plain Array for offsets, type codes, masks and run ends
pa.array() is annotated as returning the base Array, so parameters typed as Int32Array, Int8Array or BooleanArray forced users to cast. Annotate them as Array, and widen run ends to what the runtime accepts. DictionaryArray.from_arrays never accepts an Arrow array as mask, so drop BooleanArray there.
1 parent 5a026a0 commit bc94a18

2 files changed

Lines changed: 70 additions & 14 deletions

File tree

‎python/pyarrow-stubs-tests/test_array.py‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,59 @@ def test_buffer_constructors_invalid_usage() -> None:
268268
# Fields are passed as `fields`, `names` only takes strings
269269
pa.StructArray.from_arrays(
270270
children, names=[pa.field("a", pa.int64())])
271+
272+
273+
def test_array_arguments_without_cast() -> None:
274+
import numpy as np
275+
276+
# pa.array() is annotated as returning the base Array, so parameters
277+
# must accept it without requiring a cast to the exact subclass
278+
values = pa.array([1, 2, 3])
279+
offsets = pa.array([0, 1, 3], pa.int32())
280+
sizes = pa.array([1, 2], pa.int32())
281+
check(assert_type(pa.ListArray.from_arrays(offsets, values), pa.ListArray),
282+
pa.ListArray)
283+
# Offsets and sizes of another integer type are cast at runtime
284+
check(assert_type(pa.ListArray.from_arrays(pa.array([0, 1, 3]), values),
285+
pa.ListArray),
286+
pa.ListArray)
287+
check(assert_type(pa.ListArray.from_arrays(np.array([0, 1, 3]), values),
288+
pa.ListArray),
289+
pa.ListArray)
290+
check(assert_type(pa.ListArray.from_arrays([0, None, 3], values),
291+
pa.ListArray),
292+
pa.ListArray)
293+
check(assert_type(pa.LargeListArray.from_arrays(offsets, values),
294+
pa.LargeListArray),
295+
pa.LargeListArray)
296+
check(assert_type(pa.ListViewArray.from_arrays(offsets[:2], sizes, values),
297+
pa.ListViewArray),
298+
pa.ListViewArray)
299+
check(assert_type(pa.LargeListViewArray.from_arrays(offsets[:2], sizes, values),
300+
pa.LargeListViewArray),
301+
pa.LargeListViewArray)
302+
check(assert_type(pa.MapArray.from_arrays(offsets, values, values), pa.MapArray),
303+
pa.MapArray)
304+
305+
type_codes = pa.array([0, 0], pa.int8())
306+
value_offsets = pa.array([0, 1], pa.int32())
307+
children = [pa.array([1, 2])]
308+
check(assert_type(pa.UnionArray.from_dense(type_codes, value_offsets, children),
309+
pa.UnionArray),
310+
pa.UnionArray)
311+
check(assert_type(pa.UnionArray.from_sparse(type_codes, children),
312+
pa.UnionArray),
313+
pa.UnionArray)
314+
315+
mask = pa.array([True, False])
316+
check(assert_type(pa.StructArray.from_arrays(children, names=["a"], mask=mask),
317+
pa.StructArray),
318+
pa.StructArray)
319+
check(assert_type(pa.RunEndEncodedArray.from_arrays(pa.array([1, 3]), ["a", "b"]),
320+
pa.RunEndEncodedArray),
321+
pa.RunEndEncodedArray)
322+
check(assert_type(pa.RunEndEncodedArray.from_arrays(np.array([1, 3]), ["a", "b"]),
323+
pa.RunEndEncodedArray),
324+
pa.RunEndEncodedArray)
325+
check(assert_type(pa.Bool8Array.from_storage(type_codes), pa.Bool8Array),
326+
pa.Bool8Array)

‎python/pyarrow-stubs/__lib/array.pyi‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ class BaseListArray(Array):
317317
class ListArray(BaseListArray):
318318
@staticmethod
319319
def from_arrays(
320-
offsets: Int32Array | Iterable[int | None] | np.ndarray,
320+
offsets: Array | Iterable[int | None] | np.ndarray,
321321
values: Array | Iterable[Any],
322322
type: DataType | None = None,
323323
pool: MemoryPool | None = None,
@@ -332,7 +332,7 @@ class ListArray(BaseListArray):
332332
class LargeListArray(BaseListArray):
333333
@staticmethod
334334
def from_arrays(
335-
offsets: Int64Array | Iterable[int | None] | np.ndarray,
335+
offsets: Array | Iterable[int | None] | np.ndarray,
336336
values: Array | Iterable[Any],
337337
type: DataType | None = None,
338338
pool: MemoryPool | None = None,
@@ -347,8 +347,8 @@ class LargeListArray(BaseListArray):
347347
class ListViewArray(BaseListArray):
348348
@staticmethod
349349
def from_arrays(
350-
offsets: Int32Array | Iterable[int | None] | np.ndarray,
351-
sizes: Int32Array | Iterable[int | None] | np.ndarray,
350+
offsets: Array | Iterable[int | None] | np.ndarray,
351+
sizes: Array | Iterable[int | None] | np.ndarray,
352352
values: Array | Iterable[Any],
353353
type: DataType | None = None,
354354
pool: MemoryPool | None = None,
@@ -365,8 +365,8 @@ class ListViewArray(BaseListArray):
365365
class LargeListViewArray(BaseListArray):
366366
@staticmethod
367367
def from_arrays(
368-
offsets: Int64Array | Iterable[int | None] | np.ndarray,
369-
sizes: Int64Array | Iterable[int | None] | np.ndarray,
368+
offsets: Array | Iterable[int | None] | np.ndarray,
369+
sizes: Array | Iterable[int | None] | np.ndarray,
370370
values: Array | Iterable[Any],
371371
type: DataType | None = None,
372372
pool: MemoryPool | None = None,
@@ -395,7 +395,7 @@ class FixedSizeListArray(BaseListArray):
395395
class MapArray(ListArray):
396396
@staticmethod
397397
def from_arrays( # type: ignore[override]
398-
offsets: Int32Array | Iterable[int | None] | np.ndarray,
398+
offsets: Array | Iterable[int | None] | np.ndarray,
399399
keys: Array | Iterable[Any],
400400
items: Array | Iterable[Any],
401401
type: DataType | None = None,
@@ -415,15 +415,15 @@ class UnionArray(Array):
415415
def offsets(self) -> Int32Array: ...
416416
@staticmethod
417417
def from_dense(
418-
types: Int8Array,
419-
value_offsets: Int32Array,
418+
types: Array,
419+
value_offsets: Array,
420420
children: list[Array],
421421
field_names: list[str] | None = None,
422422
type_codes: list[int] | None = None,
423423
) -> UnionArray: ...
424424
@staticmethod
425425
def from_sparse(
426-
types: Int8Array,
426+
types: Array,
427427
children: list[Array],
428428
field_names: list[str] | None = None,
429429
type_codes: list[int] | None = None,
@@ -483,7 +483,7 @@ class DictionaryArray(Array):
483483
def from_arrays(
484484
indices: Indices,
485485
dictionary: Array | np.ndarray | pd.Series | list[Any],
486-
mask: np.ndarray | pd.Series | BooleanArray | None = None,
486+
mask: np.ndarray | pd.Series | None = None,
487487
ordered: bool = False,
488488
from_pandas: bool = False,
489489
safe: bool = True,
@@ -498,7 +498,7 @@ class StructArray(Array):
498498
arrays: Iterable[Array | np.ndarray | list],
499499
names: Sequence[str] | list[Field] | None = None,
500500
fields: list[Field] | None = None,
501-
mask: BooleanArray | None = None,
501+
mask: Array | None = None,
502502
memory_pool: MemoryPool | None = None,
503503
type: StructType | None = None,
504504
) -> StructArray: ...
@@ -513,7 +513,7 @@ class StructArray(Array):
513513
class RunEndEncodedArray(Array):
514514
@staticmethod
515515
def from_arrays(
516-
run_ends: Int16Array | Int32Array | Int64Array | list[int],
516+
run_ends: Array | Sequence[int] | np.ndarray,
517517
values: Array | list[Any],
518518
type: DataType | None = None,
519519
) -> RunEndEncodedArray: ...
@@ -556,7 +556,7 @@ class Bool8Array(ExtensionArray):
556556
self, zero_copy_only: bool = ..., writable: bool = ...
557557
) -> np.ndarray: ...
558558
@staticmethod
559-
def from_storage(storage: Int8Array) -> Bool8Array: ... # type: ignore[override]
559+
def from_storage(storage: Array) -> Bool8Array: ... # type: ignore[override]
560560
@staticmethod
561561
def from_numpy(obj: np.ndarray) -> Bool8Array: ...
562562

0 commit comments

Comments
 (0)