From bad4cf65b06b4b15a30f7fe2820a35194abbc0d2 Mon Sep 17 00:00:00 2001 From: Roman Lutz Date: Mon, 1 Jun 2026 11:19:45 -0700 Subject: [PATCH 1/2] MAINT: Migrate AddImage/AddTextImage converter deprecations to print_deprecation_message MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The PyRIT style guide (.github/instructions/style-guide.instructions.md, Deprecations section) requires all deprecations to go through pyrit.common.deprecation.print_deprecation_message — never raw warnings.warn — so message format and DeprecationWarning filtering stay consistent across the codebase. Three call sites in the prompt_converter package bypassed the helper by emitting FutureWarning directly. They are all positional/legacy-kwarg migrations introduced in commit bde0ed1ade (PR #1591) on 2026-04-22, during the 0.14.0.dev0 cycle (i.e. after v0.13.0 was tagged). The originally announced removal version is 0.15.0, which has not yet shipped to users, so we preserve removed_in="0.15.0" on all three — this migrates the *mechanism* without resetting the deprecation clock. Migrated sites: - pyrit/prompt_converter/add_text_image_converter.py:66 (positional text_to_add → keyword) - pyrit/prompt_converter/add_image_text_converter.py:86 (positional img_to_add → keyword) - pyrit/prompt_converter/add_image_text_converter.py:99 (x_pos/y_pos → bounding_box) The warning class changes FutureWarning → DeprecationWarning as a side effect of using the helper, so the three corresponding pytest.warns assertions and the warnings.simplefilter("error", ...) check in tests/unit/prompt_converter/test_add_image_text_converter.py were updated to match. Verified separately: the two warnings.warn(..., UserWarning, ...) calls in pyrit/prompt_target/hugging_face/{hugging_face_endpoint_target, hugging_face_chat_target}.py are legitimate user-misconfiguration warnings about sampling parameters (not deprecations) and were left untouched, as was the ExperimentalWarning in pyrit/auxiliary_attacks/__init__.py. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../add_image_text_converter.py | 21 ++++++++----------- .../add_text_image_converter.py | 12 +++++------ .../test_add_image_text_converter.py | 8 +++---- 3 files changed, 18 insertions(+), 23 deletions(-) diff --git a/pyrit/prompt_converter/add_image_text_converter.py b/pyrit/prompt_converter/add_image_text_converter.py index cd08c2a438..1d6425f9c9 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.identifiers import ComponentIdentifier from pyrit.models import PromptDataType, data_serializer_factory from pyrit.prompt_converter.base_image_text_converter import _BaseImageTextConverter @@ -83,12 +83,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: @@ -96,11 +94,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 759a649942..c0ece8f84e 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.identifiers import ComponentIdentifier from pyrit.models import PromptDataType, data_serializer_factory from pyrit.prompt_converter.base_image_text_converter import _BaseImageTextConverter @@ -63,12 +63,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) From 1840b692e854d6240650f182b8cd96eb50966361 Mon Sep 17 00:00:00 2001 From: Roman Lutz Date: Mon, 1 Jun 2026 16:35:40 -0700 Subject: [PATCH 2/2] TEST: Cover deprecated positional path in AddTextImageConverter Adds three tests mirroring AddImageTextConverter's positional-arg coverage: - positional text_to_add emits DeprecationWarning and still works - positional + keyword raises TypeError - 2+ positional args raises TypeError Lifts diff-cover for the merged branch back above the 90% gate. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../test_add_text_image_converter.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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")