Skip to content

Multi-level narrowing not working properly #2745

@grievejia

Description

@grievejia

Describe the Bug

Minimal repro:

class Message:
    def __init__(self, content: str | None = None) -> None:
        self.content = content

def has_error_marker(messages: list[Message]) -> bool:
    if (
        len(messages) > 0
        and messages[-1].content  # narrows str | None -> str
        and "[ERROR]" in messages[-1].content  # pyrefly: `in` ... and `None`
    ):
        return True
    return False

No other type checker disallows this code.

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeSIAxlKnHAAQCy8aA5jIgDrr2-2Yww9APrCI6CABdRACjgwoYADT1KudJJgbE9OJIBO9AD70Acupj0AvGYsBKegFoAfLfQdufL7oVhCajS1Ja1V1TQ1ubgEhAAtaYRh9fVx9YQBbVH0Aa0SZNJZUdjgdKAg9AG1mOkKYAF0HF3o8XCguHj4IIRlPb15YdDyCoodXAAYe3oxMenzqovLHAEZa-zCg3gBienRM5IB3Bj1DE3N3J1cjie8p%2Bk4QcoBRACUngHkn2rv6cRmh%2BAXlqtAhpNvQyPpBFBSDoAAbiGH0QhI%2Bg3GGnGAwq52Nq9XgQyQAV30PAAKvoCTArviiTwAGKoKDyEBKEAEyTQOAkciIEBbACq7NKklI9DABPQlHZ6jgkSwglFKQy0nQBLS2Fy%2BB04kkDQuBhxfGpxNFd1MqvV%2Bh0wHwAF87txmSBwZDSIRJLg0lAKFsAAqkCFgKG6KZ4fChdCQVhE1BS9CEbhbADKMEsMUkkmIxQA9FnnYHXSlWFmtFnMLhKHAswFI9HY1mFYZUAA3VDQVDYWDhmv6GMQdT0XDEWOc7hkSQxdSOJuJOB9ng2O4AZkIiwATPb0CAbSzUJKINPadAYBQ0FhQ1yt0A

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

Labels

narrowingIssues with narrowing - root cause is usually narrowing, flow handling, or bothtypecheckingusabilityUsability & readiness issues identified with running Pyrefly on top OSS projects

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions