Skip to content

GH-49194: [Python][Annotations] Compute module - #49201

Open
rok wants to merge 35 commits into
apache:mainfrom
rok:pyarrow-stubs-pr4-compute
Open

rok wants to merge 35 commits into
apache:mainfrom
rok:pyarrow-stubs-pr4-compute

Conversation

@rok

@rok rok commented Feb 9, 2026 •

Copy link
Copy Markdown
Member

Rationale for this change

Closes: #49194

What changes are included in this PR?

Adds type annotation stubs for compute, _compute, _compute_docstring, and _acero modules. Includes source fixes in compute.py and acero.py, and type-ignore annotations in related tests.

Are these changes tested?

Yes, by CI.

Are there any user-facing changes?

Compute module functions will be type annotated.

@github-actions

github-actions Bot commented Feb 9, 2026

Copy link
Copy Markdown

⚠️ GitHub issue #49194 has been automatically assigned in GitHub to PR creator.

rok added a commit to rok/arrow that referenced this pull request Nov 13, 2026
Add type annotation stubs for compute, _compute, _compute_docstring,
and _acero modules.

These are the stubs of apacheGH-49201 as they were, at their original paths.
The type checker accommodations that apacheGH-49201 made in compute.py,
acero.py and the PyArrow tests are left out, as the type checkers no
longer run on pyarrow/ (see apacheGH-55264).
rok added a commit to rok/arrow that referenced this pull request Nov 13, 2026
Add type annotation stubs for compute, _compute, _compute_docstring,
and _acero modules.

These are the stubs of apacheGH-49201 as they were, at their original paths.
The type checker accommodations that apacheGH-49201 made in compute.py,
acero.py and the PyArrow tests are left out, as the type checkers no
longer run on pyarrow/ (see apacheGH-55264).
rok added a commit to rok/arrow that referenced this pull request Nov 13, 2026
Add type annotation stubs for compute, _compute, _compute_docstring,
and _acero modules.

These are the stubs of apacheGH-49201 as they were, at their original paths.
The type checker accommodations that apacheGH-49201 made in compute.py,
acero.py and the PyArrow tests are left out, as the type checkers no
longer run on pyarrow/ (see apacheGH-55264).
@rok
rok force-pushed the pyarrow-stubs-pr4-compute branch from d3f0fb7 to a78bedf Compare November 13, 2026 13:06
@rok
rok force-pushed the pyarrow-stubs-pr4-compute branch 2 times, most recently from 29a6b0b to fb3204a Compare November 13, 2026 13:29
@rok

rok commented Nov 17, 2026

Copy link
Copy Markdown
Member Author

Given there were changes in the data types PR you might want to rebase or merge them here @AlenkaF.

@AlenkaF
AlenkaF force-pushed the pyarrow-stubs-pr4-compute branch from e8779e3 to 300602b Compare November 18, 2026 05:21
@AlenkaF

AlenkaF commented Nov 18, 2026

Copy link
Copy Markdown
Member

Oh, should have squashed, sorry.

Comment thread python/pyarrow-stubs/_stubs_typing.pyi Resolved
Comment thread python/pyarrow-stubs/_stubs_typing.pyi Resolved
Comment thread python/pyarrow-stubs/_compute.pyi Resolved
@AlenkaF
AlenkaF force-pushed the pyarrow-stubs-pr4-compute branch from 300602b to bc6a844 Compare November 19, 2026 09:19
check(assert_type(field.equals(pc.field("a")), bool), bool)

# Calling a compute function with an expression builds an expression.
check(pc.count(field), pc.Expression)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to myself: add assert_type checks once the scalar stubs land.

Comment thread python/pyarrow-stubs/compute.pyi Outdated

