Skip to content

Commit

Permalink
More idiomatic way to exclude path from serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
scosman committed Aug 20, 2024
1 parent 23c8fbe commit a62797b
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions libs/core/kiln_ai/datamodel/basemodel.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pydantic import BaseModel, computed_field
from pydantic import BaseModel, computed_field, Field
from typing import Optional
from pathlib import Path
from typing import Type, TypeVar
Expand All @@ -8,16 +8,12 @@

class KilnBaseModel(BaseModel):
v: int = 1 # schema_version
path: Optional[Path] = None
path: Optional[Path] = Field(default=None, exclude=True)

@computed_field()
def type(self) -> str:
return self.type_name()

# def __init__(self, **data: Any):
# # automatically set type name
# super().__init__(**data, type=self.type_name())

# override this to set the type name explicitly
def type_name(self) -> str:
return self.__class__.__name__
Expand All @@ -41,7 +37,7 @@ def save_to_file(self) -> None:
f"Cannot save to file because 'path' is not set. Class: {self.__class__.__name__}, "
f"id: {getattr(self, 'id', None)}, path: {self.path}"
)
json_data = self.model_dump_json(exclude={"path"}, indent=2)
json_data = self.model_dump_json(indent=2)
with open(self.path, "w") as file:
file.write(json_data)

Expand Down

0 comments on commit a62797b

Please sign in to comment.