1

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?

1
  • @creanion thanks for your reply. That issue doesn't really answer the question. We have two people saying they think it's impossible to provide type hints, which I can accept if it's true. But even still, is there some way to silence this error message which is coming from the language server? Commented Jun 20, 2022 at 18:14

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.