Skip to content

Final (context-sensitive Literal) doesn't support type narrowing #19814

Open

Description

@Dreamsorcerer

Bug Report

Maybe there's a reason this is not supported, but at a quick look it seems like it should work. When a union of context-sensitive Literals are used, it fails to perform type narrowing, whereas explicit Literals do work.

To Reproduce

from typing import Final, Literal

class TXTRecord:
    type: Final = "TXT"
    text: str = "Foo"

class ARecord:
    type: Final = "A"
    host: str = "192.168.1.1"

record: TXTRecord | ARecord
reveal_type(record.type)
if record.type == "TXT":
    print(record.text)  # error with Final, but not with Literal
else:
    print(record.host)  # error with Final, but not with Literal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions