You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Final (context-sensitive Literal) doesn't support type narrowing #19814
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
fromtypingimportFinal, LiteralclassTXTRecord:
type: Final="TXT"text: str="Foo"classARecord:
type: Final="A"host: str="192.168.1.1"record: TXTRecord|ARecordreveal_type(record.type)
ifrecord.type=="TXT":
print(record.text) # error with Final, but not with Literalelse:
print(record.host) # error with Final, but not with Literal
Activity