Skip to content

Commit

Permalink
Store the type for assignment expr (walrus) targets (#9479)
Browse files Browse the repository at this point in the history
Fixes #9054
  • Loading branch information
llchan authored and JukkaL committed Oct 1, 2020
1 parent 498769b commit bbfad46
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3778,6 +3778,9 @@ def accept(self,
assert typ is not None
self.chk.store_type(node, typ)

if isinstance(node, AssignmentExpr) and not has_uninhabited_component(typ):
self.chk.store_type(node.target, typ)

if (self.chk.options.disallow_any_expr and
not always_allow_any and
not self.chk.is_stub and
Expand Down
10 changes: 10 additions & 0 deletions test-data/unit/check-python38.test
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,13 @@ def check_partial_list() -> None:
z.append(3)
reveal_type(z) # N: Revealed type is 'builtins.list[builtins.int]'
[builtins fixtures/list.pyi]

[case testWalrusExpr]
def func() -> None:
foo = Foo()
if x := foo.x:
pass

class Foo:
def __init__(self) -> None:
self.x = 123

0 comments on commit bbfad46

Please sign in to comment.