Skip to content

Commit 31e01bc

Browse files
authored
Sorting Doc.fields_to_overwrite_from_metadata serialization (#1010)
1 parent 85a4981 commit 31e01bc

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

paperqa/types.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from copy import deepcopy
99
from datetime import datetime
1010
from enum import StrEnum
11-
from typing import Any, ClassVar, cast
11+
from typing import Annotated, Any, ClassVar, cast
1212
from uuid import UUID, uuid4
1313

1414
import tiktoken
@@ -21,6 +21,7 @@
2121
BaseModel,
2222
ConfigDict,
2323
Field,
24+
PlainSerializer,
2425
computed_field,
2526
field_validator,
2627
model_validator,
@@ -59,9 +60,12 @@ class Doc(Embeddable):
5960
docname: str
6061
dockey: DocKey
6162
citation: str
62-
fields_to_overwrite_from_metadata: set[str] = Field(
63-
default_factory=lambda: set(DEFAULT_FIELDS_TO_OVERWRITE_FROM_METADATA),
64-
description="fields from metadata to overwrite when upgrading to a DocDetails",
63+
# Sort the serialization to minimize the diff of serialized objects
64+
fields_to_overwrite_from_metadata: Annotated[set[str], PlainSerializer(sorted)] = (
65+
Field(
66+
default_factory=lambda: set(DEFAULT_FIELDS_TO_OVERWRITE_FROM_METADATA),
67+
description="fields from metadata to overwrite when upgrading to a DocDetails",
68+
)
6569
)
6670

6771
@model_validator(mode="before")

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ unfixable = [
536536
"ERA001", # While debugging, temporarily commenting code can be useful
537537
"F401", # While debugging, unused imports can be useful
538538
"F841", # While debugging, unused locals can be useful
539+
"PIE794", # Autoremoving the latter of two fields is dangerous
539540
"RUF059", # While debugging, unused locals can be useful
540541
"TC004", # While debugging, it can be nice to keep TYPE_CHECKING in-tact
541542
]

0 commit comments

Comments
 (0)