Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Change no_answer attribute #3411

Merged
merged 10 commits into from
Oct 25, 2022
Merged
9 changes: 5 additions & 4 deletions haystack/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,17 +533,18 @@ def __init__(
# If an Answer is provided we need to make sure that it's consistent with the `no_answer` value
# TODO: reassess if we want to enforce Span.start=0 and Span.end=0 for no_answer=True
if self.answer is not None:
if no_answer is None:
# Set default before validation
no_answer = self.answer.answer == "" or self.answer.answer is None
ZanSara marked this conversation as resolved.
Show resolved Hide resolved

if no_answer == True:
if self.answer.answer != "" or self.answer.context:
raise ValueError(f"Got no_answer == True while there seems to be an possible Answer: {self.answer}")
raise ValueError(f"Got no_answer == True while there seems to be a possible Answer: {self.answer}")
elif no_answer == False:
if self.answer.answer == "":
raise ValueError(
f"Got no_answer == False while there seems to be no possible Answer: {self.answer}"
)
else:
# Automatically infer no_answer from Answer object
no_answer = self.answer.answer == "" or self.answer.answer is None

self.no_answer = no_answer

Expand Down
12 changes: 12 additions & 0 deletions test/others/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ def test_no_answer_label():
assert labels[3].no_answer == False


def test_invalid_label():
with pytest.raises(ValueError, match="there seems to be a possible Answer"):
_ = Label(
answer=Answer(answer="", context="some value"),
query="test",
document=Document("doc"),
is_correct_answer=True,
is_correct_document=True,
origin="user-feedback",
)


def test_equal_label():
assert LABELS[2] == LABELS[0]
assert LABELS[1] != LABELS[0]
Expand Down