Skip to content

Commit 20f795c

Browse files
authored
Fixed Context not always requiring a call-arg type ignore (#1009)
1 parent e31d3bb commit 20f795c

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

paperqa/types.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,18 @@ def __hash__(self) -> int:
127127
return hash((self.name, self.text))
128128

129129

130+
# Sentinel to autopopulate a field within model_validator
131+
AUTOPOPULATE_VALUE = "" # NOTE: this is falsy by design
132+
133+
130134
class Context(BaseModel):
131135
"""A class to hold the context of a question."""
132136

133137
model_config = ConfigDict(extra="allow")
134138

135139
id: str = Field(
140+
default=AUTOPOPULATE_VALUE,
136141
description="Unique identifier for the context. Auto-generated if not provided.",
137-
init=False,
138142
)
139143

140144
context: str = Field(description="Summary of the text with respect to a question.")
@@ -159,15 +163,17 @@ def __str__(self) -> str:
159163

160164
@model_validator(mode="before")
161165
@classmethod
162-
def populate_id(cls, data: Any) -> Any:
163-
if not data.get("id"):
166+
def populate_id(cls, data: dict[str, Any]) -> dict[str, Any]:
167+
if not data.get("id"): # NOTE: this includes missing or empty strings
164168
content = (
165169
data.get("question", "")
166170
+ data.get("context", "")[: cls.CONTEXT_ENCODING_LENGTH]
167171
)
168-
data["id"] = cls.REFERENCE_TEMPLATE.format(
169-
id=encode_id(content or str(uuid4()), maxsize=cls.ID_HASH_LENGTH)
170-
)
172+
return data | { # Avoid mutating input data
173+
"id": cls.REFERENCE_TEMPLATE.format(
174+
id=encode_id(content or str(uuid4()), maxsize=cls.ID_HASH_LENGTH)
175+
)
176+
}
171177
return data
172178

173179

@@ -485,9 +491,6 @@ def update_other(self, other: dict[str, Any] | None = None) -> dict[str, Any]:
485491
class DocDetails(Doc):
486492
model_config = ConfigDict(validate_assignment=True, extra="ignore")
487493

488-
# Sentinel to auto-populate a field within model_validator
489-
AUTOPOPULATE_VALUE: ClassVar[str] = ""
490-
491494
docname: str = AUTOPOPULATE_VALUE
492495
dockey: DocKey = AUTOPOPULATE_VALUE
493496
citation: str = AUTOPOPULATE_VALUE

tests/test_agents.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ def test_answers_are_striped() -> None:
953953
session = PQASession(
954954
question="What is the meaning of life?",
955955
contexts=[
956-
Context( # type: ignore[call-arg]
956+
Context(
957957
context="bla",
958958
question="foo",
959959
text=Text(

0 commit comments

Comments
 (0)