AlenkaF commented on code in PR #55264:
URL: https://github.com/apache/arrow/pull/55264#discussion_r4492878328


##########
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),
+          lib.ChunkedArray)
+    check(assert_type(table[0], lib.Array | lib.ChunkedArray | lib.Table),
+          lib.ChunkedArray)
+    check(assert_type(table[:1], lib.Array | lib.ChunkedArray | lib.Table),
+          lib.Table)
+
+    check(assert_type(table.slice(0, 1), lib.Table), lib.Table)
+    check(assert_type(table.select(["a"]), lib.Table), lib.Table)
+    check(assert_type(table.select(["a", 1]), lib.Table), lib.Table)
+    check(assert_type(table.equals(None), bool), bool)
+    check(assert_type(table.combine_chunks(), lib.Table), lib.Table)
+    check(assert_type(table.to_batches(), list[lib.RecordBatch]), list)
+    check(assert_type(table.to_pydict(), dict[str, list]), dict)
+    check(assert_type(lib.Table.from_arrays([(1, 2)], names=["a"]), lib.Table),
+          lib.Table)
+
+    chunked = lib.chunked_array([[1, 2]])
+    check(assert_type(table.add_column(0, "c", chunked), lib.Table), lib.Table)
+    check(assert_type(table.add_column(0, "c", lib.array([1, 2])), lib.Table),
+          lib.Table)
+    check(assert_type(table.add_column(0, "c", [[1], [2]]), lib.Table),
+          lib.Table)
+    check(assert_type(table.add_column(0, "c", [lib.array([1, 2])]), 
lib.Table),
+          lib.Table)
+
+    table = lib.table(([1],), names=["a"])
+    check(assert_type(table, lib.Table), lib.Table)
+
+    chunked_struct = lib.chunked_array([[{"x": 1}]])
+    check(assert_type(lib.Table.from_struct_array(chunked_struct), lib.Table),
+          lib.Table)
+
+    # The reader isn't annotated yet, but must not reject valid usage.
+    check(table.to_reader().read_all(), lib.Table)
+    for batch in table.to_reader():
+        check(batch, lib.RecordBatch)
+
+
+def test_record_batch() -> None:
+    batch = lib.record_batch({"a": [1, 2]})  # from a dict
+    check(assert_type(batch, lib.RecordBatch), lib.RecordBatch)
+    check(assert_type(batch.num_rows, int), int)
+    check(assert_type(batch.column(0), lib.Array | lib.ChunkedArray),
+          lib.Array)
+    check(assert_type(batch.slice(1), lib.RecordBatch), lib.RecordBatch)
+    check(assert_type(lib.Table.from_batches([batch]), lib.Table), lib.Table)
+    check(assert_type(lib.concat_batches([batch, batch]), lib.RecordBatch),
+          lib.RecordBatch)
+    check(assert_type(batch.add_column(0, "b", lib.array([3, 4])),
+                      lib.RecordBatch),
+          lib.RecordBatch)
+    check(assert_type(batch.add_column(0, "b", [3, 4]), lib.RecordBatch),
+          lib.RecordBatch)
+    check(assert_type(batch.equals(batch), bool), bool)
+    # Any object can be compared, a non-batch returns False
+    check(assert_type(batch.equals(42), bool), bool)
+
+    batch = lib.record_batch([lib.array([1])], names=["a"])  # from a list
+    check(assert_type(batch, lib.RecordBatch), lib.RecordBatch)
+
+    struct_array = batch.to_struct_array()
+    check(assert_type(lib.RecordBatch.from_struct_array(struct_array),

Review Comment:
   
https://github.com/apache/arrow/pull/55264/commits/5114af567c9f5c9ea4d6ad08909c9b2ac4b20ef0



-- 
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]

Reply via email to