You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add support for using __bool__ method literal value in union narrowing in if statements (#9297)
This adds support for union narrowing in if statements when condition value has
defined literal annotations in `__bool__` method. Value is narrowed based on the
`__bool__` method return annotation and this works even if multiple instances
defines the same literal value for `__bool__` method return type.
This PR also works well with #9288 and
makes below example to work as expected:
```python
class A:
def __bool__(self) -> Literal[True]: ...
class B:
def __bool__(self) -> Literal[False]: ...
def get_thing() -> Union[A, B]: ...
if x := get_thing():
reveal_type(x) # Revealed type is '__main__.A'
else:
reveal_type(x) # Revealed type is '__main__.B'
```
Partially fixes#9220
0 commit comments