1 from .api import TypeProviderBase
2 from .utils import is_primitive, is_basic
4 class PrimitiveType(TypeProviderBase):
5 def __init__(self, type) -> None:
9 def typedef_matched(typedef):
10 return is_primitive(typedef)
13 return isinstance(val, self._type)
15 def serialise(self, val):
18 def deserialise(self, val):
19 if val.lower() == "true":
21 elif val.lower() == "false":
24 return self._type(val)
26 def parse_input(self, input_str):
27 return self.deserialise(input_str)
29 def to_input(self, val):
30 return self.serialise(val)
32 def __str__(self) -> str:
33 if isinstance(self._type, type):
34 return f"any with type of {self._type}"
35 return f"exact of value '{self._type}'"
38 class MultipleChoiceType(PrimitiveType):
39 def __init__(self, type) -> None:
40 super().__init__(type)
43 def typedef_matched(typedef):
44 if not isinstance(typedef, list):
46 return all([is_basic(x) for x in typedef])
51 return val in self._type
53 def parse_input(self, input_str):
54 return super().parse_input(input_str)
56 def deserialise(self, val):
57 if val.lower() == "true":
59 elif val.lower() == "false":
70 return None in self._type
72 def __str__(self) -> str:
73 accepted = [f" * {t}" for t in self._type]