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...
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')
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()
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.