@@ -3979,3 +3979,95 @@ def check(mapping: Mapping[str, _T]) -> None:
3979
3979
reveal_type(ok1) # N: Revealed type is "Union[_T`-1, builtins.str]"
3980
3980
ok2: Union[_T, str] = mapping.get("", "")
3981
3981
[builtins fixtures/tuple.pyi]
3982
+
3983
+ [case testInferWalrusAssignmentAttrInCondition]
3984
+ class Foo:
3985
+ def __init__(self, value: bool) -> None:
3986
+ self.value = value
3987
+
3988
+ def check_and(maybe: bool) -> None:
3989
+ foo = None
3990
+ if maybe and (foo := Foo(True)).value:
3991
+ reveal_type(foo) # N: Revealed type is "__main__.Foo"
3992
+ else:
3993
+ reveal_type(foo) # N: Revealed type is "Union[__main__.Foo, None]"
3994
+
3995
+ def check_and_nested(maybe: bool) -> None:
3996
+ foo = None
3997
+ bar = None
3998
+ baz = None
3999
+ if maybe and (foo := (bar := (baz := Foo(True)))).value:
4000
+ reveal_type(foo) # N: Revealed type is "__main__.Foo"
4001
+ reveal_type(bar) # N: Revealed type is "__main__.Foo"
4002
+ reveal_type(baz) # N: Revealed type is "__main__.Foo"
4003
+ else:
4004
+ reveal_type(foo) # N: Revealed type is "Union[__main__.Foo, None]"
4005
+ reveal_type(bar) # N: Revealed type is "Union[__main__.Foo, None]"
4006
+ reveal_type(baz) # N: Revealed type is "Union[__main__.Foo, None]"
4007
+
4008
+ def check_or(maybe: bool) -> None:
4009
+ foo = None
4010
+ if maybe or (foo := Foo(True)).value:
4011
+ reveal_type(foo) # N: Revealed type is "Union[__main__.Foo, None]"
4012
+ else:
4013
+ reveal_type(foo) # N: Revealed type is "__main__.Foo"
4014
+
4015
+ def check_or_nested(maybe: bool) -> None:
4016
+ foo = None
4017
+ bar = None
4018
+ baz = None
4019
+ if maybe and (foo := (bar := (baz := Foo(True)))).value:
4020
+ reveal_type(foo) # N: Revealed type is "__main__.Foo"
4021
+ reveal_type(bar) # N: Revealed type is "__main__.Foo"
4022
+ reveal_type(baz) # N: Revealed type is "__main__.Foo"
4023
+ else:
4024
+ reveal_type(foo) # N: Revealed type is "Union[__main__.Foo, None]"
4025
+ reveal_type(bar) # N: Revealed type is "Union[__main__.Foo, None]"
4026
+ reveal_type(baz) # N: Revealed type is "Union[__main__.Foo, None]"
4027
+
4028
+ [case testInferWalrusAssignmentIndexInCondition]
4029
+ def check_and(maybe: bool) -> None:
4030
+ foo = None
4031
+ bar = None
4032
+ if maybe and (foo := [1])[(bar := 0)]:
4033
+ reveal_type(foo) # N: Revealed type is "builtins.list[builtins.int]"
4034
+ reveal_type(bar) # N: Revealed type is "builtins.int"
4035
+ else:
4036
+ reveal_type(foo) # N: Revealed type is "Union[builtins.list[builtins.int], None]"
4037
+ reveal_type(bar) # N: Revealed type is "Union[builtins.int, None]"
4038
+
4039
+ def check_and_nested(maybe: bool) -> None:
4040
+ foo = None
4041
+ bar = None
4042
+ baz = None
4043
+ if maybe and (foo := (bar := (baz := [1])))[0]:
4044
+ reveal_type(foo) # N: Revealed type is "builtins.list[builtins.int]"
4045
+ reveal_type(bar) # N: Revealed type is "builtins.list[builtins.int]"
4046
+ reveal_type(baz) # N: Revealed type is "builtins.list[builtins.int]"
4047
+ else:
4048
+ reveal_type(foo) # N: Revealed type is "Union[builtins.list[builtins.int], None]"
4049
+ reveal_type(bar) # N: Revealed type is "Union[builtins.list[builtins.int], None]"
4050
+ reveal_type(baz) # N: Revealed type is "Union[builtins.list[builtins.int], None]"
4051
+
4052
+ def check_or(maybe: bool) -> None:
4053
+ foo = None
4054
+ bar = None
4055
+ if maybe or (foo := [1])[(bar := 0)]:
4056
+ reveal_type(foo) # N: Revealed type is "Union[builtins.list[builtins.int], None]"
4057
+ reveal_type(bar) # N: Revealed type is "Union[builtins.int, None]"
4058
+ else:
4059
+ reveal_type(foo) # N: Revealed type is "builtins.list[builtins.int]"
4060
+ reveal_type(bar) # N: Revealed type is "builtins.int"
4061
+
4062
+ def check_or_nested(maybe: bool) -> None:
4063
+ foo = None
4064
+ bar = None
4065
+ baz = None
4066
+ if maybe or (foo := (bar := (baz := [1])))[0]:
4067
+ reveal_type(foo) # N: Revealed type is "Union[builtins.list[builtins.int], None]"
4068
+ reveal_type(bar) # N: Revealed type is "Union[builtins.list[builtins.int], None]"
4069
+ reveal_type(baz) # N: Revealed type is "Union[builtins.list[builtins.int], None]"
4070
+ else:
4071
+ reveal_type(foo) # N: Revealed type is "builtins.list[builtins.int]"
4072
+ reveal_type(bar) # N: Revealed type is "builtins.list[builtins.int]"
4073
+ reveal_type(baz) # N: Revealed type is "builtins.list[builtins.int]"
0 commit comments