Skip to content

TYPING: df.columns.levels raises mypy error on MultiIndex for columns #36307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hweecat opened this issue Sep 12, 2020 · 2 comments
Closed

TYPING: df.columns.levels raises mypy error on MultiIndex for columns #36307

hweecat opened this issue Sep 12, 2020 · 2 comments

Comments

@hweecat
Copy link
Contributor

hweecat commented Sep 12, 2020

Observed in PR #36305

When adding check on inferred type for each level in MultiIndex for columns, error message pops up in CI (Typing validation with mypy):

pandas/io/parquet.py:59: error: "Index" has no attribute "levels"; maybe "nlevels"? [attr-defined]

I understand that single-level Indexes do not have the "levels" attribute while MultiIndexes have the "levels" attribute. It seems that mypy static type checking may have inferred "df.columns" as Index type by default, hence flagging the error when using "df.columns.levels" in the code change.

Would it make sense to add a "levels" attribute to "Index" for consistency with MultiIndex, or are there any possible workarounds for this mypy error?

@dsaxton
Copy link
Member

dsaxton commented Sep 13, 2020

Rather than adding an attribute to silence this error I think you could use a more explicit check to convince mypy it's a MultiIndex (commented in the PR as well)

@dsaxton
Copy link
Member

dsaxton commented Sep 13, 2020

Closing as resolved

@dsaxton dsaxton closed this as completed Sep 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants