microsoft / microsoft/pyright

Surprising type analysis failure

Open
#11,247 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
15.6k
Forks
1.8k
Avg merge
12h 13m
Merged PRs (30d)
52

Description

```python
class Node:
pass

class Wrapper:
def __init__(self, child: Node):
self.child = child

class SpecialNode(Node):
pass

def f(plan: Wrapper | None):
if not (plan and isinstance(plan.child, SpecialNode)):
return
ts1: SpecialNode = plan.child # Ok
ts2: SpecialNode = plan.child if plan else None # "None" is not assignable to declared type "SpecialNode"
```

Since `plan.child` is accessed successfully, I assume pyright knows that plan isn't None and that the extra condition `if plan` is a no-op. Hopefully this knowledge can be incorporated into the analysis.

Contributor guide

Open the contributing guide

Research direction

Start by running the provided Python snippet through pyright and confirm the diagnostic on the conditional expression. Trace the type analysis for the `plan` guard and conditional expression; done means the `ts2` assignment is accepted when the earlier access establishes that `plan` is not None.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.