-
-
Notifications
You must be signed in to change notification settings - Fork 112
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
Return type of pipe not consistent with types at run time #908
Comments
Thanks for the report. PR with tests welcome. |
The type annotations of pipe are correct -- it returns the same type returned by the input function. In your case, the function is declared to return a from typing import TypeVar
from pandas import DataFrame
DataFrameT = TypeVar("DataFrameT", bound=DataFrame)
def func(df: DataFrameT) -> DataFrameT: return df
class SubDF(DataFrame):
... # required other stuff
sub = SubDF()
sub_f = sub.pipe(func)
reveal_type(sub_f) # Type of "sub_f" is "SubDF" (Pylance) |
I'd still like to see if returning |
I don’t know what this means, pipe already uses |
See the suggestion above. Right now, |
No,. that wouldn't work. pipe returns exactly what the function passed to it returns, not a copy of "self". If you run this example: import pandas as pd
df = pd.DataFrame(data={"A": [1, 2], "B": [3, 4]})
def f(df: pd.DataFrame) -> int:
return df.size
def g(df: pd.DataFrame) -> pd.Series:
return df["A"]
res_f = df.pipe(f)
print(res_f, type(res_f))
res_g = df.pipe(g)
print(res_g, type(res_g)) You get:
|
Thanks for your analysis. Your solution at #908 (comment) is how the OP should handle this. |
Describe the bug
Using
pipe
returns a instance of the type pipe is called on, but the type stubs imply it's the type of the function being applied by pipe.i.e. if my function returns a
DataFrame
, but I callpipe
on a class which inherits fromDataFrame
, then at run time I get back the subclass, but the typing implies it's just a vanillaDataFrame
.To Reproduce
Subclass
DataFrame
per the docs.Create a
pipe
function using this signature:Observe that at run time if I use
pipe
on the subtype, your get back an instance of the subtype, which is nice:But if you hover
sub_f
in VSCode it's type isDataFrame
.Please complete the following information:
pandas-stubs
2.2.1.240316Additional context
The offending type is the
T
here, it it should returnSelf
:pandas-stubs/pandas-stubs/core/generic.pyi
Lines 369 to 374 in 072997b
The text was updated successfully, but these errors were encountered: