Skip to content

[red-knot] add narrowing support in match class patterns #14740

@carljm

Description

@carljm

The goal of this issue is to make this mdtest pass:

def get_bool() -> bool: ...
def get_object() -> object: ...
class A: ...
class B: ...

x = get_object()

reveal_type(x)  # revealed: object

match x:
    case A():
        reveal_type(x)  # revealed: A
    case B():
        reveal_type(x)  # revealed: B

reveal_type(x)  # revealed: object

This should just require filling in the TODO for MatchClass at

We don't need to worry about arguments yet; the goal here is just to narrow based on the implicit isinstance check performed by the class pattern.

See https://docs.python.org/3/reference/compound_stmts.html#class-patterns for the documentation of the language feature.

Metadata

Metadata

Assignees

Labels

tyMulti-file analysis & type inference

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions