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
Type narrowing for unions Literal | uuid.UUID doesn't work #18569
When using the union of Literal type and uuid.UUID as a type, after comparing with a literal value, the value does not narrow to the type uuid.UUID.
However, this behavior is observed only when uuid.UUID is used as the type. If other types, such as built-ins and custom classes, are used instead, the comparison with a literal value results in the value narrowing to the appropriate type.
fromtypingimportLiteralfromuuidimportUUID
type TLiteral=Literal["text"]
TEXT: TLiteral="text"# type T = int # works good
type T=UUID# failsdeff(v: T|TLiteral) ->T|None:
ifv==TEXT:
returnNonereturnv
Expected Behavior
Success: no issues found in 1 source file
Actual Behavior
error: Incompatible return value type (got "UUID | Literal['text']", expected "UUID | None"
Your Environment
Mypy version used: 1.14.1
Mypy command-line flags:
Mypy configuration options from mypy.ini (and other config files):
Activity
A5rocks commentedon Jan 30, 2025
Reproduce without
uuid(and with less concepts):I think the negative narrowing of
==for literals is incorrect here.A5rocks commentedon Jan 30, 2025
Duplicate of #16465