Skip to content

external

The base class for MLTE Evidence extensions.

NOTE: The ExternalEvidence implementation in this module should not be confused with the Evidence implementation in the artifact module. The Evidence implementation in artifact is a proper MLTE artifact, it should be used for all "internal" types part of the MLTE evidence system. The ExternalEvidence implementation in this module is meant to be extended by users of MLTE to enrich the evidence system with their own evidence types; it provides the link between the statically-typed MLTE evidence system and dynamic extensions.

ExternalEvidence

Bases: Evidence, ABC

The base class for MLTE evidence extensions.

Source code in mlte/evidence/external.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class ExternalEvidence(Evidence, ABC):
    """The base class for MLTE evidence extensions."""

    def __init__(self):
        """Initialize an instance"""
        super().__init__()

    @classmethod
    def __subclasshook__(cls, subclass):
        """Define the interface for all Evidence subclasses."""
        return meta.has_callables(subclass, "serialize", "deserialize")

    @abstractmethod
    def serialize(self) -> dict[str, Any]:
        """
        Serialize the Evidence to a JSON-compatible dictionary.
        :return: The dictionary representation
        """
        raise NotImplementedError("ExternalEvidence.serialize()")

    @classmethod
    @abstractmethod
    def deserialize(cls, data: dict[str, Any]) -> ExternalEvidence:
        """
        Deserialize an Evidence instance from serialized representation.
        :param data: The serialized representation
        :return: The deserialized         :param metadata: Evidence metadata associated with the evidence

        """
        raise NotImplementedError("ExternalEvidence.deserialize()")

    def __str__(self) -> str:
        """Return a string representation of this Evidence."""
        return f"{self.serialize()}"

    def to_model(self) -> ArtifactModel:
        """
        Serialize an Evidence to its corresponding model.
        :return: The artifact model
        """
        return self._to_artifact_model(
            value_model=OpaqueValueModel(data=self.serialize())
        )

    @classmethod
    def from_model(cls, model: BaseModel) -> ExternalEvidence:
        """
        Deserialize an Evidence from its corresponding model.
        :param model: The artifact model
        :return: The deserialized artifact
        """
        body = cls._check_proper_types(model, EvidenceType.OPAQUE)
        return cls.deserialize(body.value.data).with_metadata(body.metadata)  # type: ignore

__init__()

Initialize an instance

Source code in mlte/evidence/external.py
28
29
30
def __init__(self):
    """Initialize an instance"""
    super().__init__()

__str__()

Return a string representation of this Evidence.

Source code in mlte/evidence/external.py
56
57
58
def __str__(self) -> str:
    """Return a string representation of this Evidence."""
    return f"{self.serialize()}"

__subclasshook__(subclass) classmethod

Define the interface for all Evidence subclasses.

Source code in mlte/evidence/external.py
32
33
34
35
@classmethod
def __subclasshook__(cls, subclass):
    """Define the interface for all Evidence subclasses."""
    return meta.has_callables(subclass, "serialize", "deserialize")

deserialize(data) abstractmethod classmethod

Deserialize an Evidence instance from serialized representation.

Parameters:

Name Type Description Default
data dict[str, Any]

The serialized representation

required

Returns:

Type Description
ExternalEvidence

The deserialized :param metadata: Evidence metadata associated with the evidence

Source code in mlte/evidence/external.py
45
46
47
48
49
50
51
52
53
54
@classmethod
@abstractmethod
def deserialize(cls, data: dict[str, Any]) -> ExternalEvidence:
    """
    Deserialize an Evidence instance from serialized representation.
    :param data: The serialized representation
    :return: The deserialized         :param metadata: Evidence metadata associated with the evidence

    """
    raise NotImplementedError("ExternalEvidence.deserialize()")

from_model(model) classmethod

Deserialize an Evidence from its corresponding model.

Parameters:

Name Type Description Default
model BaseModel

The artifact model

required

Returns:

Type Description
ExternalEvidence

The deserialized artifact

Source code in mlte/evidence/external.py
69
70
71
72
73
74
75
76
77
@classmethod
def from_model(cls, model: BaseModel) -> ExternalEvidence:
    """
    Deserialize an Evidence from its corresponding model.
    :param model: The artifact model
    :return: The deserialized artifact
    """
    body = cls._check_proper_types(model, EvidenceType.OPAQUE)
    return cls.deserialize(body.value.data).with_metadata(body.metadata)  # type: ignore

serialize() abstractmethod

Serialize the Evidence to a JSON-compatible dictionary.

Returns:

Type Description
dict[str, Any]

The dictionary representation

Source code in mlte/evidence/external.py
37
38
39
40
41
42
43
@abstractmethod
def serialize(self) -> dict[str, Any]:
    """
    Serialize the Evidence to a JSON-compatible dictionary.
    :return: The dictionary representation
    """
    raise NotImplementedError("ExternalEvidence.serialize()")

to_model()

Serialize an Evidence to its corresponding model.

Returns:

Type Description
ArtifactModel

The artifact model

Source code in mlte/evidence/external.py
60
61
62
63
64
65
66
67
def to_model(self) -> ArtifactModel:
    """
    Serialize an Evidence to its corresponding model.
    :return: The artifact model
    """
    return self._to_artifact_model(
        value_model=OpaqueValueModel(data=self.serialize())
    )