Reputation: 1087
Given the following example:
from typing import Literal
from dataclasses import dataclass
@dataclass
Class Example:
answer: Literal['Y', 'N']
x = Example('N')
field = fields(x)[0]
How can I check that the variable field is of type Literal? issubclass(field.type, Literal)
doesn't seem to work.
Secondly how can I then get the list ['Y', 'N']
from field.type
, so that I could check the value at run time and raise an error when fail = Example('invalid')
pydantic
does this but you would have to use their drop in dataclass...
Upvotes: 0
Views: 2959
Reputation: 3535
I created a tiny Python library for this purpose: https://github.com/tamuhey/dataclass_utils
This library can be applied for such dataclass that holds another dataclass (nested dataclass), and nested container type (like Tuple[List[Dict...
).
Of course, it can test Literal
at runtime.
Upvotes: 0
Reputation: 703
from dataclasses import dataclass
from typing import Literal
from validated_dc import ValidatedDC
@dataclass
class Example(ValidatedDC):
answer: Literal['Y', 'N']
instance = Example('N')
assert instance.is_valid()
instance = Example('X')
assert not instance.is_valid()
Upvotes: 1
Reputation: 32294
Literal
is not a normal type that a python object would have at runtime, it doesn't make sense to check that an object is a Literal
You can access the annotations for a class using __annotations__
, following on from your example:
>>> Example.__annotations__['answer'].__args__
('Y', 'N')
Upvotes: 1