Skip to content

Validate literal types #24

@rishi-menon

Description

@rishi-menon

Minimum working example:

from dataclasses import dataclass
from typing import Literal
from dataclass_type_validator import dataclass_validate

@dataclass_validate
@dataclass
class ExampleClass:
    check: Literal["success", "failure"] | None = None  # Should be "success" or "failure"


a = ExampleClass("success")
b = ExampleClass("failure")
c = ExampleClass(None)
d = ExampleClass("invalid") # This passes validation even though it shouldn't

I tried strict = True, but that gives the following error:

RuntimeError: Unknown type of typing.Optional[typing.Literal['success', 'failure']] (_name = Optional)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions