Skip to content

Non-None type inference doesn't work with walrus operator #3103

@injust

Description

@injust

Summary

ty doesn't infer that val cannot be None when a walrus operator is used:

class Foo:
    val: int | None


def test() -> None:
    foo1 = Foo()
    if foo1.val:
        reveal_type(foo1.val)  # int & ~AlwaysFalsy

    if (foo2 := Foo()).val:
        reveal_type(foo2.val)  # int | None

Playground

Version

962e0b729

Metadata

Metadata

Assignees

Labels

narrowingrelated to flow-sensitive type narrowing

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions