Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions pyrit/prompt_converter/add_image_text_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -82,24 +82,21 @@ 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:
if bounding_box is not None:
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:
Expand Down
12 changes: 5 additions & 7 deletions pyrit/prompt_converter/add_text_image_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Comment thread
romanlutz marked this conversation as resolved.
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() == "":
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/prompt_converter/test_add_image_text_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -58,20 +58,20 @@ 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)


def test_add_image_text_converter_no_x_pos_y_pos_no_warning(image_text_converter_sample_image):
import warnings

with warnings.catch_warnings():
warnings.simplefilter("error", FutureWarning)
warnings.simplefilter("error", DeprecationWarning)
AddImageTextConverter(img_to_add=image_text_converter_sample_image)


Expand Down
18 changes: 18 additions & 0 deletions tests/unit/prompt_converter/test_add_text_image_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading