Pyright: The type of the identifier of an if statement is inferred as `Never` when pyright tries to exhaust a sum type value

Created on 3 Sep 2020  路  3Comments  路  Source: microsoft/pyright

Describe the bug
Title says it all.

To Reproduce
See code below.

Expected behavior
At least the type inference should consistently Never _or_ Literal[2, 3] not both. Ideally, I would like it to be Literal[2, 3] without any error report (I used to see this behavior).

Screenshots or Code

class C:
    member: Literal[1, 2, 3] = 1

c = C()
c.member
if c.member == 1:
    ...
elif c.member == 2:  # pyright infers correctly the type of member `Literal[2, 3]`. However, it reports Cannot access member "member" for type "Never", too
    ...
d: Literal[1,2,3] = 1

if d == 1:
    ...
elif d == 2:  # here, d is inferred as `Never`
    ...

image
image

VS Code extension or command-line
coc-pyright with pyright 1.1.65

Additional context
N/A

addressed in next version bug

All 3 comments

Issue number 1000. Yay!

Thanks for the bug report. This was caused by a bug in the logic I introduced for "discriminated union type narrowing" (#944). It will be fixed in the next release.

This is addressed in Pyright 1.1.67, which I just published. It will also be included in next week's release of Pylance.

Was this page helpful?
0 / 5 - 0 ratings