Skip to content

Commit 04c4092

Browse files
authored
chore(python): Add minimal type stubs for .pyx files (#468)
These are generic stubs that are not particularly good; however, they are low-maintenance and do the trick of listing methods and Enum values for the limited set of user-facing functions that return a `CBuffer` or `CArrayView`. They are also nice for local development since the user-facing interface wraps the Cython code. <img width="401" alt="Screenshot 2024-05-14 at 12 36 18 PM" src="https://github.com/apache/arrow-nanoarrow/assets/10995762/b855fe9f-aa3b-4ad2-9b91-a5dfca07fa90">
1 parent 65e90b7 commit 04c4092

File tree

5 files changed

+606
-0
lines changed

5 files changed

+606
-0
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717

1818
dist/* linguist-generated
1919
extensions/nanoarrow_ipc/thirdparty/* linguist-vendored
20+
python/src/nanoarrow/*.pxi linguist-generated

.github/workflows/python.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ jobs:
5858
run: |
5959
pytest python/tests -v -s
6060
61+
- name: Check type stubs
62+
if: success() && matrix.python-version == '3.12'
63+
run: |
64+
pip install mypy "black==22.3.0"
65+
python/generate_type_stubs.sh
66+
67+
if git diff --name-only | grep -e "\\.pxi$"; then
68+
echo "Type stubs were changed. Update them with python/generate_type_stubs.sh."
69+
fi
70+
71+
stubtest nanoarrow._lib
72+
stubtest nanoarrow._ipc_lib
73+
6174
- name: Run doctests
6275
if: success() && matrix.python-version == '3.12'
6376
run: |

python/generate_type_stubs.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
SOURCE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
19+
20+
# Generates src/nanoarrow/*.pxi given the currently installed copy of
21+
# nanoarrow. Requires mypy and black (where black is pinned to the
22+
# same version as in pre-commit)
23+
24+
pushd "${SOURCE_DIR}"
25+
26+
# Generate stubs using mypy
27+
stubgen --module nanoarrow._lib --include-docstrings -o src
28+
stubgen --module nanoarrow._ipc_lib --include-docstrings -o src
29+
30+
# Reformat stubs
31+
black src/nanoarrow/*.pyi
32+
33+
popd

python/src/nanoarrow/_ipc_lib.pyi

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import _cython_3_0_10
2+
from _typeshed import Incomplete
3+
4+
__reduce_cython__: _cython_3_0_10.cython_function_or_method
5+
__setstate_cython__: _cython_3_0_10.cython_function_or_method
6+
__test__: dict
7+
init_array_stream: _cython_3_0_10.cython_function_or_method
8+
9+
class CIpcInputStream:
10+
@classmethod
11+
def __init__(cls, *args, **kwargs) -> None:
12+
"""Create and return a new object. See help(type) for accurate signature."""
13+
@staticmethod
14+
def from_readable(*args, **kwargs): ...
15+
def is_valid(self, *args, **kwargs): ...
16+
def release(self, *args, **kwargs): ...
17+
def __reduce__(self): ...
18+
19+
class PyInputStreamPrivate:
20+
close_obj: Incomplete
21+
obj: Incomplete
22+
@classmethod
23+
def __init__(cls, *args, **kwargs) -> None:
24+
"""Create and return a new object. See help(type) for accurate signature."""
25+
def set_buffer(self, *args, **kwargs): ...
26+
def __len__(self) -> int:
27+
"""Return len(self)."""
28+
def __reduce__(self): ...

0 commit comments

Comments
 (0)