REFACTOR: Promote exception status codes & messages to ClassVar defaults - #1966
Closed
Roman Lutz (romanlutz) wants to merge 1 commit into
Closed
REFACTOR: Promote exception status codes & messages to ClassVar defaults#1966Roman Lutz (romanlutz) wants to merge 1 commit into
Roman Lutz (romanlutz) wants to merge 1 commit into
Conversation
Move hard-coded `status_code` / `message` defaults out of the `__init__` signatures of `PyritException` and its subclasses in `pyrit/exceptions/exception_classes.py` and onto each class as `DEFAULT_STATUS_CODE: ClassVar[int]` / `DEFAULT_MESSAGE: ClassVar[str]`. Use the sentinel-default pattern (`None` -> `self.DEFAULT_*`) so subclass overrides resolve through `self` while keeping the keyword-only signatures unchanged for all existing callers. No value changes. No behaviour changes. Verified: ruff / pytest tests/unit/exceptions/. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Richard Lundeen (rlundeen2)
approved these changes
Jun 9, 2026
Contributor
Author
|
Closing per maintainer feedback on the constants-audit work. This PR introduced the sentinel-default pattern (changing Branch left intact in case any portion is worth cherry-picking later. |
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Promote hard-coded
status_code/messagedefaults out of the__init__signatures ofPyritExceptionand its subclasses inpyrit/exceptions/exception_classes.pyand onto each class asDEFAULT_STATUS_CODE: ClassVar[int]/DEFAULT_MESSAGE: ClassVar[str],in line with the class-level constants rule in the style guide and the
pattern established by #1964 and #1965.
Changes
For each of the following exception classes, the hard-coded
status_code/messagedefaults in the__init__signature arereplaced with per-class
DEFAULT_*ClassVarconstants resolvedthrough the sentinel-default pattern (
None→self.DEFAULT_*):DEFAULT_STATUS_CODEDEFAULT_MESSAGEPyritException500"An error occurred"BadRequestException400"Bad Request"RateLimitException429"Rate Limit Exception"ServerErrorException500"Server Error"EmptyResponseException204"No Content"InvalidJsonException"Invalid JSON Response"MissingPromptPlaceholderException"No prompt placeholder"InvalidJsonExceptionandMissingPromptPlaceholderExceptiononlyparameterize
message, so onlyDEFAULT_MESSAGEis defined forthem — they inherit
DEFAULT_STATUS_CODE(500) fromPyritException.The keyword-only signature (
*,) is preserved for every class. Allexisting call sites already pass these arguments by name (verified by
grep across
pyrit/,tests/,doc/), so this is signature-compatible.Verification
Spot-checked dependent tests that import these exceptions:
tests/unit/score/test_insecure_code_scorer.py,tests/unit/prompt_converter/test_generic_llm_converter.py,tests/unit/executor/attack/multi_turn/test_tree_of_attacks.py,tests/unit/prompt_normalizer/test_prompt_normalizer.py— 146 passed.Notes
REFACTOR: Promote single-class module constants to ClassVar (prompt_converter) #1965 (prompt_converter).