# =============================== Aggregations ========================================
def all(
array: BooleanScalar | _ArrayOrChunkedArray | Expression,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be consistent with decisions made in array.pyi we should use general Scalar class in parameters, not narrow types like BooleanScalar and aliases like _NumericScalar. The narrow types stay for returns.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlenkaF
AlenkaF force-pushed the pyarrow-stubs-pr4-compute branch from 7d4856e to 0394026 Compare December 3, 2026 05:15

@rok rok left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this @AlenkaF !
Mostly 🤖 nits and some 👨 , but nothing major!

def list_functions() -> list[str]: ...
def call_function(
name: str,
args: list[Any],

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Match Function.call so callers can pass tuples or other iterables to call_function, not just lists.

Suggested change
args: list[Any],
args: Iterable[Any],


def __init__(
self,
target_type: DataType | str | None = None,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Reject str in CastOptions.__init__: _CastOptions._set_options expects a DataType at runtime (unlike safe()/unsafe(), which call ensure_type).

Suggested change
target_type: DataType | str | None = None,
target_type: DataType | None = None,

) -> None: ...

class MatchSubstringOptions(FunctionOptions):
def __init__(self, pattern: str, *, ignore_case: bool = False) -> None: ...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Allow bytes here to match the corresponding compute.pyi signatures (which accept str | bytes and convert via tobytes()). The same applies to TrimOptions, ReplaceSubstringOptions, ExtractRegexOptions, ExtractRegexSpanOptions, ReplaceSliceOptions, SplitPatternOptions, StrptimeOptions, StrftimeOptions, and AssumeTimezoneOptions.

Suggested change
def __init__(self, pattern: str, *, ignore_case: bool = False) -> None: ...
def __init__(self, pattern: str | bytes, *, ignore_case: bool = False) -> None: ...


class CumulativeOptions(FunctionOptions):
def __init__(
self, start: Scalar | None = None, *, skip_nulls: bool = False

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Allow numeric values for start to match cumulative_sum in compute.pyi (and _CumulativeOptions._set_options, which wraps non-Scalar inputs with lib.scalar()).

Suggested change
self, start: Scalar | None = None, *, skip_nulls: bool = False
self, start: float | Scalar | None = None, *, skip_nulls: bool = False

func: Callable[..., Any],
function_name: str,
function_doc: _FunctionDoc | dict[str, str],
in_types: dict[str, DataType | str],

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Because dict is invariant in its value type, dict[str, DataType | str] rejects typed dicts like in_types = {"array": pa.int64()} (dict[str, Int64Type] / dict[str, DataType]) or dict[str, str]. Introducing a bounded type variable _InType = TypeVar("_InType", bound=DataType | str) and using in_types: dict[str, _InType] across all four register_*_function signatures keeps the runtime dict requirement while accepting subtype value dicts.

self,
factory_name: str,
options: ExecNodeOptions,
inputs: list[Declaration] | None = None,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Accept any iterable of declarations to match from_sequence and _acero.pyx.

Suggested change
inputs: list[Declaration] | None = None,
inputs: Iterable[Declaration] | None = None,

Comment on lines +42 to +44
# scalars. float covers int, np.generic covers NumPy scalars
NumericLiteral: TypeAlias = float | Decimal | np.generic
TemporalLiteral: TypeAlias = dt.date | dt.time | dt.timedelta | np.generic

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Narrow np.generic to np.number[Any] / np.datetime64 | np.timedelta64 so non-numeric or non-temporal NumPy scalars (like np.str_) don't pass numeric/temporal compute stubs, matching NDArray[np.bool_] and NDArray[np.integer[Any]] above.

Suggested change
# scalars. float covers int, np.generic covers NumPy scalars
NumericLiteral: TypeAlias = float | Decimal | np.generic
TemporalLiteral: TypeAlias = dt.date | dt.time | dt.timedelta | np.generic
# scalars. float covers int.
NumericLiteral: TypeAlias = float | Decimal | np.number[Any]
TemporalLiteral: TypeAlias = (
dt.date | dt.time | dt.timedelta | np.datetime64 | np.timedelta64
)

Comment on lines +31 to +45
_StrOrExpr: TypeAlias = str | Expression
_IntoField: TypeAlias = str | int | Expression
_Target: TypeAlias = _IntoField | Sequence[_IntoField]
_Aggregate: TypeAlias = tuple[_Target, _AggregationName, _AggregateOptions | None, str]

_JoinType: TypeAlias = Literal[
"left semi",
"right semi",
"left anti",
"right anti",
"inner",
"left outer",
"right outer",
"full outer",
]

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 To simplify these aliases and avoid introducing a _stubs_typing ↔ _compute cycle (either here or in your follow-up PR), we can unify _StrOrExpr/_IntoField/_Target around _ensure_field_ref and split shared aliases by what they depend on:

  • _stubs_typing.pyi (pure Literals, no _compute imports): JoinType, AggregationName
  • _compute.pyi (owns Expression and *Options): _FieldRef = str | int | Expression (reused by _SortKey) and _AggregateOptions (from __lib/table.pyi, removing _acero.pyi's import from pyarrow.__lib.table)
  • _acero.pyi (Acero-only): _FieldRefs = _FieldRef | Sequence[_FieldRef] (used by AggregateNodeOptions, HashJoinNodeOptions, and AsofJoinNodeOptions) and _Aggregate

Comment on lines +26 to +27
# PyArrow can be built without Acero.
pytest.importorskip("pyarrow._acero")

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👨 Are we going to test without acero?

_UnsignedIntegerScalar: TypeAlias = (
UInt8Scalar | UInt16Scalar | UInt32Scalar | UInt64Scalar
)
_IntegerScalar: TypeAlias = _SignedIntegerScalar | _UnsignedIntegerScalar

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed this should be done and agreed to do it in a separate PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Python][Annotations] Compute module

4 participants