geopandas: index type should be Any
My repo currently satisfies pandas-stubs type hints. However, when I add types-geopandas, I see hundreds of new issues:
error: "Index[Any]" has no attribute "overlaps" [attr-defined]
error: "Index[Any]" has no attribute "left" [attr-defined]
error: "Index[Any]" has no attribute "right" [attr-defined]
I'm using pd.IntervalIndex, which has these attributes. However, pandas currently has no way of typing DataFrame indices (https://github.com/pandas-dev/pandas/issues/36708), so there is no way of knowing which kind of index is returned.
I think it would be more correct to use typing.Any here since it is unknown and dynamic.
I have no experience with pandas, so I can't really comment on this issue, but a PR outlining the change you desire would be very welcome.
My repo currently satisfies pandas-stubs type hints. However, when I add types-geopandas, I see hundreds of new issues:
Could you give an example on code that fails only with types-geopandas installed? types-geopandas inherits the definition of index from pandas-stubs.
I think it would be more correct to use
typing.Anyhere since it is unknown and dynamic.
I disagree. IntervalIndex is a subtype of Index and with DataFrame not being generic, Index is the correct type of index.
Also geopandas doesn't override the index neither in the stubs nor at runtime. If you want a solution please discuss it with the maintainers of the pandas-stubs repository.
Disclaimer: I added the geopandas stubs to typeshed
Oops, you are completely correct. The following code is invalid (according to mypy) for both pd.DataFrame and gpd.GeoDataFrame:
import pandas as pd
data = {'col1': [1, 2], 'col2': [3, 4]}
index = pd.interval_range(start=0, end=2)
df = pd.DataFrame(data, index)
print(df.index.left)
print(df.index.right)
I would argue that statically typed code that is correct should not fail type checking, but let me first raise this issue with the pandas-stubs community and we can come back to geopandas later.