Skip to content

GH-55767: [Python][Annotations] Narrow inherited return types with generic private base classes (_PandasConvertible, _Tabular) - #55792

Open
tadeja wants to merge 1 commit into
apache:mainfrom
tadeja:55767-stubs-generic-private
Open

tadeja wants to merge 1 commit into
apache:mainfrom
tadeja:55767-stubs-generic-private

Conversation

@tadeja

@tadeja tadeja commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Rationale for this change

Close #55767. Methods inherited from private _PandasConvertible and _Tabular returned unions on every subclass.

What changes are included in this PR?

  • _PandasConvertible is generic over its to_pandas() result
  • _Tabular is generic over its column type

Are these changes tested?

Yes, and new assert_type test cases added as well.

Are there any user-facing changes?

Yes, type checkers now infer narrower return types for these methods.

Was AI used for this PR?

PR code and description written by:

  • Human
  • AI

Reviewed before submission by:

  • Human
  • AI
  • Not reviewed

@tadeja
tadeja marked this pull request as ready for review November 27, 2026 10:17
Copilot AI balanced review requested due to automatic review settings November 27, 2026 10:17
@tadeja

tadeja commented Nov 27, 2026

Copy link
Copy Markdown
Member Author

@AlenkaF @jorisvandenbossche @rok: Ready for review! Here are two nice clean examples where making private base classes ( _PandasConvertible and _Tabular) generic narrows return types (and helps avoid duplicated overloads). So users wouldn't need cast, isinstance or # type: ignore there anymore.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@rok rok left a comment

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.

This looks good to me. I'll ask Joris to check as well.

@AlenkaF

AlenkaF commented Nov 27, 2026

Copy link
Copy Markdown
Member

The change in the stub tests is super nice =)

I would propose though to fist get the basic stubs (minimal overloads, no generics) in as we still have modules missing (compute, scalar, ...). This way we will be safe to get the basic setup in for the next release and still have time to introduce generics later? Plus, they might be useful elsewhere too.

@tadeja

tadeja commented Nov 27, 2026 •

Copy link
Copy Markdown
Member Author

Completely agree on focusing on basic stubs and avoiding all public generics!

This PR wouldn't make any public class generic (like Array, ChunkedArray, Table, RecordBatch) though and no commitment to public generics either. It's more like an internal shortcut to avoid copying to_pandas() and column()/columns/itercolumns()/__getitem__() to each subclass and prevent type-checkers' failures like for df: pd.DataFrame = table.to_pandas() ;
error: Incompatible types in assignment (expression has type "Series[Any] | DataFrame", variable has type "DataFrame") [assignment]
(and table.to_pandas().columns, table["a"].num_chunks, batch["a"].buffers()) for the next release - but that'll be only a problem for users if stubs get included in wheels with that release.

@jorisvandenbossche

Copy link
Copy Markdown
Member

Yes, agreed with @tadeja and I think we can go ahead here and merge this.

Ensuring the typing is correct regarding something returning Array vs ChunkedArray is I think something basic we should get right from the start, while something returning Int32Array vs Int64Array is much more advanced.

So if we don't go this way, I think we should right now add overloads to ensure those return types are correct. And at that point this seems a nice solution to avoid adding a bunch overloads to narrow down the return type


class _PandasConvertible(_Weakrefable):
# to_pandas() arrays return Series, tables return DataFrame
_PandasT = TypeVar("_PandasT", pd.Series[Any], pd.DataFrame)

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.

Is there a reason we are using pd.Series[Any] instead of pd.Series? (from a typing perspective, is there actually a difference?)

@jorisvandenbossche jorisvandenbossche changed the title GH-55767: [Python][Annotations] Narrow inherited return types with generic private base classes GH-55767: [Python][Annotations] Narrow inherited return types with generic private base classes (_PandasConvertible, _Tabular) Nov 27, 2026
@AlenkaF

AlenkaF commented Nov 27, 2026

Copy link
Copy Markdown
Member

Aha, ok, thanks for the background @tadeja , helps a lot! I thought we are removing generics in full to ease first batch of reviews and get smaller changes, so didn't think private vs public made any difference, sorry.

Happy you pushed this forward then 👍

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] Consider narrowing return types in pyarrow-stubs with generic private base classes

5 participants