tadeja commented on code in PR #55264: URL: https://github.com/apache/arrow/pull/55264#discussion_r4632792851
########## python/pyarrow-stubs-tests/test_table.py: ########## @@ -0,0 +1,152 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# mypy: warn-unused-ignores + +from types import MappingProxyType + +import pytest + +import pyarrow.lib as lib + +from _check import TYPE_CHECKING_INVALID_USAGE, assert_type, check + + +def test_table() -> None: + table = lib.table({"a": [1, 2], "b": ["x", "y"]}) + check(assert_type(table, lib.Table), lib.Table) + check(assert_type(table.num_rows, int), int) + check(assert_type(table.num_columns, int), int) + check(assert_type(table.shape, tuple[int, int]), tuple) + check(assert_type(table.column_names, list[str]), list) + check(assert_type(table.schema, lib.Schema), lib.Schema) + check(assert_type(table.field("a"), lib.Field), lib.Field) + + # Columns aren't typed per tabular class, so Table and RecordBatch both + # return the union here. + check(assert_type(table.column("a"), lib.Array | lib.ChunkedArray), + lib.ChunkedArray) + check(assert_type(table["a"], lib.Array | lib.ChunkedArray | lib.Table), Review Comment: Hmm, it looks like `_PandasConvertible.to_pandas` could be sorted out without per-class overloads, but by making `_PandasConvertible` base **generic**. Public classes remain non-generic. What do you say, @AlenkaF, @jorisvandenbossche, @rok, ... OK? Tested fine like so: (+ from typing import Generic, ...) ```diff diff --git a/python/pyarrow-stubs/__lib/array.pyi b/python/pyarrow-stubs/__lib/array.pyi @@ -117,7 +126,10 @@ class ArrayStatistics(_Weakrefable): @property def is_max_exact(self) -> bool | None: ... -class _PandasConvertible(_Weakrefable): +# What to_pandas() returns: a Series for arrays, a DataFrame for tables. +_PandasT = TypeVar("_PandasT", pd.Series, pd.DataFrame) + +class _PandasConvertible(_Weakrefable, Generic[_PandasT]): def to_pandas( self, memory_pool: MemoryPool | None = None, @@ -136,9 +148,9 @@ class _PandasConvertible(_Weakrefable): maps_as_pydicts: MapsAsPydicts | None = None, types_mapper: Any = None, # Callable[[DataType], ExtensionDtype | None] | None coerce_temporal_nanoseconds: bool = False, - ) -> pd.Series | pd.DataFrame: ... + ) -> _PandasT: ... -class Array(_PandasConvertible): +class Array(_PandasConvertible[pd.Series]): __hash__: ClassVar[None] # type: ignore[assignment] def diff(self, other: Array) -> str: ... def cast( diff --git a/python/pyarrow-stubs/__lib/table.pyi b/python/pyarrow-stubs/__lib/table.pyi @@ -148,7 +148,7 @@ _NullarySelector: TypeAlias = tuple[()] _NarySelector: TypeAlias = list[str] | tuple[str, ...] _ColumnSelector: TypeAlias = _UnarySelector | _NullarySelector | _NarySelector -class ChunkedArray(_PandasConvertible): +class ChunkedArray(_PandasConvertible[pd.Series]): @property def type(self) -> DataType: ... def length(self) -> int: ... @@ -246,7 +246,7 @@ def chunked_array( type: DataType | str | None = None, ) -> ChunkedArray: ... -class _Tabular(_PandasConvertible): +class _Tabular(_PandasConvertible[pd.DataFrame]): def __array__( self, dtype: np.dtype[Any] | None = None, copy: bool | None = None ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
