pandas-stubs
pandas-stubs copied to clipboard
Subclassing pandas DataFrame results in mypy misinterpreting indexers return
Describe the bug
When subclassing pandas DataFrame by subclassing _constructor, type hints indicate that any return from a loc, iloc, etc... indexing is a pandas DataFrame not the subclass. To Reproduce
- Provide a minimal runnable
pandas
example that is not properly checked by the stubs.
import pandas as pd
class MyClass(pd.DataFrame):
@property
def _constructor(self) -> 'type[MyClass]':
return MyClass
df = MyClass({'a': [1, 2, 3]})
df2 = df.iloc[1:2]
reveal_type(df2)
- Indicate which type checker you are using (
mypy
orpyright
).
I used mypy
.
- Show the error message received from that type checker while checking your example.
- OS: Linux
- OS Version: Debian GNU/Linux 10
- python version: 3.10.13
- version of type checker : mypy==1.8.0
- version of installed
pandas-stubs
: 2.1.4.231227
We should probably return Self
more often: after double checking that pandas actually returns the sub-class, for example, _consructor
returns DataFrame
unless a sub-class overwrites it.