Skip to content

refactor: codebase modernization, quality tooling, and API correctness fixes#56

Merged
LegendEvent merged 18 commits intodevfrom
cherry-picked-desloppify
Apr 2, 2026
Merged

refactor: codebase modernization, quality tooling, and API correctness fixes#56
LegendEvent merged 18 commits intodevfrom
cherry-picked-desloppify

Conversation

@LegendEvent
Copy link
Copy Markdown
Owner

Summary

This PR is a comprehensive housekeeping pass across the entire SDK — cleaning up code quality, hardening error handling, adding developer tooling, and fixing a handful of real bugs found along the way. The changes touch every endpoint module, but the public API surface is fully backwards-compatible.

What changed

🔧 Code quality & consistency

  • Applied consistent PEP 8 formatting and ruff linting across all 25+ endpoint modules
  • Added proper type hints throughout — _UNSET sentinel replaced with a properly typed class
  • Added __all__ exports to every module for clean from darktrace.dt_antigena import * behavior
  • Added __repr__ to core classes for better debugging experience

🐛 Bug fixes

  • dt_antigena.py: Restored activate_action() — a real API method that was accidentally removed. It POSTs {"codeid": X, "activate": true} to /antigena per the official Darktrace API spec
  • dt_antigena.py: Fixed approve_action() deprecation message — it was pointing users to the non-existent activate_action(). Now correctly states it's a no-op for backwards compatibility
  • dt_advanced_search.py: Restored missing time parameter default in GET path
  • dt_similardevices.py: Fixed silent error swallowing (bare except Exception → specific exception types)
  • dt_breaches.py: Fixed bare except Exception handlers in add_comment, acknowledge, unacknowledge

🏗️ Internal refactoring

  • Modernized dt_utils.py with structured logging, _get/_post_form/_post_json helper methods, and __repr__
  • Refactored dt_analyst.py and dt_breaches.py to use the new BaseEndpoint helpers
  • All refactoring is internal — public method signatures are unchanged

🧪 Tests

  • Expanded test coverage in test_sdk_readonly.py and test_mock.py
  • Restored TestDeprecations class to verify approve_action() emits the correct warning
  • 81/81 mock tests passing

⚙️ Developer tooling

  • Added ruff linter/formatter config (pyproject.toml)
  • Added pre-commit hooks (ruff, trailing whitespace, end-of-file)
  • Added GitHub Actions CI lint workflow
  • Added GitNexus code intelligence integration

Files changed: 48 · +2,338 / -1,334

Testing

  • pytest tests/test_mock.py81 passed
  • pytest tests/test_compilation.py — all modules compile cleanly
  • python3 -m py_compile — all modified files pass syntax check
  • ⏭️ pytest tests/test_sdk_readonly.py — requires authenticated Darktrace instance (51 passed, 10 skipped, 0 failed in last run)

Backwards compatibility

No breaking changes. All existing method signatures, return types, and behavior are preserved. The approve_action() backwards-compat shim is retained with a corrected deprecation notice.

LegendEvent and others added 18 commits March 31, 2026 13:07
Apply consistent PEP 8 style transformations to all remaining Python files, matching the pattern already applied to 11 files in a previous commit.

Changes are purely mechanical with zero logic modifications:
- Single to double quotes for all string literals
- PEP 8 import ordering (stdlib > third-party > local)
- Remove unused `import requests` from endpoint modules
- Trailing commas on multi-line function signatures
- Multi-line `_make_request` call formatting
- Remove trailing whitespace, ensure trailing newlines
- Type hint improvements (`int = None` to `Optional[int] = None`)

Files changed:
  darktrace/__init__.py, auth.py
  darktrace/dt_antigena.py, dt_breaches.py, dt_components.py, dt_cves.py,
  dt_details.py, dt_deviceinfo.py, dt_devicesearch.py, dt_email.py,
  dt_endpointdetails.py, dt_enums.py, dt_intelfeed.py, dt_mbcomments.py,
  dt_metrics.py, dt_models.py, dt_pcaps.py, dt_status.py,
  dt_summarystatistics.py
  examples/intelfeed_example.py, threat_intelligence.py

Verified: all 22 files pass py_compile, package imports OK (v0.9.0),
90 tests pass / 0 fail, scope fidelity confirmed (style-only changes).

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
- Add [tool.ruff] config: target-version py37, line-length 120
- Configure lint rules: E, F, W, I, UP (ignore E501 for docstrings)
- Configure format: double quotes, space indentation
- Configure isort: known-first-party darktrace
- Add [project.optional-dependencies] dev group: ruff, pytest, pre-commit
- Exclude examples/ from ruff checks
- Replace _UNSET = object() with _Unset singleton class in dt_utils.py
- _Unset has __repr__ ('<UNSET>'), __bool__ (False), singleton via __new__
- Add _InternalTimeoutType = Union[_Unset, None, float, Tuple[float, float]]
- Update _resolve_timeout to use _InternalTimeoutType (no type: ignore needed)
- Remove 24 _UNSET-related # type: ignore[assignment] across 19 modules
- Preserve all return-type # type: ignore[assignment] (unrelated to _UNSET)
- Fix missing Dict/Any imports in dt_subnets.py (F821 bug)
- Add # noqa: F401 to __version__ import in __init__.py
- Update test files for ruff formatting compatibility
- One-time ruff format pass on all darktrace/ and root Python files
- Import sorting (isort), consistent quote style, whitespace normalization
- No logic changes — formatting only
- conftest.py: add # noqa: E402 for post-function imports
- setup.py: quote style normalization, remove unused os import
- Add .pre-commit-config.yaml with ruff (--fix) and ruff-format hooks
- Add .github/workflows/lint.yml with parallel lint + test jobs
- Lint job: ruff check + ruff format --check (Python 3.12)
- Test job: pip install -e .[dev] + pytest
- Triggers on push/PR to main branch
3-phase architecture:
- Phase 1: Refresh GitNexus index with embeddings (skippable)
- Phase 2: Run 4 Cypher detection checks + allowlist filtering
- Phase 3: Dynamic deep investigation for actionable issues

Features:
- Allowlist for false positives (30 orphaned SDK classes)
- LLM-consumable output with rich context
- Dynamic investigation queries per issue type
- Sisyphus ulw mode trigger for complex issues
- Remove deprecated approve_action method from dt_antigena.py
- Remove corresponding test from test_mock.py
- Replace bare except Exception with specific exception types
  (requests.exceptions.RequestException, json.JSONDecodeError)
  in add_comment, acknowledge, unacknowledge methods

Code smells detected by GitNexus code intelligence hook.
Add real activate_action() method that POSTs to /antigena with activate:true.
Fix approve_action() deprecation message to no longer reference the
previously missing activate_action method.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)
Restore approve_action and activate_action assertions in test_antigena_methods.
Restore TestDeprecations class with test_approve_action_deprecated.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)
@LegendEvent LegendEvent merged commit aa0d6b7 into dev Apr 2, 2026
@LegendEvent LegendEvent deleted the cherry-picked-desloppify branch April 2, 2026 16:09
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