Skip to content

Commit 48ebd5c

Browse files
authored
MAINT: Fix docstrings for pyrit/chat_message_normalizer and pyrit/cli (microsoft#1196)
1 parent f02d3f2 commit 48ebd5c

14 files changed

Lines changed: 82 additions & 19 deletions

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ extend-select = [
251251
# Temporary ignores for pyrit/ subdirectories until issue #1176
252252
# https://github.com/Azure/PyRIT/issues/1176 is fully resolved
253253
# TODO: Remove these ignores once the issues are fixed
254-
"pyrit/{auxiliary_attacks,chat_message_normalizer,cli,embedding,exceptions,executor,memory,models,prompt_converter,prompt_normalizer,prompt_target,scenarios,score,setup,ui}/**/*.py" = ["D101", "D102", "D103", "D104", "D105", "D106", "D107", "D401", "D404", "D417", "D418", "DOC102", "DOC201", "DOC202", "DOC402", "DOC501"]
254+
"pyrit/{auxiliary_attacks,exceptions,executor,memory,models,prompt_converter,prompt_normalizer,prompt_target,scenarios,score,setup,ui}/**/*.py" = ["D101", "D102", "D103", "D104", "D105", "D106", "D107", "D401", "D404", "D417", "D418", "DOC102", "DOC201", "DOC202", "DOC402", "DOC501"]
255255
"pyrit/__init__.py" = ["D104"]
256256

257257
[tool.ruff.lint.pydocstyle]

pyrit/chat_message_normalizer/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Licensed under the MIT license.
33

44
"""
5-
This module contains the functionality to normalize chat messages into compatible formats for targets.
5+
Functionality to normalize chat messages into compatible formats for targets.
66
"""
77

88
from pyrit.chat_message_normalizer.chat_message_normalizer import ChatMessageNormalizer

pyrit/chat_message_normalizer/chat_message_nop.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ChatMessageNop(ChatMessageNormalizer[list[ChatMessage]]):
1212

1313
def normalize(self, messages: list[ChatMessage]) -> list[ChatMessage]:
1414
"""
15-
Returns the same list as was passed in.
15+
Return the same list as was passed in.
1616
1717
Args:
1818
messages (list[ChatMessage]): The list of messages to normalize.

pyrit/chat_message_normalizer/chat_message_normalizer.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,30 @@
1010

1111

1212
class ChatMessageNormalizer(abc.ABC, Generic[T]):
13+
"""
14+
Abstract base class for normalizing chat messages for model or target compatibility.
15+
"""
16+
1317
@abc.abstractmethod
1418
def normalize(self, messages: list[ChatMessage]) -> T:
1519
"""
16-
Normalizes the list of chat messages into a compatible format for the model or target.
20+
Normalize the list of chat messages into a compatible format for the model or target.
1721
"""
1822

1923
@staticmethod
2024
def squash_system_message(messages: list[ChatMessage], squash_function) -> list[ChatMessage]:
2125
"""
22-
Combines the system message into the first user request.
26+
Combine the system message into the first user request.
2327
2428
Args:
2529
messages: The list of chat messages.
2630
squash_function: The function to combine the system message with the user message.
2731
2832
Returns:
2933
The list of chat messages with squashed system messages.
34+
35+
Raises:
36+
ValueError: If the chat message list is empty.
3037
"""
3138
if not messages:
3239
raise ValueError("ChatMessage list cannot be empty")

pyrit/chat_message_normalizer/chat_message_normalizer_chatml.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ def from_chatml(content: str) -> list[ChatMessage]:
4040
4141
Returns:
4242
list[ChatMessage]: The list of chat messages.
43+
44+
Raises:
45+
ValueError: If the input content is invalid.
4346
"""
4447
messages: list[ChatMessage] = []
4548
matches = list(re.finditer(r"<\|im_start\|>(.*?)<\|im_end\|>", content, re.DOTALL | re.MULTILINE))

pyrit/chat_message_normalizer/chat_message_normalizer_tokenizer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99

1010
class ChatMessageNormalizerTokenizerTemplate(ChatMessageNormalizer[str]):
1111
"""
12-
This class enables you to apply the chat template stored in a Hugging Face tokenizer
12+
Enable application of the chat template stored in a Hugging Face tokenizer
1313
to a list of chat messages. For more details, see
1414
https://huggingface.co/docs/transformers/main/en/chat_templating.
1515
"""
1616

1717
def __init__(self, tokenizer: PreTrainedTokenizerBase):
1818
"""
19-
Initializes an instance of the ChatMessageNormalizerTokenizerTemplate class.
19+
Initialize an instance of the ChatMessageNormalizerTokenizerTemplate class.
2020
2121
Args:
2222
tokenizer (PreTrainedTokenizerBase): A Hugging Face tokenizer.
@@ -25,7 +25,7 @@ def __init__(self, tokenizer: PreTrainedTokenizerBase):
2525

2626
def normalize(self, messages: list[ChatMessage]) -> str:
2727
"""
28-
Applies the chat template stored in the tokenizer to a list of chat messages.
28+
Apply the chat template stored in the tokenizer to a list of chat messages.
2929
3030
Args:
3131
messages (list[ChatMessage]): A list of ChatMessage objects.

pyrit/chat_message_normalizer/generic_system_squash.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66

77

88
class GenericSystemSquash(ChatMessageNormalizer[list[ChatMessage]]):
9+
"""
10+
Normalizer that combines the first system message with the first user message using generic instruction tags.
11+
"""
12+
913
def normalize(self, messages: list[ChatMessage]) -> list[ChatMessage]:
1014
"""
11-
Returns the first system message combined with the first user message.
15+
Return the first system message combined with the first user message.
1216
1317
The format of the result uses generic instruction tags.
1418
@@ -28,7 +32,7 @@ def combine_system_user_message(
2832
system_message: ChatMessage, user_message: ChatMessage, msg_type: ChatMessageRole = "user"
2933
) -> ChatMessage:
3034
"""
31-
Combines the system message with the user message.
35+
Combine the system message with the user message.
3236
3337
Args:
3438
system_message (str): The system message.

pyrit/cli/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
# Licensed under the MIT License.
33

44
"""
5-
This module provides command-line interface functionalities for PyRIT.
5+
Module to provide command-line interface functionalities for PyRIT.
66
The CLI module is currently experimental.
77
"""

pyrit/cli/frontend_core.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@
3030

3131
# Create a dummy termcolor module for fallback
3232
class termcolor: # type: ignore
33+
"""Dummy termcolor fallback for colored printing if termcolor is not installed."""
34+
3335
@staticmethod
3436
def cprint(text: str, color: str = None, attrs: list = None) -> None: # type: ignore
37+
"""Print text without color."""
3538
print(text)
3639

3740

@@ -471,7 +474,7 @@ def validate_integer(value: str, *, name: str = "value", min_value: Optional[int
471474

472475
def _argparse_validator(validator_func: Callable[..., Any]) -> Callable[[Any], Any]:
473476
"""
474-
Decorator to convert ValueError to argparse.ArgumentTypeError.
477+
Adapt a validator to argparse by converting ValueError to ArgumentTypeError.
475478
476479
This decorator adapts our keyword-only validators for use with argparse's type= parameter.
477480
It handles two challenges:
@@ -498,6 +501,9 @@ def _argparse_validator(validator_func: Callable[..., Any]) -> Callable[[Any], A
498501
- Accepts a single positional argument (for argparse compatibility)
499502
- Calls validator_func with the correct keyword argument
500503
- Raises ArgumentTypeError instead of ValueError
504+
505+
Raises:
506+
ValueError: If validator_func has no parameters.
501507
"""
502508
import inspect
503509

pyrit/cli/pyrit_scan.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515

1616

1717
def parse_args(args=None) -> Namespace:
18-
"""Parse command-line arguments for the PyRIT scanner."""
18+
"""
19+
Parse command-line arguments for the PyRIT scanner.
20+
21+
Returns:
22+
Namespace: Parsed command-line arguments.
23+
"""
1924
parser = ArgumentParser(
2025
prog="pyrit_scan",
2126
description="""PyRIT Scanner - Run security scenarios against AI systems
@@ -120,7 +125,12 @@ def parse_args(args=None) -> Namespace:
120125

121126

122127
def main(args=None) -> int:
123-
"""Main entry point for the PyRIT scanner CLI."""
128+
"""
129+
Start the PyRIT scanner CLI.
130+
131+
Returns:
132+
int: Exit code (0 for success, 1 for error).
133+
"""
124134
print("Starting PyRIT...")
125135
sys.stdout.flush()
126136

0 commit comments

Comments
 (0)