MAINT Enable ruff rules: DTZ, N, T10, TID, YTT#1414
MAINT Enable ruff rules: DTZ, N, T10, TID, YTT#1414romanlutz wants to merge 1 commit intoAzure:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Enables additional Ruff lint rule categories (DTZ, N, T10, TID, YTT) and updates the codebase accordingly, with a primary behavioral change being standardization on timezone-aware UTC datetimes and handling SQLite’s naive datetime round-tripping.
Changes:
- Standardize timestamp creation to
datetime.now(tz=timezone.utc)across models, memory, and printers (DTZ). - Rename variables/fixtures to PEP 8 naming and add targeted
noqafor intentional patterns (N). - Update unit/integration tests to use UTC-aware datetimes and to account for SQLite returning naive datetimes.
Reviewed changes
Copilot reviewed 35 out of 35 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/unit/models/test_score.py | Use UTC-aware timestamps in Score tests. |
| tests/unit/models/test_message_piece.py | Use UTC-aware timestamps throughout MessagePiece tests and reformat long constructor calls. |
| tests/unit/memory/test_sqlite_memory.py | Adjust timestamp comparisons for SQLite naive datetime behavior. |
| tests/unit/memory/test_azure_sql_memory.py | Adjust timestamp comparisons to normalize expected tzinfo. |
| tests/unit/memory/memory_interface/test_interface_prompts.py | Use UTC-aware datetimes for sent_before/sent_after filtering tests. |
| tests/unit/executor/attack/core/test_attack_parameters.py | Add noqa for intentional class-like local variable naming. |
| tests/unit/executor/attack/core/test_attack_executor.py | Add noqa for intentional class-like local variable naming. |
| tests/unit/converter/test_azure_speech_text_converter.py | Add noqa for mocked parameter naming. |
| tests/unit/converter/test_azure_speech_converter.py | Add noqa for mocked parameter naming and reformat signature. |
| tests/unit/backend/test_converter_service.py | Rename local “constant-like” variables to snake_case for N compliance. |
| tests/unit/auth/test_azure_storage_auth.py | Use UTC-aware timestamps in auth tests. |
| tests/integration/score/test_hitl_gradio_integration.py | Rename fixture to snake_case and update usages. |
| tests/integration/memory/test_azure_sql_memory_integration.py | Replace utcnow() usage with UTC-aware now(tz=...). |
| tests/integration/ai_recruiter/test_ai_recruiter.py | Rename local constant-like variable to snake_case. |
| pyrit/score/scorer_evaluation/scorer_evaluator.py | Rename evaluator map variable to snake_case. |
| pyrit/prompt_target/websocket_copilot_target.py | Rename local constant-like variable to snake_case. |
| pyrit/prompt_target/openai/openai_realtime_target.py | Rename local constant-like variable to snake_case. |
| pyrit/prompt_converter/morse_converter.py | Rename local constant-like variable to snake_case. |
| pyrit/prompt_converter/insert_punctuation_converter.py | Rename local constant-like variables to snake_case. |
| pyrit/prompt_converter/braille_converter.py | Rename local variables to snake_case. |
| pyrit/models/seeds/seed_dataset.py | Default date_added to UTC-aware datetime. |
| pyrit/models/seeds/seed.py | Default date_added to UTC-aware datetime in dataclass. |
| pyrit/models/score.py | Default Score timestamp to UTC-aware datetime. |
| pyrit/models/message_piece.py | Default MessagePiece timestamp to UTC-aware datetime. |
| pyrit/models/message.py | Use UTC-aware timestamp when duplicating messages. |
| pyrit/memory/memory_models.py | Add _ensure_utc helper and normalize datetimes returned from DB; use UTC-aware timestamps for result entries. |
| pyrit/memory/memory_interface.py | Use UTC-aware datetimes for seed insertion and export filename timestamping. |
| pyrit/executor/attack/printer/markdown_printer.py | Use UTC-aware datetime for report footer timestamp. |
| pyrit/executor/attack/printer/console_printer.py | Use UTC-aware datetime for report footer timestamp. |
| pyrit/exceptions/exception_classes.py | Add noqa for exception class naming convention. |
| pyrit/datasets/seed_datasets/local/local_dataset_loader.py | Add noqa for nested __init__ naming pattern. |
| pyrit/cli/pyrit_shell.py | Add noqa for do_EOF cmd alias naming. |
| pyrit/cli/frontend_core.py | Add noqa for dummy termcolor class and rename local path var to snake_case. |
| pyrit/auth/azure_storage_auth.py | Use UTC-aware datetimes for delegation key and SAS start/expiry times. |
| pyproject.toml | Enable Ruff rule sets DTZ, N, T10, TID, YTT and update per-path ignores. |
Comments suppressed due to low confidence (1)
pyrit/executor/attack/printer/console_printer.py:352
- The footer timestamp is now generated in UTC, but the printed value has no timezone indicator. Consider appending “UTC” (or formatting with
%z/ ISO-8601) so operators can interpret the time correctly.
timestamp = datetime.now(tz=timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
print()
self._print_colored("─" * self._width, Style.DIM, Fore.WHITE)
footer_text = f"Report generated at: {timestamp}"
self._print_colored(footer_text.center(self._width), Style.DIM, Fore.WHITE)
pyrit/memory/memory_models.py
Outdated
| def _ensure_utc(dt: datetime) -> datetime: | ||
| """ | ||
| Attach UTC tzinfo to a naive datetime (as returned by SQLite). | ||
|
|
||
| Args: | ||
| dt (datetime): The datetime to normalize. | ||
|
|
||
| Returns: | ||
| datetime: The datetime with UTC tzinfo attached if it was naive. |
There was a problem hiding this comment.
_ensure_utc is annotated as taking/returning datetime, but the implementation treats dt as possibly None (if dt is not None ... and return dt). This creates an inconsistent contract (and can confuse type-checkers); either make the parameter/return type Optional[datetime] or drop the None handling and assume a non-null datetime.
| def _ensure_utc(dt: datetime) -> datetime: | |
| """ | |
| Attach UTC tzinfo to a naive datetime (as returned by SQLite). | |
| Args: | |
| dt (datetime): The datetime to normalize. | |
| Returns: | |
| datetime: The datetime with UTC tzinfo attached if it was naive. | |
| def _ensure_utc(dt: Optional[datetime]) -> Optional[datetime]: | |
| """ | |
| Attach UTC tzinfo to a naive datetime (as returned by SQLite). | |
| Args: | |
| dt (Optional[datetime]): The datetime to normalize, or None. | |
| Returns: | |
| Optional[datetime]: The datetime with UTC tzinfo attached if it was naive, or None. |
| # Footer | ||
| markdown_lines.append("\n---") | ||
| markdown_lines.append(f"*Report generated at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}*") | ||
| markdown_lines.append(f"*Report generated at {datetime.now(tz=timezone.utc).strftime('%Y-%m-%d %H:%M:%S')}*") |
There was a problem hiding this comment.
The footer timestamp is now generated in UTC, but the rendered string doesn’t indicate the timezone. This can be misleading in reports; consider including an explicit “UTC” suffix or using an ISO-8601 format with offset/Z.
| markdown_lines.append(f"*Report generated at {datetime.now(tz=timezone.utc).strftime('%Y-%m-%d %H:%M:%S')}*") | |
| timestamp_utc = datetime.now(tz=timezone.utc).isoformat().replace("+00:00", "Z") | |
| markdown_lines.append(f"*Report generated at {timestamp_utc}*") |
Enable 5 additional ruff rule categories: - DTZ: datetime timezone awareness (use timezone.utc everywhere, add _ensure_utc helper in memory_models to re-attach UTC to naive datetimes returned by SQLite) - N: PEP 8 naming conventions (renamed camelCase variables to snake_case, noqa for intentional patterns like do_EOF, PyritException, Mock fixtures) - T10: debugger calls (0 violations) - TID: tidy imports (0 violations) - YTT: sys.version checks (0 violations) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
b9b64f4 to
61941a0
Compare
Enable 5 additional ruff rule categories: