Skip to content
Open
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
8 changes: 0 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,6 @@ include = [
"pyrit/backend/services/finding_export.py",
"pyrit/backend/services/prompt_generation_service.py",
"pyrit/backend/services/scenario_service.py",
"pyrit/cli/api_client.py",
"pyrit/cli/pyrit_scan.py",
"pyrit/common/cli_helpers.py",
"pyrit/common/yaml_loadable.py",
"pyrit/converter/decomposition_converter.py",
"pyrit/embedding/openai_text_embedding.py",
"pyrit/executor/attack/multi_turn/tree_of_attacks.py",
Expand All @@ -341,10 +337,6 @@ include = [
"pyrit/executor/promptgen/gcg/attack/gcg/candidate_proposer.py",
"pyrit/executor/promptgen/gcg/attack/gcg/gcg_attack.py",
"pyrit/executor/promptgen/gcg/default_implementations.py",
"pyrit/memory/alembic/env.py",
"pyrit/memory/attack_analytics.py",
"pyrit/memory/azure_sql_memory.py",
"pyrit/memory/memory_interface.py",
"pyrit/models/harm_category.py",
"pyrit/models/messages/conversations.py",
"pyrit/models/parameter.py",
Expand Down
9 changes: 5 additions & 4 deletions pyrit/cli/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,10 +735,11 @@ async def _get_catalog_items_async(self, *, path: str, limit: int | None) -> lis
pagination = payload.get("pagination", {})
if not pagination.get("has_more"):
return items
cursor = pagination.get("next_cursor")
if not isinstance(cursor, str) or not cursor or cursor in seen_cursors:
next_cursor = pagination.get("next_cursor")
if not isinstance(next_cursor, str) or not next_cursor or next_cursor in seen_cursors:
raise ValueError(f"Catalog response from {path} has a missing or repeated next-page cursor.")
seen_cursors.add(cursor)
seen_cursors.add(next_cursor)
cursor = next_cursor

async def _get_json_async(self, *, path: str, params: dict[str, Any] | None = None) -> dict[str, Any]:
"""
Expand Down Expand Up @@ -788,7 +789,7 @@ def _response_detail(resp: Any) -> str | None:
if detail_value is not None:
return str(detail_value)

text = getattr(resp, "text", "")
text: object = getattr(resp, "text", "")
if isinstance(text, bytes):
text = text.decode(errors="replace")
if isinstance(text, str):
Expand Down
6 changes: 4 additions & 2 deletions pyrit/cli/pyrit_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from collections.abc import Callable

from pyrit.cli._target_credentials import CredentialSource
from pyrit.cli.api_client import PyRITApiClient
from pyrit.models import Parameter
from pyrit.models.catalog import (
RunScenarioRequest,
Expand Down Expand Up @@ -1041,7 +1042,8 @@ def _reparse_with_scenario_params(*, parsed_args: Namespace, supported_params: l
Returns:
Namespace | None: The re-parsed Namespace, or ``None`` on argparse ``SystemExit``.
"""
translated: list[str] = getattr(parsed_args, "_translated_args", [])
# _parse_args_with_scenario_params stores the translated argv as a list[str].
translated = cast("list[str]", getattr(parsed_args, "_translated_args", []))

if not supported_params:
unknown = getattr(parsed_args, "_unknown_args", None)
Expand Down Expand Up @@ -1116,7 +1118,7 @@ def _build_run_request(*, parsed_args: Namespace, scenario_name: str) -> RunScen

async def _poll_until_terminal_async(
*,
client: Any,
client: PyRITApiClient,
scenario_result_id: str,
) -> ScenarioRunSummary:
"""
Expand Down
3 changes: 1 addition & 2 deletions pyrit/common/cli_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ def validate_log_level(*, log_level: str) -> int:
level_upper = log_level.upper()
if level_upper not in valid_levels:
raise ValueError(f"Invalid log level: {log_level}. Must be one of: {', '.join(valid_levels)}")
level_value: int = getattr(logging, level_upper)
return level_value
return logging.getLevelNamesMapping()[level_upper]


def validate_log_level_argparse(value: Any) -> int:
Expand Down
8 changes: 6 additions & 2 deletions pyrit/common/yaml_loadable.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def from_yaml_file(cls, file: Path | str) -> Self:
Raises:
FileNotFoundError: If the input YAML file path does not exist.
ValueError: If the YAML file is invalid.
TypeError: If the class's ``from_dict`` returns an object of another type.
"""
file = verify_and_resolve_path(file)
yaml_data = load_yaml_file(path=file)
Expand All @@ -37,7 +38,10 @@ def from_yaml_file(cls, file: Path | str) -> Self:

# If this class provides a from_dict factory, use it;
# otherwise, just instantiate directly with **yaml_data
if hasattr(cls, "from_dict") and callable(getattr(cls, "from_dict")): # noqa: B009
result: Self = cls.from_dict(yaml_data) # type: ignore[ty:call-non-callable]
from_dict = getattr(cls, "from_dict", None)
if callable(from_dict):
result = from_dict(yaml_data)
if not isinstance(result, cls):
raise TypeError(f"{cls.__name__}.from_dict returned {type(result).__name__}, not {cls.__name__}")
return result
return cls(**yaml_data)
8 changes: 4 additions & 4 deletions pyrit/memory/alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
from pyrit.memory.migration import PYRIT_MEMORY_ALEMBIC_VERSION_TABLE

config = context.config
connection: Connection | None = config.attributes.get("connection")
configured_connection = config.attributes.get("connection")
if not isinstance(configured_connection, Connection):
raise RuntimeError("No connection found for Alembic migration")
connection: Connection = configured_connection
target_metadata = Base.metadata
applied_revisions: set[str] = set()

if connection is None:
raise RuntimeError("No connection found for Alembic migration")


def _include_object(
obj: SchemaItem, name: str | None, type_: str, reflected: bool, compare_to: SchemaItem | None
Expand Down
5 changes: 3 additions & 2 deletions pyrit/memory/attack_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,9 +588,10 @@ def before_cursor_execute(
operation=lambda: connection.exec_driver_sql("BEGIN"), control=control
)
elif dialect == "mssql":
old_timeout = getattr(driver, "timeout", None)
if not isinstance(old_timeout, int):
timeout = getattr(driver, "timeout", None)
if not isinstance(timeout, int):
raise NotImplementedError("Azure SQL analytics requires an ODBC query-timeout property")
old_timeout = timeout
await self._odbc_timeout_async(driver=driver, timeout=max(1, math.ceil(control.remaining)))
else:
raise NotImplementedError(f"Attack analytics does not support {dialect!r}")
Expand Down
7 changes: 3 additions & 4 deletions pyrit/memory/azure_sql_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ async def connect_async() -> aioodbc.Connection:
token_bytes = token.token.encode("utf-16-le")
attrs = dict(params.pop("attrs_before", {}))
attrs[self.SQL_COPT_SS_ACCESS_TOKEN] = struct.pack(f"<I{len(token_bytes)}s", len(token_bytes), token_bytes)
connection: aioodbc.Connection = await aioodbc.connect(dsn=dsn, attrs_before=attrs, **params)
return connection
# aioodbc ships no type information; connect() returns an aioodbc.Connection.
return cast("aioodbc.Connection", await aioodbc.connect(dsn=dsn, attrs_before=attrs, **params))

engine = create_async_engine(
url,
Expand Down Expand Up @@ -245,8 +245,7 @@ def _resolve_sas_token(env_var_name: str, passed_value: str | None = None) -> st
# get_required_value() is typed to return Any because it can also preserve callables
# (e.g. token providers). This call site only ever passes a str | None, so the
# runtime value is guaranteed to be a str.
resolved_value: str = value
return resolved_value
return cast("str", value)
except ValueError:
return None

Expand Down
10 changes: 7 additions & 3 deletions pyrit/memory/memory_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -6762,8 +6762,8 @@ def _execute_get_seed_dataset_names(self) -> Sequence[str]:
.where(SeedEntry.dataset_name.isnot(None), SeedEntry.dataset_name != "")
.distinct()
)
names: Sequence[str | None] = session.scalars(statement).all()
return [name for name in names if name is not None]
# SeedEntry.dataset_name has no Mapped annotation, so the rows are typed Any.
return [name for name in session.scalars(statement).all() if isinstance(name, str)]
except SQLAlchemyError as e:
logger.exception(f"Failed to retrieve dataset names with error {e}")
raise
Expand Down Expand Up @@ -8408,7 +8408,11 @@ def _execute_increment_scenario_number_tries(
)
if not updated_rows:
raise ValueError(f"Scenario result with ID {scenario_result_id} not found in memory")
new_tries: int = session.query(ScenarioResultEntry.number_tries).filter_by(id=scenario_result_id).scalar()
new_tries = session.scalar(
select(ScenarioResultEntry.number_tries).where(ScenarioResultEntry.id == scenario_result_id)
)
if new_tries is None:
raise ValueError(f"Scenario result with ID {scenario_result_id} not found in memory")
session.commit()
return new_tries

Expand Down
10 changes: 10 additions & 0 deletions tests/unit/common/test_yaml_loadable.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ def test_from_yaml_file_uses_from_dict_if_available(yaml_file_for_from_dict: Pat
assert obj.name == "LOWER"


def test_from_yaml_file_rejects_from_dict_returning_another_type(yaml_file_for_from_dict: Path):
class _BadFromDict(YamlLoadable):
@classmethod
def from_dict(cls, data: dict) -> dict:
return data

with pytest.raises(TypeError, match="_BadFromDict.from_dict returned dict, not _BadFromDict"):
_BadFromDict.from_yaml_file(yaml_file_for_from_dict)


def test_from_yaml_file_nonexistent_raises():
with pytest.raises(FileNotFoundError):
_SimpleYaml.from_yaml_file(Path("nonexistent_file.yaml"))
Expand Down
Loading