fix: logging for adapted entries should include superclasses

This commit is contained in:
gmega 2025-02-19 18:16:26 -03:00
parent 9563a96373
commit 1ab2112542
No known key found for this signature in database
GPG Key ID: 6290D34EAD824B18
2 changed files with 23 additions and 1 deletions

View File

@ -53,9 +53,11 @@ class LogEntry(SnakeCaseModel):
def recover_instance(self):
return model.model_validate(self.model_dump())
parents = [base for base in model.__bases__ if issubclass(base, BaseModel)]
adapted = type(
f"{model.__name__}LogEntry",
(LogEntry,),
tuple([LogEntry] + parents),
{
"__annotations__": model.__annotations__,
"adapt_instance": classmethod(adapt_instance),

View File

@ -2,6 +2,8 @@ import datetime
from collections import defaultdict
from io import StringIO
from pydantic import BaseModel
from benchmarks.logging.logging import (
LogEntry,
LogParser,
@ -236,3 +238,21 @@ def test_should_store_split_logs_as_jsonl_for_requested_types():
{"name":"start2","timestamp":"2021-01-01T00:00:00Z"}
""")
)
class AModel(BaseModel):
a: int
class BModel(AModel):
b: int
def test_should_log_attributes_for_superclasses_in_adapted_entries():
BModelLogEntry = LogEntry.adapt(BModel)
instance = BModel(a=1, b=2)
assert (
str(BModelLogEntry.adapt_instance(instance))
== '>>{"a":1,"b":2,"entry_type":"b_model_log_entry"}'
)