• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

deepset-ai / haystack / 13651977173

04 Mar 2025 10:48AM UTC coverage: 90.218% (+0.2%) from 90.017%
13651977173

Pull #8906

github

web-flow
Merge f9482a15a into 0d65b4caa
Pull Request #8906: refactor!: remove `dataframe` field from `Document` and `ExtractedTableAnswer`; make `pandas` optional

9601 of 10642 relevant lines covered (90.22%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.61
haystack/dataclasses/answer.py
1
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
2
#
3
# SPDX-License-Identifier: Apache-2.0
4

5
from dataclasses import asdict, dataclass, field
1✔
6
from typing import Any, Dict, List, Optional, Protocol, runtime_checkable
1✔
7

8
from haystack.core.serialization import default_from_dict, default_to_dict
1✔
9
from haystack.dataclasses.document import Document
1✔
10

11

12
@runtime_checkable
1✔
13
@dataclass
1✔
14
class Answer(Protocol):
1✔
15
    data: Any
1✔
16
    query: str
1✔
17
    meta: Dict[str, Any]
1✔
18

19
    def to_dict(self) -> Dict[str, Any]:  # noqa: D102
1✔
20
        ...
×
21

22
    @classmethod
1✔
23
    def from_dict(cls, data: Dict[str, Any]) -> "Answer":  # noqa: D102
1✔
24
        ...
×
25

26

27
@dataclass
1✔
28
class ExtractedAnswer:
1✔
29
    query: str
1✔
30
    score: float
1✔
31
    data: Optional[str] = None
1✔
32
    document: Optional[Document] = None
1✔
33
    context: Optional[str] = None
1✔
34
    document_offset: Optional["Span"] = None
1✔
35
    context_offset: Optional["Span"] = None
1✔
36
    meta: Dict[str, Any] = field(default_factory=dict)
1✔
37

38
    @dataclass
1✔
39
    class Span:
1✔
40
        start: int
1✔
41
        end: int
1✔
42

43
    def to_dict(self) -> Dict[str, Any]:
1✔
44
        """
45
        Serialize the object to a dictionary.
46

47
        :returns:
48
            Serialized dictionary representation of the object.
49
        """
50
        document = self.document.to_dict(flatten=False) if self.document is not None else None
1✔
51
        document_offset = asdict(self.document_offset) if self.document_offset is not None else None
1✔
52
        context_offset = asdict(self.context_offset) if self.context_offset is not None else None
1✔
53
        return default_to_dict(
1✔
54
            self,
55
            data=self.data,
56
            query=self.query,
57
            document=document,
58
            context=self.context,
59
            score=self.score,
60
            document_offset=document_offset,
61
            context_offset=context_offset,
62
            meta=self.meta,
63
        )
64

65
    @classmethod
1✔
66
    def from_dict(cls, data: Dict[str, Any]) -> "ExtractedAnswer":
1✔
67
        """
68
        Deserialize the object from a dictionary.
69

70
        :param data:
71
            Dictionary representation of the object.
72
        :returns:
73
            Deserialized object.
74
        """
75
        init_params = data.get("init_parameters", {})
1✔
76
        if (doc := init_params.get("document")) is not None:
1✔
77
            data["init_parameters"]["document"] = Document.from_dict(doc)
1✔
78

79
        if (offset := init_params.get("document_offset")) is not None:
1✔
80
            data["init_parameters"]["document_offset"] = ExtractedAnswer.Span(**offset)
1✔
81

82
        if (offset := init_params.get("context_offset")) is not None:
1✔
83
            data["init_parameters"]["context_offset"] = ExtractedAnswer.Span(**offset)
1✔
84
        return default_from_dict(cls, data)
1✔
85

86

87
@dataclass
1✔
88
class GeneratedAnswer:
1✔
89
    data: str
1✔
90
    query: str
1✔
91
    documents: List[Document]
1✔
92
    meta: Dict[str, Any] = field(default_factory=dict)
1✔
93

94
    def to_dict(self) -> Dict[str, Any]:
1✔
95
        """
96
        Serialize the object to a dictionary.
97

98
        :returns:
99
            Serialized dictionary representation of the object.
100
        """
101
        documents = [doc.to_dict(flatten=False) for doc in self.documents]
1✔
102
        return default_to_dict(self, data=self.data, query=self.query, documents=documents, meta=self.meta)
1✔
103

104
    @classmethod
1✔
105
    def from_dict(cls, data: Dict[str, Any]) -> "GeneratedAnswer":
1✔
106
        """
107
        Deserialize the object from a dictionary.
108

109
        :param data:
110
            Dictionary representation of the object.
111

112
        :returns:
113
            Deserialized object.
114
        """
115
        init_params = data.get("init_parameters", {})
1✔
116
        if (documents := init_params.get("documents")) is not None:
1✔
117
            data["init_parameters"]["documents"] = [Document.from_dict(d) for d in documents]
1✔
118

119
        return default_from_dict(cls, data)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc