Skip to content

Incorrect type narrowing for literal patterns #1349

@erictraut

Description

@erictraut

Summary

I just fixed a longstanding bug in pyright that ty appears to suffer from too. It's related to literal patterns in case statements. The runtime uses equality checks for literal pattern matching, but equality checks can succeed for non-overlapping types.

Consider the following:

def func(x: int):
    match x:
        case 1.0:
            reveal_type(x) # ty incorrectly reveals Never here
            print("This code is reachable")

func(1)

Version

No response

Metadata

Metadata

Labels

bugSomething isn't workingnarrowingrelated to flow-sensitive type narrowingruntime semanticsAccurate modeling of how Python's semantics work at runtime

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions