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
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):
Bug Report
When using the union of
Literal
type anduuid.UUID
as a type, after comparing with a literal value, the value does not narrow to the typeuuid.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.To Reproduce
https://mypy-play.net/?mypy=1.14.1&python=3.13&gist=04f8465218eabef5641c72b78e4a7e67
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.ini
(and other config files):The text was updated successfully, but these errors were encountered: