Skip to content

Type equality checking raises for subclassed objects #22914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
2 tasks done
nikaltipar opened this issue May 23, 2025 · 2 comments · May be fixed by #22915
Open
2 tasks done

Type equality checking raises for subclassed objects #22914

nikaltipar opened this issue May 23, 2025 · 2 comments · May be fixed by #22915
Labels
python Related to Python Polars

Comments

@nikaltipar
Copy link
Contributor

Checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of Polars.

Reproducible example

Example provided as-is from a comment made in #22802 by @ctdunc.

import polars as pl


class MyDf(pl.DataFrame):
    def has_column(self, col: str) -> bool:
        return col in self.columns


df = pl.DataFrame(
    {
        "foo": [1, 2, 3],
        "ham": ["a", "b", "c"],
    }
)
my_df = MyDf(
    {
        "foo": [1, 2, 3],
        "bar": [6, 7, 8],
    }
)

# works fine
df_join_mine = df.join(my_df, on="foo")

# type error
mine_join_df = my_df.join(df, on="foo")

Log output

Issue description

The type equality check is too restrictive, as sometimes subclasses are preferred in order to modify or add certain behaviours.

Expected behavior

Allow operations such as joins, that check for type equality, to go through, if the other argument is a subclass of self.

Installed versions

--------Version info---------
Polars:              1.30.0
Index type:          UInt32
Platform:            Windows-10-10.0.22631-SP0
Python:              3.11.7 (tags/v3.11.7:fa7a6f2, Dec  4 2023, 19:24:49) [MSC v.1937 64 bit (AMD64)]
LTS CPU:             False

----Optional dependencies----
Azure CLI            'az' is not recognized as an internal or external command,
operable program or batch file.
<not installed>
adbc_driver_manager  <not installed>
altair               <not installed>
azure.identity       <not installed>
boto3                <not installed>
cloudpickle          <not installed>
connectorx           <not installed>
deltalake            <not installed>
fastexcel            <not installed>
fsspec               <not installed>
gevent               <not installed>
google.auth          <not installed>
great_tables         <not installed>
matplotlib           <not installed>
numpy                <not installed>
openpyxl             <not installed>
pandas               <not installed>
polars_cloud         <not installed>
pyarrow              <not installed>
pydantic             <not installed>
pyiceberg            <not installed>
sqlalchemy           <not installed>
torch                <not installed>
xlsx2csv             <not installed>
xlsxwriter           <not installed>
@nikaltipar nikaltipar added bug Something isn't working python Related to Python Polars needs triage Awaiting prioritization by a maintainer labels May 23, 2025
@nikaltipar nikaltipar linked a pull request May 23, 2025 that will close this issue
@alexander-beedie
Copy link
Collaborator

alexander-beedie commented May 24, 2025

We don't actually support subclassing1, so this is expected/correct behaviour.

Some methods will return the subclassed type (when inheriting from DataFrame/etc), but many will not.
For example, with your MyDf class/example above:

type(my_df)
# __main__.MyDf

type(my_df.head(1))
# __main__.MyDf

type(my_df.select("bar"))
# polars.dataframe.frame.DataFrame

type(my_df.filter(pl.col("bar") >= 7))
# polars.dataframe.frame.DataFrame

Footnotes

  1. https://github.com/pola-rs/polars/pull/7426#issuecomment-1475834972

@alexander-beedie alexander-beedie removed bug Something isn't working needs triage Awaiting prioritization by a maintainer labels May 24, 2025
@nikaltipar
Copy link
Contributor Author

We don't actually support subclassing1, so this is expected/correct behaviour.

Some methods will return the subclassed type (when inheriting from DataFrame/etc), but many will not. For example, with your MyDf class/example above:

type(my_df)

main.MyDf

type(my_df.head(1))

main.MyDf

type(my_df.select("bar"))

polars.dataframe.frame.DataFrame

type(my_df.filter(pl.col("bar") >= 7))

polars.dataframe.frame.DataFrame

Footnotes

  1. feat(python): Easier subclassing #7426 (comment)

Added a comment on the PR, let me know if instead you'd like to continue the discussion here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
python Related to Python Polars
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants