You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
A suggested on #908 (comment) I'm trying to use DataFrameT = TypeVar("DataFrameT", bound=DataFrame),
but with boolean indexing instead of a pipe.
To Reproduce
fromtypingimportTypeVar, reveal_typefrompandasimportDataFrame, SeriesclassSubDF(DataFrame):
# https://pandas.pydata.org/pandas-docs/stable/development/extending.html#override-constructor-properties@propertydef_constructor(self):
returnSubDF@propertydef_constructor_sliced(self):
returnSeriessub=SubDF({'a': [1, 2, 3]})
DataFrameT=TypeVar("DataFrameT", bound=DataFrame)
deffunc(df: DataFrameT) ->DataFrameT:
index=Series([True, False, True])
df_=df.loc[index]
reveal_type(df_)
returndf_# Type "DataFrame" is not assignable to return type "DataFrameT@func"reveal_type(func(sub))
pyright:
/workspaces/ng/repro.py:27:17 - information: Type of "df_" is "DataFrame"
/workspaces/ng/repro.py:29:12 - error: Type "DataFrame" is not assignable to return type "DataFrameT@func"
Type "DataFrame" is not assignable to type "DataFrameT@func" (reportReturnType)
/workspaces/ng/repro.py:32:13 - information: Type of "func(sub)" is "SubDF"
1 error, 0 warnings, 2 informations
It's not a .loc issue. You get a similar result with any DataFrame method that returns a DataFrame, e.g.:
deffunc2(df: DataFrameT) ->DataFrameT:
df_=df.query("x <= 10")
returndf_# Type "DataFrame" is not assignable to return type "DataFrameT@func"reveal_type(func2(sub))
The type revealed is still correct (SubDF) in this case.
That particular example can be fixed by changing query() to return Self instead of DataFrame. I imagine we'd have to do that with any of the methods in class DataFrame that currently return DataFrame - i.e., change them to return Self
But loc is different, because it is returning the class _LocIndexerFrame, so I think that latter class would have to become generic with Self passed in as a parameter, so it is a subclass of Generic[_T]
Describe the bug
A suggested on #908 (comment) I'm trying to use
DataFrameT = TypeVar("DataFrameT", bound=DataFrame)
,but with boolean indexing instead of a pipe.
To Reproduce
pyright:
Please complete the following information:
pandas-stubs
2.2.3.241126Additional context
Repro inspired by:
Might be same root cause as:
The text was updated successfully, but these errors were encountered: