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
Fix false negatives in walrus vs inference fallback logic (#20622)
Fixes#20606
This fixes couple edge cases where walrus interferes with type inference
fallback logic. This is not a complete fix, but I think it is OK for now
as these are probably rare situations. Most changes in `binder.py` are a
pure performance optimization to compensate the fact that
`frame_context()` will be more hot now. I also leave a TODO explaining a
more proper fix for the assignment case (and other possible cases),
return case should be already good (it is simpler since we don't need to
apply any of the narrowing).
Copy file name to clipboardExpand all lines: test-data/unit/check-python38.test
+21Lines changed: 21 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -810,3 +810,24 @@ y: List[int]
810
810
if (y := []):
811
811
reveal_type(y) # N: Revealed type is "builtins.list[builtins.int]"
812
812
[builtins fixtures/list.pyi]
813
+
814
+
[case testAssignToOptionalTupleWalrus]
815
+
from typing import Optional
816
+
817
+
def condition() -> bool: return False
818
+
819
+
i: Optional[int] = 0 if condition() else None
820
+
x: Optional[tuple[int, int]] = (i, (i := 1)) # E: Incompatible types in assignment (expression has type "tuple[int | None, int]", variable has type "tuple[int, int] | None")
821
+
[builtins fixtures/tuple.pyi]
822
+
823
+
[case testReturnTupleOptionalWalrus]
824
+
from typing import Optional
825
+
826
+
def condition() -> bool: return False
827
+
828
+
def fn() -> tuple[int, int]:
829
+
i: Optional[int] = 0 if condition() else None
830
+
return (i, (i := i + 1)) # E: Incompatible return value type (got "tuple[int | None, int]", expected "tuple[int, int]") \
831
+
# E: Unsupported operand types for + ("None" and "int") \
0 commit comments