paddyroddy commented on code in PR #55264:
URL: https://github.com/apache/arrow/pull/55264#discussion_r4600584977
##########
.github/workflows/dev.yml:
##########
@@ -67,6 +67,12 @@ jobs:
- name: Run pre-commit
run: |
pre-commit run --all-files --color=always --show-diff-on-failure
+ - name: Run pre-commit for type stubs
+ env:
+ # The previous step already ran the ruff hooks.
+ SKIP: ruff-check,ruff-format
+ run: |
+ pre-commit run --hook-stage manual python-stubs --all-files
--color=always --show-diff-on-failure
Review Comment:
Would recommend [prek](https://github.com/j178/prek) over pre-commit. More
feature rich, friendlier community and faster. Acts as a drop-in replacement.
```suggestion
prek run --hook-stage manual python-stubs --all-files
--color=always --show-diff-on-failure
```
##########
.pre-commit-config.yaml:
##########
@@ -145,6 +145,70 @@ repos:
(
?^python/pyarrow/vendored/|
)
+ # Linting, formatting and type checking of the type stubs. The type checkers
+ # don't run on commit, only on request (the Lint CI job runs them too):
+ # pre-commit run --hook-stage manual python-stubs --all-files
+ # The type checkers run with uvx, in the environment of
+ # python/requirements-stubs.txt. They don't need PyArrow installed: the
+ # modules that aren't annotated yet, e.g. pyarrow.compute, are treated as Any
+ # (see python/pyproject.toml).
+ - repo: https://github.com/astral-sh/ruff-pre-commit
+ rev: v0.17.2
Review Comment:
Are we keeping these up-to-date anywhere? @renovatebot or @dependabot as
options, I guess @pre-commit-ci too (although not a fan of anything @pre-commit
personally).
##########
python/pyarrow-stubs-tests/_check.py:
##########
@@ -0,0 +1,48 @@
+# 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.
+
+"""Helpers for the type annotation tests in this directory.
+
+Each test pairs a static and a runtime check of the same expression::
+
+ check(assert_type(pa.list_(pa.int32()), pa.ListType), pa.ListType)
+
+``assert_type`` is verified by every type checker we run in CI (mypy,
+pyright and ty) and is a no-op at runtime. ``check`` runs under pytest and
+verifies that the value really is an instance of the given class, so the
+stubs can't drift away from what pyarrow actually returns.
+
+Code that type checkers should *reject* goes under ``if TYPE_CHECKING:``
+with a ``# type: ignore[<mypy code>]`` comment. The block never runs, and
+mypy and pyright are configured to report unused ignores in these tests, so
+the test fails if the usage stops being an error.
+"""
+
+from typing import Any, TypeVar, assert_type
+
+__all__ = ["assert_type", "check"]
+
+_T = TypeVar("_T")
+
+
+def check(actual: _T, klass: type[Any] | tuple[type[Any], ...]) -> _T:
+ """Assert that ``actual`` is an instance of ``klass`` and return it."""
Review Comment:
What is `klass`?
##########
python/requirements-stubs.txt:
##########
@@ -0,0 +1,20 @@
+# Type checkers and the type stubs of PyArrow's dependencies, pinned so that
Review Comment:
How will these versions be maintained?
##########
docs/source/developers/python/development.rst:
##########
@@ -114,6 +114,18 @@ Running Type Checkers
We support multiple type checkers. Their configurations are in
``pyproject.toml``.
+The easiest way to run all of them is with ``pre-commit``, which installs
+the same versions of the type checkers and stub packages for everyone. This
+also lints and formats the stubs with ruff:
+
+.. code-block::
+
+ $ pre-commit run --hook-stage manual python-stubs --all-files
Review Comment:
Works for me (with `prek` at least).
##########
python/requirements-stubs.txt:
##########
@@ -0,0 +1,20 @@
+# Type checkers and the type stubs of PyArrow's dependencies, pinned so that
Review Comment:
Could this instead be done through a lock file (`uv.lock` for example)?
##########
python/pyarrow/tests/test_schema.py:
##########
@@ -23,7 +23,7 @@
try:
import numpy as np
except ImportError:
- np = None
+ np = None # type: ignore[assignment]
Review Comment:
In the event NumPy isn't installed, `np` will be initialised as `None` and
then lines such as `np.object_` won't work. Is this just for type checking?
Seem unusual otherwise.
--
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]