Pydantic Integration
With typed-logic, you can use whatever framework you like to define the classes that specify predicate definitions in your theory:
- Python
dataclasses
(part of the Python standard) - Hand-rolled classes
- Pydantic
The only requirement is that you inherit from Fact
or FactMixin
, or declare your class
to be a predicate definition with @predicate
. For convenience, there is a ready-made base class for you to use.
However, there are some specific considerations for Pydantic due to its aversion to positional arguments.
Using the Pydantic bridge
from typedlogic.integrations.frameworks.pydantic import FactBaseModel
from pydantic import Field
class Person(FactBaseModel):
name: str = Field(..., description="unique name of the person")
age: int = Field(..., description="age in years", ge=0)
Person("Akira", 20)
from pydantic import Field, ValidationError
try:
Person("Akira", "20 years")
except ValidationError as e:
print("Got in error (this is expected!)")
print(e)
from utils import show
show("examples/pydantic_example.py")
import examples.pydantic_example
from typedlogic.parsers.pyparser import PythonParser
pp = PythonParser()
theory = pp.parse(open("examples/pydantic_example.py"))
theory.predicate_definitions[0]