pandas-stubs icon indicating copy to clipboard operation
pandas-stubs copied to clipboard

Return type of pipe not consistent with types at run time

Open davetapley opened this issue 2 months ago • 1 comments

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 call pipe on a class which inherits from DataFrame, then at run time I get back the subclass, but the typing implies it's just a vanilla DataFrame.

To Reproduce

Subclass DataFrame per the docs.

Create a pipe function using this signature:

def func(df: DataFrame) -> DataFrame:

Observe that at run time if I use pipe on the subtype, your get back an instance of the subtype, which is nice:

class SubDB(DataFrame):
   ... # required other stuff

sub = SubDF()
sub_f = sub.pipe(func)
type(sub_f) # is SubDF()

But if you hover sub_f in VSCode it's type is DataFrame.

Please complete the following information:

  • OS: Linux
  • OS Version 20.04.6
  • python version 3.11.7
  • version of type checker pyright version 1.1.356, commit 6652c4a8)
  • version of installed pandas-stubs 2.2.1.240316

Additional context

The offending type is the T here, it it should return Self:

https://github.com/pandas-dev/pandas-stubs/blob/072997b077ad4f766272e9bb2d03fc3771829270/pandas-stubs/core/generic.pyi#L369-L374

davetapley avatar Apr 18 '24 19:04 davetapley