New tests

This commit is contained in:
David Peter
2024-11-28 19:38:56 +01:00
parent eb4ae2b910
commit dccfd6e4f8

View File

@@ -35,8 +35,6 @@ x = 1
if True:
x = 2
else:
pass
reveal_type(x) # revealed: Literal[2]
```
@@ -69,7 +67,7 @@ reveal_type(x) # revealed: Literal[2]
## Nested
```py path=nested_if_in_if_true.py
```py path=nested_if_true_if_true.py
x = 1
if True:
@@ -83,7 +81,7 @@ else:
reveal_type(x) # revealed: Literal[2]
```
```py path=nested_if_in_if_false.py
```py path=nested_if_true_if_false.py
x = 1
if True:
@@ -97,7 +95,39 @@ else:
reveal_type(x) # revealed: Literal[3]
```
```py path=nested_if_in_else_true.py
```py path=nested_if_true_if_bool.py
def flag() -> bool: ...
x = 1
if True:
if flag():
x = 2
else:
x = 3
else:
x = 4
reveal_type(x) # revealed: Literal[2, 3]
```
```py path=nested_if_bool_if_true.py
def flag() -> bool: ...
x = 1
if flag():
if True:
x = 2
else:
x = 3
else:
x = 4
reveal_type(x) # revealed: Literal[2, 4]
```
```py path=nested_else_if_true.py
x = 1
if False:
@@ -111,7 +141,7 @@ else:
reveal_type(x) # revealed: Literal[3]
```
```py path=nested_if_in_else_false.py
```py path=nested_else_if_false.py
x = 1
if False:
@@ -124,3 +154,91 @@ else:
reveal_type(x) # revealed: Literal[4]
```
```py path=nested_else_if_bool.py
def flag() -> bool: ...
x = 1
if False:
x = 2
else:
if flag():
x = 3
else:
x = 4
reveal_type(x) # revealed: Literal[3, 4]
```
## If-expressions
### Always true
```py
x = 1 if True else 2
# TODO
reveal_type(x) # revealed: Never
```
### Always false
```py
x = 1 if False else 2
reveal_type(x) # revealed: Literal[2]
```
## Boolean expressions
### Always true
```py
(x := 1) == 1 or (x := 2)
reveal_type(x) # revealed: Literal[1]
```
### Always false
```py
(x := 1) == 0 or (x := 2)
reveal_type(x) # revealed: Literal[2]
```
## Conditionally defined functions
```py
def f() -> int: ...
def g() -> int: ...
if True:
def f() -> str: ...
else:
def g() -> str: ...
reveal_type(f()) # revealed: str
reveal_type(g()) # revealed: int
```
## Conditionally defined class attributes
```py
class C:
if True:
x: int = 1
else:
x: str = "a"
# TODO
reveal_type(C.x) # revealed: int | str
```
TODO:
- boundness
- conditional imports
- conditional class definitions