Skip to content

Commit 0b098ac

Browse files
authored
test(narrowing): add regression test for walrus narrowing in and operand
Add regression tests for walrus operator behavior in nested expressions.
1 parent 82d2147 commit 0b098ac

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

test-data/unit/check-inference.test

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4264,6 +4264,28 @@ def check_or_nested(maybe: bool) -> None:
42644264
reveal_type(bar) # N: Revealed type is "builtins.list[builtins.int]"
42654265
reveal_type(baz) # N: Revealed type is "builtins.list[builtins.int]"
42664266

4267+
[case testInferWalrusAssignmentNestedInAndExpr]
4268+
# Walrus narrowing should propagate when the assignment is nested inside
4269+
# an arbitrary expression on the right side of an "and" condition.
4270+
# Regression test for https://github.com/python/mypy/issues/19430
4271+
class Node:
4272+
def __init__(self, value: bool) -> None:
4273+
self.value = value
4274+
4275+
def check_walrus_in_unary(cond: bool) -> None:
4276+
woo = None
4277+
if cond and not (woo := Node(True)).value:
4278+
reveal_type(woo) # N: Revealed type is "__main__.Node"
4279+
else:
4280+
reveal_type(woo) # N: Revealed type is "__main__.Node | None"
4281+
4282+
def check_walrus_in_nested_call(cond: bool) -> None:
4283+
woo = None
4284+
if cond and (woo := Node(True)).value:
4285+
reveal_type(woo) # N: Revealed type is "__main__.Node"
4286+
else:
4287+
reveal_type(woo) # N: Revealed type is "__main__.Node | None"
4288+
42674289
[case testInferOptionalAgainstAny]
42684290
from typing import Any, Optional, TypeVar
42694291

0 commit comments

Comments
 (0)