在 pandas.DataFrame.iterrows() 中输入 annotation/hint 作为索引

Type annotation/hint for index in pandas.DataFrame.iterrows()

我正在尝试在 Python 脚本中为 运行ning mypy checks. I have a pandas.DataFrame object, which I iterate 添加类型 annotations/hints,如下所示:

someTable: pandas.DataFrame = pandas.DataFrame()

# ...
# adding some data to someTable
# ...

for index, row in someTable.iterrows():
    #reveal_type(index)
    print(type(index))
    print(index + 1)

如果我 运行 这个脚本,这就是我得到的:

$ python ./some.py
<class 'int'>
2
<class 'int'>
3

如果我用mypy检查它,然后它报告错误:

$ mypy ./some.py
some.py:32: note: Revealed type is "Union[typing.Hashable, None]"
some.py:34: error: Unsupported operand types for + ("Hashable" and "int")
some.py:34: error: Unsupported operand types for + ("None" and "int")
some.py:34: note: Left operand is of type "Optional[Hashable]"
Found 2 errors in 1 file (checked 1 source file)

据我了解,mypyindex 视为 Union[typing.Hashable, None],而不是 int,因此 index + 1 看起来像是一个错误.那么我应该如何以及在哪里 annotate/hint 它来满足 mypy?

我试过这个:

index: int
for index, row in someTable.iterrows():
    # ...

但这会导致:

$ mypy ./some.py
some.py:32: error: Incompatible types in assignment (expression has type "Optional[Hashable]", variable has type "int")
Found 1 error in 1 file (checked 1 source file)

您可以将 index 提示为 Optional[int],但 x + 1 将不会键入检查。

我不确定 Union[typing.Hashable, None] 来自哪里; iterrows 本身 return 是一个 Iterable[tuple[Hashable, Series]]。但似乎您可以安全地断言,如果 index 被赋予了一个值,那么它就不会是 None.

index: Optional[int]
for index, row in someTable.iterrows():
    index = typing.cast(int, index)
    print(index + 1)

Union 是否应该反映可迭代引发 StopIteration 的可能性?这似乎不对,因为引发异常的函数不会 return None;根本return 。)