Skip to content

[red-knot] Type narrowing for assertions #17147

@sharkdp

Description

@sharkdp

It is fairly common (often in test code, but also in regular code) to use assertions to encode invariants that are hard/impossible to detect statically. So we should be able to perform type narrowing via assert statements:

def f(x: str | None):
    assert x is not None
    reveal_type(x)  # Should be 'str'

(https://playknot.ruff.rs/d44edb95-1204-484c-b6c8-2a11ba6cef51)

Metadata

Metadata

Assignees

Labels

tyMulti-file analysis & type inference

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions