Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate literal types #24

Open
rishi-menon opened this issue Oct 29, 2024 · 0 comments
Open

Validate literal types #24

rishi-menon opened this issue Oct 29, 2024 · 0 comments

Comments

@rishi-menon
Copy link

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant