Skip to content
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

Type narrowing for subscripting is too eager #502

Open
JelleZijlstra opened this issue Mar 15, 2022 · 0 comments
Open

Type narrowing for subscripting is too eager #502

JelleZijlstra opened this issue Mar 15, 2022 · 0 comments

Comments

@JelleZijlstra
Copy link
Contributor

import pandas as pd


def x():

    res = pd.DataFrame([1, 2, 3])

    res["mean"] = [None] * 3
    reveal_type(res["mean"])

This will currently produce something like list[None], but that's incorrect because DataFrames return Series objects from __getitem__. Mypy and pyright get this right, so it may be worth looking into what heuristics they are using.

Solution I can think of: currently NameCheckVisitor.composite_from_subscript sets the narrowed type, regardless of the type of the subscripted object. If we instead did this in list.__setitem__ and dict._setitem__ (where we know it's safe), that would solve the problem.

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

1 participant