Skip to content

[MAINT]: Enable ruff preview and address various linting issues#55

Draft
spencrr wants to merge 13 commits into
microsoft:mainfrom
spencrr:spencrr/ruff-enable-preview
Draft

[MAINT]: Enable ruff preview and address various linting issues#55
spencrr wants to merge 13 commits into
microsoft:mainfrom
spencrr:spencrr/ruff-enable-preview

Conversation

@spencrr
Copy link
Copy Markdown
Contributor

@spencrr spencrr commented May 20, 2026

Description

Intent is to align with PyRIT (and enable ruff preview rules).

Enables preview = true for ruff and burns down the global ignore list it surfaced. After this PR, the project lints clean under preview with no broad rule suppressions - only DOC502 (intentional, with rationale) remains.

  • Restructured rampart/attacks/, rampart/probes/, and rampart/payloads/ __init__.py modules into thin re-export shims, moving class bodies (Attacks, Probes, Payloads) into dedicated private modules.
  • Documented return values and raised exceptions on public APIs across drivers, surfaces, payload store, pyrit bridge, evaluators, core types, and the pytest plugin.
  • Converted private helpers with no self use to @staticmethod; added an @override-annotated handler in the pytest plugin with a conditional typing_extensions fallback for Python 3.11.
  • Minor cleanups surfaced by preview rules: dropped async from sync test callbacks, switched manual file reads to Path.read_text(...), removed unused async in helpers, modernized pytest.approx usage, and scoped test-only rule carve-outs in [tool.ruff.lint.per-file-ignores].

Breaking changes

None.

Checklist

  • pre-commit run --all-files passes
  • Tests added or updated for changes - existing unit tests cover the refactored modules; test helpers updated alongside the source they exercise
  • Documentation updated

spencrr added 13 commits May 19, 2026 20:45
- Replace bare float == comparisons in tests with pytest.approx().
- Suppress pyright reportUnknownMemberType on the new approx calls
  (matches existing convention used elsewhere in the suite).
- Remove RUF069 from global ruff ignore list.
The five async helpers flagged by RUF029 are sync side_effect callbacks
passed to AsyncMock-backed targets. AsyncMock accepts sync side_effects
and awaits the return value, so dropping 'async' from the helpers is
semantically equivalent.

- Drop async from capture_eval / capture helpers in tests.
- Remove RUF029 from global ruff ignore list.
- Replace open()/read() with Path.read_text() in the one test helper.
- Remove FURB101 from global ruff ignore list.
All 10 violations were in tests, where 'x == ""' asserts an explicit
empty-string contract \u2014 rewriting to 'not x' would silently accept
None / 0 / [] as well.

- Move PLC1901 from global ignore to per-file-ignores[tests/**] with a
  comment explaining the intent.
- Production code remains under PLC1901 enforcement.
Extract logic out of the three package __init__.py files so they
contain only docstrings and re-exports:

- rampart/attacks/__init__.py -> Attacks class moved to
  rampart/attacks/_factory.py.
- rampart/probes/__init__.py -> Probes class moved to
  rampart/probes/_factory.py.
- rampart/payloads/__init__.py -> Payloads class plus the
  _apply_converters_async and _build_text_payload helpers moved
  to rampart/payloads/_facade.py (kept separate from _generator.py
  so the lower-level generator stays focused on LLM I/O).

Public import paths are unchanged (still re-exported via __init__).

- Remove RUF067 from global ruff ignore list.
DOC502 forbids documenting exceptions that propagate from a delegated
callee. That conflicts with Google-style / Sphinx / numpydoc convention,
which deliberately documents such exceptions because they are part of
the caller-visible contract.

All 4 current sites (KeyError from str.format_map, ValueError from a
_validate helper, pytest.UsageError from trial-marker helpers) are
legitimate contract exceptions worth documenting. The rule's
'fixes' (delete the Raises: section, or wrap with try/except + re-raise)
would degrade the docs or add boilerplate that hides the original error.

- Move DOC502 from the TODO ignore list into a separately documented
  'intentionally disabled' extend-ignore with rationale.
Ruff format collapses 'raise X(\n    msg,\n)' to a single line. These
changes were produced by the formatter; no behavioral changes.
Document the Raises: contract for the 10 production functions that
raise exceptions but had no Raises: section. Also add DOC501 to the
tests/** per-file-ignores: test fixture executions (e.g. one-line
_execute_async that just 'raise InfrastructureError(...)') are
self-documenting and don't need contract docstrings.

Files touched (Raises: sections added):
- rampart/core/types.py (Payload.__post_init__, Request.__post_init__,
  EvalContext.current_turn)
- rampart/drivers/llm.py (_ensure_initialized,
  _assert_conversations_consistent, _send_async)
- rampart/payloads/_store.py (_validate_collection_name)
- rampart/pyrit_bridge/llm_bridge.py (_validate)
- rampart/pytest_plugin/plugin.py (_create_trial_clones)
- rampart/surfaces/onedrive.py (InjectionHandle.__aenter__)

- Remove DOC501 from global ruff ignore list.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant