Formatron v0.4.9
Formatron empowers everyone to control the output format of language models with minimal overhead.
Loading...
Searching...
No Matches
schema.py
Go to the documentation of this file.
1"""
2This module contains the Schema abstract class and FieldInfo abstract class.
3"""
4import abc
5from dataclasses import dataclass
6import typing
7
8
9class FieldInfo(abc.ABC):
10 """
11 An abstract field info that describes a data field in a schema.
12 """
13 @property
14 @abc.abstractmethod
15 def annotation(self) -> typing.Type[typing.Any] | None:
16 """
17 Get the type annotation of the field.
18 """
19 pass
20
21 @property
22 @abc.abstractmethod
23 def required(self) -> bool:
24 """
25 Check if the field is required for the schema.
26 """
27 pass
28
29class TypeWithMetadata:
30 """
31 A type with metadata.
32 """
33 def __init__(self, type: typing.Type[typing.Any], metadata: dict[str, typing.Any]|None):
34 self._type = type
35 self._metadata = metadata
36
37 @property
38 def type(self) -> typing.Type[typing.Any]:
39 """
40 Get the type of the type with metadata.
41 """
42 return self._type
44 @property
45 def metadata(self) -> dict[str, typing.Any]|None:
46 """
47 Get the metadata of the type with metadata.
48 """
49 return self._metadata
50
51class Schema(abc.ABC):
52 """
53 An abstract schema that describes some data.
54 """
55 @classmethod
56 @abc.abstractmethod
57 def fields(cls) -> dict[str, FieldInfo]:
58 """
59 Get the fields of the schema.
60 """
61 pass
62
63 @classmethod
64 @abc.abstractmethod
65 def from_json(cls, json: str) -> "Schema":
66 """
67 Create a schema from a JSON string.
68 """
69 pass
70
71@dataclass
72class SubstringOf:
73 """
74 A metadata class that indicates that the field is a substring of the given string.
75 """
76 substring_of: str
An abstract field info that describes a data field in a schema.
Definition schema.py:13
typing.Type[typing.Any]|None annotation(self)
Get the type annotation of the field.
Definition schema.py:26
bool required(self)
Check if the field is required for the schema.
Definition schema.py:43
An abstract schema that describes some data.
Definition schema.py:91
A metadata class that indicates that the field is a substring of the given string.
Definition schema.py:130