edgarrmondragon commented on code in PR #1026:
URL: https://github.com/apache/arrow-nanoarrow/pull/1026#discussion_r4633354574


##########
python/generate_type_stubs.sh:
##########
@@ -43,17 +47,20 @@ LICENSE='
 # under the License.
 '
 
-# Remove old stubs
-find src/nanoarrow -name "*.pyi" -delete
-
-# Generate new ones
-pushd src/nanoarrow
-for mod in $(find . -name "*.pyx" | sed -e "s|./||" -e "s|.pyx||"); do
-  stubgen --module "nanoarrow.${mod}" --include-docstrings -o ../../build/tmp
-  echo "$LICENSE" > "${mod}.pyi"
-  cat "../../build/tmp/nanoarrow/${mod}.pyi" >> "${mod}.pyi"
-  black "${mod}.pyi"
+modules=()
+sources=(*.pyx)
+for source in "${sources[@]}"; do
+  modules+=(--module "nanoarrow.${source%.pyx}")
 done
-popd
+stubgen "${modules[@]}" --include-docstrings -o "$GENERATED_DIR"
 
-popd
+# Only replace the source stubs after generation and formatting succeed.
+for source in "${sources[@]}"; do
+  stub="${source%.pyx}.pyi"
+  {
+    printf '%s\n' "$LICENSE"
+    cat "$GENERATED_DIR/nanoarrow/$stub"
+  } > "$GENERATED_DIR/$stub"
+done
+black "$GENERATED_DIR"/*.pyi
+cp "$GENERATED_DIR"/*.pyi .

Review Comment:
   Unlikely scenario, but worth considering:
   
   If a .pyx is deleted, its .pyi will persist. Instead of copying over 
existing files, how about deleting them entirely before copying the new stubs?



##########
python/src/nanoarrow/_array.pyi:
##########
@@ -87,6 +87,7 @@ class CArrayBuilder:
         """
     def append_bytes(self, *args, **kwargs): ...
     def append_strings(self, *args, **kwargs): ...
+    def ensure_buffers(self, n_buffers) -> CArrayBuilder: ...

Review Comment:
   Aren't this and `set_map_keys_sorted` below hand edits? i.e. wouldn't this 
change to `def ensure_buffers(self, *args, **kwargs): ...` when the stubs are 
regenerated with the script?



##########
python/tests/test_type_stubs.py:
##########
@@ -0,0 +1,67 @@
+# 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.
+
+import ast
+import importlib
+from pathlib import Path
+
+import pytest
+
+SOURCE_DIR = Path(__file__).resolve().parents[1] / "src" / "nanoarrow"
+
+
+def _public_declarations(body):
+    names = set()
+    for node in body:
+        if isinstance(node, (ast.ClassDef, ast.FunctionDef, 
ast.AsyncFunctionDef)):
+            names.add(node.name)
+        elif isinstance(node, ast.AnnAssign) and isinstance(node.target, 
ast.Name):
+            names.add(node.target.id)
+        elif isinstance(node, ast.Assign):
+            names.update(
+                target.id for target in node.targets if isinstance(target, 
ast.Name)
+            )
+    return {name for name in names if not name.startswith("_")}
+
+
[email protected](
+    "source", sorted(SOURCE_DIR.glob("*.pyx")), ids=lambda f: f.stem
+)
+def test_extension_stub_members(source):

Review Comment:
   This isn't ideal i.m.o. because it only validates the names, so signatures, 
types and staleness are not checked.



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