33
44from __future__ import annotations
55
6- from dataclasses import dataclass
76from enum import Enum
87from typing import Optional
98
9+ from pydantic import BaseModel , ConfigDict
10+
11+ from pyrit .common .deprecation import print_deprecation_message
12+
1013
1114class ConversationType (Enum ):
1215 """Types of conversations that can be associated with an attack."""
@@ -17,15 +20,15 @@ class ConversationType(Enum):
1720 CONVERTER = "converter"
1821
1922
20- @dataclass (frozen = True )
21- class ConversationReference :
23+ class ConversationReference (BaseModel ):
2224 """Immutable reference to a conversation that played a role in the attack."""
2325
26+ model_config = ConfigDict (frozen = True )
27+
2428 conversation_id : str
2529 conversation_type : ConversationType
2630 description : Optional [str ] = None
2731
28- # Allow use in set / dict
2932 def __hash__ (self ) -> int :
3033 """
3134 Return a hash derived from conversation ID.
@@ -36,45 +39,55 @@ def __hash__(self) -> int:
3639 """
3740 return hash (self .conversation_id )
3841
42+ def __eq__ (self , other : object ) -> bool :
43+ """
44+ Compare two references by conversation ID.
45+
46+ Args:
47+ other (object): Other object to compare.
48+
49+ Returns:
50+ bool: True when the other object is a matching ConversationReference.
51+
52+ """
53+ return isinstance (other , ConversationReference ) and self .conversation_id == other .conversation_id
54+
3955 def to_dict (self ) -> dict [str , str | None ]:
4056 """
4157 Serialize to a JSON-compatible dictionary.
4258
59+ .. deprecated::
60+ Use :meth:`model_dump` with ``mode="json"`` instead. This method
61+ will be removed in version 0.16.0.
62+
4363 Returns:
4464 dict[str, str | None]: Dictionary with conversation_id, conversation_type, and description.
4565 """
46- return {
47- "conversation_id" : self .conversation_id ,
48- "conversation_type" : self .conversation_type .value ,
49- "description" : self .description ,
50- }
66+ print_deprecation_message (
67+ old_item = ConversationReference .to_dict ,
68+ new_item = 'ConversationReference.model_dump(mode="json")' ,
69+ removed_in = "0.16.0" ,
70+ )
71+ return self .model_dump (mode = "json" )
5172
5273 @classmethod
5374 def from_dict (cls , data : dict [str , str | None ]) -> ConversationReference :
5475 """
5576 Reconstruct a ConversationReference from a dictionary.
5677
78+ .. deprecated::
79+ Use :meth:`model_validate` instead. This method will be removed
80+ in version 0.16.0.
81+
5782 Args:
58- data (dict[str, str | None]): Dictionary as produced by to_dict() .
83+ data (dict[str, str | None]): Dictionary as produced by ``model_dump(mode="json")`` .
5984
6085 Returns:
6186 ConversationReference: Reconstructed instance.
6287 """
63- return cls (
64- conversation_id = str ( data [ "conversation_id" ]) ,
65- conversation_type = ConversationType ( data [ "conversation_type" ]) ,
66- description = data . get ( "description" ) ,
88+ print_deprecation_message (
89+ old_item = ConversationReference . from_dict ,
90+ new_item = "ConversationReference.model_validate" ,
91+ removed_in = "0.16.0" ,
6792 )
68-
69- def __eq__ (self , other : object ) -> bool :
70- """
71- Compare two references by conversation ID.
72-
73- Args:
74- other (object): Other object to compare.
75-
76- Returns:
77- bool: True when the other object is a matching ConversationReference.
78-
79- """
80- return isinstance (other , ConversationReference ) and self .conversation_id == other .conversation_id
93+ return cls .model_validate (data )
0 commit comments