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

isinstance check incorrectly changes type to include list[Any] outside of the if statement #71

Open
DetachHead opened this issue Feb 3, 2024 · 0 comments · May be fixed by #447
Open
Labels
rejected upstream also a bug in pyright/pylance or feature that isn't in pyright/pylance - they refused to address it type checking / linting issues relating to existing diagnostic rules or proposals for new diagnostic rules

Comments

@DetachHead
Copy link
Owner

def foo(value: object):
    print(value)
    if isinstance(value, list):
        print(value) # error: reportUnknownArgumentType (correct)
    print(value) # error: reportUnknownArgumentType (wrong) 

playground

this is because the if statement changes the type to list[Any] | object after the if statement, which is technically correct, but redundant because object is wider than list[Any] so the type can be simplified to just object, avoiding the error. here's the same example in typescript which does simplify unions like this:

declare const foo: unknown

if (Array.isArray(foo)) {
    foo // any[]
}
foo //still unknown

declare const bar: any[] | unknown // gets simplified to just `unknown`

playground

rejected upstream issue: microsoft/pyright#7193

@DetachHead DetachHead added the rejected upstream also a bug in pyright/pylance or feature that isn't in pyright/pylance - they refused to address it label Feb 14, 2024
@DetachHead DetachHead added the type checking / linting issues relating to existing diagnostic rules or proposals for new diagnostic rules label Mar 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rejected upstream also a bug in pyright/pylance or feature that isn't in pyright/pylance - they refused to address it type checking / linting issues relating to existing diagnostic rules or proposals for new diagnostic rules
Projects
None yet
1 participant