pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
43.4k stars 17.83k forks source link

Refactor inherit_names to work by inheritance #32100

Open SaturnFromTitan opened 4 years ago

SaturnFromTitan commented 4 years ago

We face a lot of mypy errors due to the dynamic nature of inherit_names. See issue #31716 and the discussion that evolved there.

Summary: In the past, we added explicit type annotations to the respective classes, but this approach doesn't scale.

As mypy is a static type checker, it cannot understand the dynamic nature of inherit_names by design. The only alternative would be to refactor inherit_names to work by inheritance instead of dynamic assignments. This would make the code more understandable to humans as well.

twoertwein commented 2 years ago

Convincing pyright/mypy to infer "inherited" methods sounds unlikely https://github.com/microsoft/pyright/issues/3812.

Using mixin sub-classes to out-source common methods/properties definitely sounds like the best way to go! Is that still the ideal way to deal with that? @simonjayhawkins

I can imagine that those mixin-classes might need to be abstract as the shared methods might refer to methods/properties not existing in those otherwise empty mixin-classes (or we add many mypy ignore comments inside those methods).

There are luckily only a "few" classes making use of inherit_names: IntervalIndex, DatetimeIndexOpsMixin, CategoricalIndex, PeriodIndex, TimedeltaIndex, DatetimeIndex.

twoertwein commented 2 years ago

Using mixin sub-classes to out-source common methods/properties definitely sounds like the best way to go!

seems that isn't possible as self needs to be self._data