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
playground
this is because the if statement changes the type to
list[Any] | objectafter the if statement, which is technically correct, but redundant becauseobjectis wider thanlist[Any]so the type can be simplified to justobject, avoiding the error. here's the same example in typescript which does simplify unions like this:playground
rejected upstream issue: microsoft/pyright#7193