Skip to content

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

@DetachHead

Description

@DetachHead
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    rejected upstreamalso a bug in pyright/pylance or feature that isn't in pyright/pylance - they refused to address ittype checking / lintingissues relating to existing diagnostic rules or proposals for new diagnostic rules

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions