You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "timsaucer (via GitHub)" <gi...@apache.org> on 2026/10/07 15:29:00 UTC
[PR] ci: add ty type checker and fix existing type errors [datafusion-python]
timsaucer opened a new pull request, #1786:
URL: https://github.com/apache/datafusion-python/pull/1786
# Which issue does this PR close?
No issue filed yet; happy to open one if preferred.
# Rationale for this change
The package ships `py.typed`, so downstream type checkers consume our annotations, but nothing in CI checks that those annotations are correct. Ruff's `ANN` rules only require annotations to exist. Running [ty](https://docs.astral.sh/ty/) against `python/datafusion` reported 82 diagnostics on `main`; after configuring imports, about 50 were real annotation drift, and one was a runtime bug (`@udtf()` without a name passed `None` into Rust and failed with `TypeError: 'None' is not an instance of 'str'`).
ty is from Astral, like the ruff and uv tooling already used here, and is fast enough to run as a pre-commit hook.
# What changes are included in this PR?
Tooling:
- Add `ty==0.0.84` to the `dev` dependency group. It is pinned exactly because ty is pre-1.0 and new releases can add diagnostics.
- Add `[tool.ty]` config limiting the check to `python/datafusion`. `datafusion._internal` (compiled, no stubs) and the optional `pandas`/`polars` imports are allowed to be unresolved, so the check does not need a built wheel.
- Run `ty check` in the `lint-python` job and as a local pre-commit hook.
Fixes for the reported diagnostics:
- `TableFunction`: the internal decorator helper now requires `name`, matching the public `udtf` overloads, so `@udtf()` fails with a normal missing-argument error instead of inside Rust.
- `AggregateUDF.__init__`: the implementation now has the `None` defaults its FFI overload already advertised. `ScalarUDF` and `WindowUDF` get the same FFI overload, following the `AggregateUDF` pattern.
- `expr_list_to_raw_expr_list` / `sort_list_to_raw_sort_list`: add `None`→`None` / sequence→`list` overloads so callers that unpack the result (`select`, `sort`, `window`, ...) type check, while optional `order_by` callers still pass `None` through.
- Stop rebinding typed `*args` and parameters to values of other types (`concat`, `coalesce`, `make_array`, `repartition_by_hash`, `unnest_columns`, ...).
- Import `warnings.deprecated` behind `sys.version_info >= (3, 13)` instead of `try`/`except`, and drop the unreachable `importlib_metadata` fallback (`requires-python >= 3.10`).
- Smaller mismatches: `LogicalPlan.__eq__` takes `object`, `spark._coerce_i32` no longer claims to accept `None`, CSV `file_compression_type` is normalized before reaching `CsvReadOptions`, and one documented `cast` in `join` for the legacy tuple form of `on`.
- Three inline ignores for intentional behavior: `Expr.__eq__`/`__ne__` return `Expr`, and the deprecated `CreateExternalTable.location` alias assignment.
# Are there any user-facing changes?
No API changes. `@udtf()` with no name still fails, but now with a standard Python `TypeError` for the missing argument. `AggregateUDF(name, exportable)` now works as its type signature already promised. Contributors need `ty` in their environment (`uv sync --dev`) for the new pre-commit hook.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] ci: add ty type checker and fix existing type errors [datafusion-python]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #1786:
URL: https://github.com/apache/datafusion-python/pull/1786#discussion_r4219092013
##########
python/datafusion/user_defined.py:
##########
@@ -1333,7 +1380,7 @@ def _create_table_udf(
@staticmethod
def _create_table_udf_decorator(
- name: str | None = None,
+ name: str,
*,
with_session: bool = False,
) -> Callable[[Callable[..., Any]], TableFunction]:
Review Comment:
Making `name` required here is an API-breaking change for decorator usage patterns that previously allowed omitting the name (and defaulting it). This is also inconsistent with the other UDF/UDWF decorator helpers in this module that accept `name: str | None = None`. Consider restoring `name: str | None = None` (and deriving a default name when `None`), or introducing overloads to support both explicit-name and inferred-name forms without breaking existing callers.
##########
python/datafusion/context.py:
##########
@@ -2196,7 +2196,7 @@ def read_csv(
schema_infer_max_records=schema_infer_max_records,
file_extension=file_extension,
table_partition_cols=table_partition_cols,
- file_compression_type=file_compression_type,
+ file_compression_type=file_compression_type or "",
Review Comment:
Replacing `None` with an empty string changes behavior and can introduce hard-to-debug failures if the Rust/internal layer treats `\"\"` as an invalid compression type rather than 'unset/default'. Prefer passing `None` through if the internal API supports it; otherwise, consider mapping `None` to the explicit default value expected by the internal API (or documenting/centralizing the sentinel handling) and rejecting `\"\"` when the user explicitly supplies it.
##########
.github/workflows/build.yml:
##########
@@ -81,6 +81,10 @@ jobs:
uv run --no-project ruff check --output-format=github python/
uv run --no-project ruff format --check python/
+ - name: Run ty
+ run: |
+ uv run --no-project ty check --output-format github
Review Comment:
This invocation likely bypasses the pinned `ty==0.0.84` declared in `pyproject.toml` (because `--no-project` avoids using the project’s dependency set). That undermines the stated goal of pinning `ty` to prevent new diagnostics from breaking CI unexpectedly. Consider running `ty` from the project environment (e.g., syncing/installing the `dev` group in CI and using `uv run ty check ...` without `--no-project`), or explicitly running a pinned tool version via uv so CI/pre-commit are deterministic.
##########
python/datafusion/functions/spark.py:
##########
@@ -56,14 +56,14 @@ def _filter_raw(filter: Expr | None) -> Any:
return filter.expr if filter is not None else None
-def _coerce_i32(value: Expr | int | None) -> Expr | None:
- """Coerce a native ``int`` to an int32 literal, passing ``Expr``/``None`` through.
+def _coerce_i32(value: Expr | int) -> Expr:
+ """Coerce a native ``int`` to an int32 literal, passing ``Expr`` through.
Several Spark datetime and interval builders require 32-bit integer
inputs, so a bare ``int`` must become an int32 literal rather than the
int64 default that :meth:`Expr.literal` would produce.
"""
Review Comment:
This change removes support for `None` (previously accepted and passed through). If any Spark helper functions call `_coerce_i32(...)` with an optional argument (common for builder-style APIs), this will now raise at runtime. Either (a) keep the prior contract (`Expr | int | None -> Expr | None`) and handle `None` explicitly, or (b) ensure all call sites validate non-None before calling and update any public function signatures/docs accordingly.
##########
python/datafusion/functions/spark.py:
##########
@@ -56,14 +56,14 @@ def _filter_raw(filter: Expr | None) -> Any:
return filter.expr if filter is not None else None
-def _coerce_i32(value: Expr | int | None) -> Expr | None:
- """Coerce a native ``int`` to an int32 literal, passing ``Expr``/``None`` through.
+def _coerce_i32(value: Expr | int) -> Expr:
+ """Coerce a native ``int`` to an int32 literal, passing ``Expr`` through.
Several Spark datetime and interval builders require 32-bit integer
inputs, so a bare ``int`` must become an int32 literal rather than the
int64 default that :meth:`Expr.literal` would produce.
"""
Review Comment:
This change removes support for `None` (previously accepted and passed through). If any Spark helper functions call `_coerce_i32(...)` with an optional argument (common for builder-style APIs), this will now raise at runtime. Either (a) keep the prior contract (`Expr | int | None -> Expr | None`) and handle `None` explicitly, or (b) ensure all call sites validate non-None before calling and update any public function signatures/docs accordingly.
##########
python/datafusion/context.py:
##########
@@ -1292,7 +1292,7 @@ def register_csv(
delimiter=delimiter,
schema_infer_max_records=schema_infer_max_records,
file_extension=file_extension,
- file_compression_type=file_compression_type,
+ file_compression_type=file_compression_type or "",
Review Comment:
Replacing `None` with an empty string changes behavior and can introduce hard-to-debug failures if the Rust/internal layer treats `\"\"` as an invalid compression type rather than 'unset/default'. Prefer passing `None` through if the internal API supports it; otherwise, consider mapping `None` to the explicit default value expected by the internal API (or documenting/centralizing the sentinel handling) and rejecting `\"\"` when the user explicitly supplies it.
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] ci: add ty type checker and fix existing type errors [datafusion-python]
Posted by "timsaucer (via GitHub)" <gi...@apache.org>.
timsaucer merged PR #1786:
URL: https://github.com/apache/datafusion-python/pull/1786
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] ci: add ty type checker and fix existing type errors [datafusion-python]
Posted by "timsaucer (via GitHub)" <gi...@apache.org>.
timsaucer commented on code in PR #1786:
URL: https://github.com/apache/datafusion-python/pull/1786#discussion_r4208914858
##########
python/datafusion/dataframe.py:
##########
@@ -1158,8 +1160,8 @@ def join(
if left_on is not None or right_on is not None:
error_msg = "`left_on` or `right_on` should not provided with `on`"
raise ValueError(error_msg)
- left_on = on
- right_on = on
+ # The legacy ``(left, right)`` tuple form was consumed above.
+ left_on = right_on = cast("str | Sequence[str]", on)
Review Comment:
This is a no-op at runtime but will make it so type checkers recognize that we've already verified the type.
##########
python/datafusion/expr.py:
##########
@@ -410,8 +411,18 @@ def _to_raw_expr(value: Expr | str) -> expr_internal.Expr:
raise TypeError(error)
+@overload
+def expr_list_to_raw_expr_list(expr_list: None) -> None: ...
+
+
+@overload
+def expr_list_to_raw_expr_list(
+ expr_list: Sequence[Expr | str] | Expr | str,
+) -> list[expr_internal.Expr]: ...
Review Comment:
These overloads make it so type checkers understand that if a `None` if fed in you get a `None` out, otherwise you get a valid list of `Expr`
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] ci: add ty type checker and fix existing type errors [datafusion-python]
Posted by "timsaucer (via GitHub)" <gi...@apache.org>.
timsaucer commented on code in PR #1786:
URL: https://github.com/apache/datafusion-python/pull/1786#discussion_r4221312353
##########
python/datafusion/user_defined.py:
##########
@@ -1333,7 +1380,7 @@ def _create_table_udf(
@staticmethod
def _create_table_udf_decorator(
- name: str | None = None,
+ name: str,
*,
with_session: bool = False,
) -> Callable[[Callable[..., Any]], TableFunction]:
Review Comment:
The public `udtf` overloads already required `name: str` on main; only this private helper had a `None` default, which forwarded `None` into `TableFunction` and failed at runtime. Requiring it here matches the public contract and surfaces a clear `TypeError`.
##########
.github/workflows/build.yml:
##########
@@ -81,6 +81,10 @@ jobs:
uv run --no-project ruff check --output-format=github python/
uv run --no-project ruff format --check python/
+ - name: Run ty
+ run: |
+ uv run --no-project ty check --output-format github
Review Comment:
`uv sync --dev` installs the pinned `ty==0.0.84` into `.venv`, and `uv run --no-project` uses that environment, the same as the ruff and codespell steps. The CI log shows `+ ty==0.0.84`.
##########
python/datafusion/context.py:
##########
@@ -1292,7 +1292,7 @@ def register_csv(
delimiter=delimiter,
schema_infer_max_records=schema_infer_max_records,
file_extension=file_extension,
- file_compression_type=file_compression_type,
+ file_compression_type=file_compression_type or "",
Review Comment:
No behavior change: `CsvReadOptions.file_compression_type` defaults to `""`, `to_inner()` already applies `or ""`, and the Rust side does `unwrap_or_default()`.
##########
python/datafusion/context.py:
##########
@@ -2196,7 +2196,7 @@ def read_csv(
schema_infer_max_records=schema_infer_max_records,
file_extension=file_extension,
table_partition_cols=table_partition_cols,
- file_compression_type=file_compression_type,
+ file_compression_type=file_compression_type or "",
Review Comment:
No behavior change: `CsvReadOptions.file_compression_type` defaults to `""`, `to_inner()` already applies `or ""`, and the Rust side does `unwrap_or_default()`.
##########
python/datafusion/functions/spark.py:
##########
@@ -56,14 +56,14 @@ def _filter_raw(filter: Expr | None) -> Any:
return filter.expr if filter is not None else None
-def _coerce_i32(value: Expr | int | None) -> Expr | None:
- """Coerce a native ``int`` to an int32 literal, passing ``Expr``/``None`` through.
+def _coerce_i32(value: Expr | int) -> Expr:
+ """Coerce a native ``int`` to an int32 literal, passing ``Expr`` through.
Several Spark datetime and interval builders require 32-bit integer
inputs, so a bare ``int`` must become an int32 literal rather than the
int64 default that :meth:`Expr.literal` would produce.
"""
Review Comment:
All call sites handle `None` before calling (`... if x is not None else _ZERO_I32`) or take non-optional parameters. On main, a `None` here would have failed on `.expr`.
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] ci: add ty type checker and fix existing type errors [datafusion-python]
Posted by "timsaucer (via GitHub)" <gi...@apache.org>.
timsaucer commented on code in PR #1786:
URL: https://github.com/apache/datafusion-python/pull/1786#discussion_r4221314035
##########
python/datafusion/functions/spark.py:
##########
@@ -56,14 +56,14 @@ def _filter_raw(filter: Expr | None) -> Any:
return filter.expr if filter is not None else None
-def _coerce_i32(value: Expr | int | None) -> Expr | None:
- """Coerce a native ``int`` to an int32 literal, passing ``Expr``/``None`` through.
+def _coerce_i32(value: Expr | int) -> Expr:
+ """Coerce a native ``int`` to an int32 literal, passing ``Expr`` through.
Several Spark datetime and interval builders require 32-bit integer
inputs, so a bare ``int`` must become an int32 literal rather than the
int64 default that :meth:`Expr.literal` would produce.
"""
Review Comment:
All call sites handle `None` before calling (`... if x is not None else _ZERO_I32`) or take non-optional parameters. On main, a `None` here would have failed on `.expr`.
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org