Skip to content

Support Literal[bool] #859

@peku33

Description

@peku33

Description

Many APIs use something like {"success": true, data: ...}.

It will be really nice to be able to validate such boolean field against constant value when deserializing:

from typing import Literal

import msgspec


class ResponseData(msgspec.Struct):
    pass


class Response(msgspec.Struct):
    success: Literal[True]
    data: ResponseData


response = msgspec.convert(
    {
        "success": True,
        "data": {},
    },
    Response,
)

At the moment this raises:

TypeError: Literal may only contain None/integers/strings - typing.Literal[True] is not supported

Thank you 👍

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions