From dccfd6e4f84dccb6fc82948eb30cceece3d87509 Mon Sep 17 00:00:00 2001 From: David Peter Date: Thu, 28 Nov 2024 19:38:56 +0100 Subject: [PATCH] New tests --- .../mdtest/statically-known-branches.md | 130 +++++++++++++++++- 1 file changed, 124 insertions(+), 6 deletions(-) diff --git a/crates/red_knot_python_semantic/resources/mdtest/statically-known-branches.md b/crates/red_knot_python_semantic/resources/mdtest/statically-known-branches.md index 119f449dd7..fd35acf6c0 100644 --- a/crates/red_knot_python_semantic/resources/mdtest/statically-known-branches.md +++ b/crates/red_knot_python_semantic/resources/mdtest/statically-known-branches.md @@ -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