FIX: remove type suppression comments from MessagePiece.to_message() via TYPE_CHECKING#1621
FIX: remove type suppression comments from MessagePiece.to_message() via TYPE_CHECKING#1621
MessagePiece.to_message() via TYPE_CHECKING#1621Conversation
…using TYPE_CHECKING pattern Agent-Logs-Url: https://github.com/microsoft/PyRIT/sessions/5a4d10d1-e063-41c1-aea3-fba586dacc17 Co-authored-by: spencrr <23708360+spencrr@users.noreply.github.com>
MessagePiece.to_message() via TYPE_CHECKING
|
Does linting pass? |
Linting: Yes, linting passes. The style guide explicitly allows local imports inside functions as the documented exception for circular dependency resolution: "The only exception is breaking circular import dependencies, which should be rare and documented." Ruff's Does the local import inside the function make sense? Yes — this is the canonical two-layer pattern for
This is exactly the same pattern already used for Is this the best solution? Yes. The alternatives are worse:
The PR description has been updated to follow the repo template (commit |
MessagePiece.to_message() via TYPE_CHECKINGMessagePiece.to_message() via TYPE_CHECKING
Description
Removes three type-suppression comments (
# type: ignore[name-defined],# noqa: F821) fromMessagePiece.to_message()inpyrit/models/message_piece.pyby using theTYPE_CHECKINGguard that already exists in that file.The
Scoreimport two lines below already uses this exact pattern — this change makesMessageconsistent with it.What changed:
from pyrit.models.message import Messageto the existingTYPE_CHECKINGblock (alongsideScore). This makesMessagevisible to mypy/pyright/IDEs with no runtime cost and no circular import.to_message()return type annotation and return statement. Thefrom __future__ import annotations(line 4) enables PEP 563 postponed evaluation, so the bare nameMessagein the signature is a valid forward reference resolved by theTYPE_CHECKINGimport.from pyrit.models.message import Messageinside the method body. This is the necessary exception to the "no inline imports" rule documented in the style guide: it resolves the circular import at runtime sinceMessageis not imported at module level.Tests and Documentation
No behavioral change — purely a type annotation / suppression-comment cleanup. No new tests or documentation required.
JupyText not applicable (no notebook changes).