无法使用Pandas DataFrame Itertuples(Neovim 0.7的Pyright-langserver)访问元组类型[任何,...]

发布于 2025-02-09 00:36:45 字数 577 浏览 2 评论 0原文

我正在使用Neovim V0.7.0的Pyright-Langserver。它的功能很好,除了我不知道如何在以下情况下正确注释类型。

import pandas as pd

df = pd.DataFrame({'num_legs': [4, 2], 'num_wings': [0, 2]},index=['dog', 'hawk'])

for row in df.itertuples():
    print(row.num_legs) # ■ Cannot access member "num_legs" for type "tuple[Any, ...]"    Member "num_legs" is unknown

如您所见,我发表了一个注释,显示Pyright报告的错误:无法访问类型“ tuple [tuple [any,...]“    member“ num_legs”的“ num_legs”是未知

该代码是有效的,因为它正如我所期望的那样打印4,然后是2。我如何满足类型检查?

I'm using the pyright-langserver with Neovim v0.7.0. It functions well, except I don't know how to correctly annotate the types in the following situation.

import pandas as pd

df = pd.DataFrame({'num_legs': [4, 2], 'num_wings': [0, 2]},index=['dog', 'hawk'])

for row in df.itertuples():
    print(row.num_legs) # ■ Cannot access member "num_legs" for type "tuple[Any, ...]"    Member "num_legs" is unknown

As you can see, I put a comment showing the error that pyright reports: Cannot access member "num_legs" for type "tuple[Any, ...]"   Member "num_legs" is unknown

The code is valid in that it prints 4, then 2, as I would expect. How do I satisfy the type-checking?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈下烟灰 2025-02-16 00:36:45

我还没有找到解决类型问题的解决方案(PANDAS仅作为通用元组),但是至少有两种抑制错误报告的方法。

  1. 告诉Pyright to 忽略错误
for row in df.itertuples():
    print(row.num_legs)  # # pyright: ignore [reportGeneralTypeIssues]
  1. cast()
from typing import cast

for row in df.itertuples():
    row: Any = cast(Any, row)
    print(row.num_legs)

I have not found a solution to fix the type issue (Pandas just types as a generic tuple), but there are at least two ways to suppress the error reports.

  1. Tell Pyright to ignore the error:
for row in df.itertuples():
    print(row.num_legs)  # # pyright: ignore [reportGeneralTypeIssues]
  1. cast() the type to Any to turn off type interference for the object
from typing import cast

for row in df.itertuples():
    row: Any = cast(Any, row)
    print(row.num_legs)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文