I have a script like below
import pandas as pd
from typing import Any
def info(i: Any) -> None:
print(f'{type(i)=:}')
print(f'{i=:}')
print(f'{i.Index=:}')
print(f'{i.x=:}')
print(f'{i.y=:}')
if __name__ == "__main__":
df = pd.DataFrame([[1,'a'], [2, 'b']], columns=['x', 'y'])
for i in df.itertuples():
info(i)
Its output is
type(i)=<class 'pandas.core.frame.Pandas'>
i=Pandas(Index=0, x=1, y='a')
i.Index=0
i.x=1
i.y=a
type(i)=<class 'pandas.core.frame.Pandas'>
i=Pandas(Index=1, x=2, y='b')
i.Index=1
i.x=2
i.y=b
I'd like to avoid using Any
, but what's the proper way to type i
in info
function? My goal is to make my aware that i
has fields Index
, x
and y
.
If I follow pandas' way of typing (I'm still using python3.8):
def info(i: Tuple[Any, ...]) -> None:
mypy complains:
toy.py:8:11: error: "Tuple[Any, ...]" has no attribute "Index"; maybe "index"? [attr-defined]
toy.py:9:11: error: "Tuple[Any, ...]" has no attribute "x" [attr-defined]
toy.py:10:11: error: "Tuple[Any, ...]" has no attribute "y" [attr-defined]
Found 3 errors in 1 file (checked 1 source file)