diff --git a/pyrit/prompt_converter/add_image_text_converter.py b/pyrit/prompt_converter/add_image_text_converter.py index bd1ad5daf6..89e11c0abc 100644 --- a/pyrit/prompt_converter/add_image_text_converter.py +++ b/pyrit/prompt_converter/add_image_text_converter.py @@ -3,13 +3,13 @@ import base64 import logging -import warnings from io import BytesIO from typing import cast from PIL import Image, ImageFont from PIL.ImageFont import FreeTypeFont +from pyrit.common.deprecation import print_deprecation_message from pyrit.models import ComponentIdentifier, PromptDataType, data_serializer_factory from pyrit.prompt_converter.base_image_text_converter import _BaseImageTextConverter from pyrit.prompt_converter.prompt_converter import ConverterResult @@ -82,12 +82,10 @@ def __init__( raise TypeError(f"AddImageTextConverter takes at most 1 positional argument, got {len(args)}") if img_to_add: raise TypeError("Cannot pass img_to_add as both positional and keyword argument") - warnings.warn( - "Passing 'img_to_add' as a positional argument is deprecated. " - "Use img_to_add=... as a keyword argument. " - "It will be keyword-only starting in version 0.15.0.", - FutureWarning, - stacklevel=2, + print_deprecation_message( + old_item="Passing img_to_add as a positional argument to AddImageTextConverter", + new_item="AddImageTextConverter(img_to_add=...) keyword argument", + removed_in="0.15.0", ) img_to_add = args[0] if x_pos is not _UNSET or y_pos is not _UNSET: @@ -95,11 +93,10 @@ def __init__( raise ValueError( "Cannot pass x_pos/y_pos together with bounding_box. Use bounding_box=(x, y, x2, y2) instead." ) - warnings.warn( - "x_pos and y_pos are deprecated. Use bounding_box=(x, y, x2, y2) instead. " - "They will be removed in version 0.15.0.", - FutureWarning, - stacklevel=2, + print_deprecation_message( + old_item="AddImageTextConverter(x_pos=..., y_pos=...)", + new_item="AddImageTextConverter(bounding_box=(x1, y1, x2, y2))", + removed_in="0.15.0", ) # Resolve defaults after deprecation check if x_pos is _UNSET: diff --git a/pyrit/prompt_converter/add_text_image_converter.py b/pyrit/prompt_converter/add_text_image_converter.py index 76476911c1..ed34dd81a1 100644 --- a/pyrit/prompt_converter/add_text_image_converter.py +++ b/pyrit/prompt_converter/add_text_image_converter.py @@ -4,13 +4,13 @@ import base64 import hashlib import logging -import warnings from io import BytesIO from typing import cast from PIL import Image, ImageFont from PIL.ImageFont import FreeTypeFont +from pyrit.common.deprecation import print_deprecation_message from pyrit.models import ComponentIdentifier, PromptDataType, data_serializer_factory from pyrit.prompt_converter.base_image_text_converter import _BaseImageTextConverter from pyrit.prompt_converter.prompt_converter import ConverterResult @@ -62,12 +62,10 @@ def __init__( raise TypeError(f"AddTextImageConverter takes at most 1 positional argument, got {len(args)}") if text_to_add: raise TypeError("Cannot pass text_to_add as both positional and keyword argument") - warnings.warn( - "Passing 'text_to_add' as a positional argument is deprecated. " - "Use text_to_add=... as a keyword argument. " - "It will be keyword-only starting in version 0.15.0.", - FutureWarning, - stacklevel=2, + print_deprecation_message( + old_item="Passing text_to_add as a positional argument to AddTextImageConverter", + new_item="AddTextImageConverter(text_to_add=...) keyword argument", + removed_in="0.15.0", ) text_to_add = args[0] if text_to_add.strip() == "": diff --git a/tests/unit/prompt_converter/test_add_image_text_converter.py b/tests/unit/prompt_converter/test_add_image_text_converter.py index 4fb68275d3..e9d12fcede 100644 --- a/tests/unit/prompt_converter/test_add_image_text_converter.py +++ b/tests/unit/prompt_converter/test_add_image_text_converter.py @@ -42,7 +42,7 @@ def test_add_image_text_converter_initialization(image_text_converter_sample_ima def test_add_image_text_converter_positional_arg_deprecation(image_text_converter_sample_image): - with pytest.warns(FutureWarning, match="Passing 'img_to_add' as a positional argument is deprecated"): + with pytest.warns(DeprecationWarning, match="Passing img_to_add as a positional argument to AddImageTextConverter"): converter = AddImageTextConverter(image_text_converter_sample_image) assert converter._img_to_add == image_text_converter_sample_image @@ -58,12 +58,12 @@ def test_add_image_text_converter_too_many_positional_args_raises(image_text_con def test_add_image_text_converter_x_pos_y_pos_deprecation(image_text_converter_sample_image): - with pytest.warns(FutureWarning, match="x_pos and y_pos are deprecated"): + with pytest.warns(DeprecationWarning, match=r"AddImageTextConverter\(x_pos=\.\.\., y_pos=\.\.\.\)"): AddImageTextConverter(img_to_add=image_text_converter_sample_image, x_pos=50, y_pos=50) def test_add_image_text_converter_x_pos_y_pos_deprecation_default_value(image_text_converter_sample_image): - with pytest.warns(FutureWarning, match="x_pos and y_pos are deprecated"): + with pytest.warns(DeprecationWarning, match=r"AddImageTextConverter\(x_pos=\.\.\., y_pos=\.\.\.\)"): AddImageTextConverter(img_to_add=image_text_converter_sample_image, x_pos=10) @@ -71,7 +71,7 @@ def test_add_image_text_converter_no_x_pos_y_pos_no_warning(image_text_converter import warnings with warnings.catch_warnings(): - warnings.simplefilter("error", FutureWarning) + warnings.simplefilter("error", DeprecationWarning) AddImageTextConverter(img_to_add=image_text_converter_sample_image) diff --git a/tests/unit/prompt_converter/test_add_text_image_converter.py b/tests/unit/prompt_converter/test_add_text_image_converter.py index 0c1f461675..45c2887142 100644 --- a/tests/unit/prompt_converter/test_add_text_image_converter.py +++ b/tests/unit/prompt_converter/test_add_text_image_converter.py @@ -38,6 +38,24 @@ def test_add_text_image_converter_invalid_font(): AddTextImageConverter(text_to_add="Sample text", font_name="helvetica.otf") # Invalid font extension +def test_add_text_image_converter_positional_arg_deprecation(): + with pytest.warns( + DeprecationWarning, match="Passing text_to_add as a positional argument to AddTextImageConverter" + ): + converter = AddTextImageConverter("Sample text") + assert converter._text_to_add == "Sample text" + + +def test_add_text_image_converter_positional_and_keyword_raises(): + with pytest.raises(TypeError, match="Cannot pass text_to_add as both positional and keyword"): + AddTextImageConverter("Sample text", text_to_add="Sample text") + + +def test_add_text_image_converter_too_many_positional_args_raises(): + with pytest.raises(TypeError, match="takes at most 1 positional argument"): + AddTextImageConverter("Sample text", "extra") + + def test_add_text_image_converter_invalid_text_to_add(): with pytest.raises(ValueError): AddTextImageConverter(text_to_add="", font_name="helvetica.ttf")