refactor(types): add comprehensive type annotations across backend Python codebase
Enable ANN rules in ruff.toml (flake8-annotations) and resolve all 221 violations: ANN201/ANN202 — return types on 168 public/private functions: - All 28 FastAPI routers: endpoints annotated with dict/list/specific schema/ StreamingResponse/FileResponse/JSONResponse as appropriate - main.py: lifespan→AsyncGenerator[None,None], exception handlers→JSONResponse - database.py: get_db→Generator[Session,None,None], proxy methods→correct types - middleware/request_context.py: dispatch→Response with Callable call_next type ANN001/ANN002/ANN003 — 32 missing argument types: - seed_demo.py: all db parameters typed as Session - domain/unit_of_work.py: __aexit__ exc_type/exc_val/exc_tb typed with TracebackType - services: audit_service user_id→UUID|None, heatmap_service query/model/builder, notification_service test→Test, tempo_service test→Test/user→User, test_workflow_service test_id→UUID, campaign_crud **fields→object, test_crud **fields→object (4 sites) ANN401 — 16 Any usages resolved: - Domain entities (campaign/technique/threat_actor/test_entity): replaced Any with actual ORM types via TYPE_CHECKING guards to avoid circular imports - detection_rule_service: test_id/detection_rule_id/evaluator_id→UUID - score_cache: kept Any with # noqa: ANN401 (genuinely generic cache) - jira_service/tempo_service: kept Any with # noqa: ANN401 (lazy optional deps) - d3fend_import_service: _to_str(v: Any) kept with # noqa: ANN401 ANN204/ANN205/ANN206 — special/static/class methods: - database.py proxy __call__/__getattr__: *args: object/**kwargs: object - schemas/test.py model_validate: obj→object, **kwargs→object - sa_technique_repository._int_type→type All 439 unit tests pass. ruff check app/ → All checks passed!
This commit is contained in:
@@ -126,7 +126,7 @@ def list_tests(
|
||||
limit: int = Query(50, ge=1, le=200),
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
) -> list:
|
||||
"""Return a paginated list of tests, optionally filtered by state, technique, platform or creator."""
|
||||
return crud_list_tests(
|
||||
db,
|
||||
@@ -156,7 +156,7 @@ def create_test(
|
||||
payload: TestCreate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(require_any_role("red_lead", "blue_lead")),
|
||||
):
|
||||
) -> TestOut:
|
||||
"""Create a new test linked to an existing technique.
|
||||
|
||||
``created_by`` is set automatically and ``state`` defaults to *draft*.
|
||||
@@ -198,7 +198,7 @@ def create_test_from_template(
|
||||
payload: TestTemplateInstantiate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(require_any_role("red_lead", "blue_lead")),
|
||||
):
|
||||
) -> TestOut:
|
||||
"""Instantiate a real Test from an existing TestTemplate.
|
||||
|
||||
The template's fields are copied into the new test as starting data.
|
||||
@@ -238,7 +238,7 @@ def get_test(
|
||||
test_id: uuid.UUID,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
) -> TestOut:
|
||||
"""Return full details for a single test, including its evidences."""
|
||||
return crud_get_test_detail(db, test_id)
|
||||
|
||||
@@ -254,7 +254,7 @@ def update_test(
|
||||
payload: TestUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(require_any_role("red_lead", "blue_lead")),
|
||||
):
|
||||
) -> TestOut:
|
||||
"""Update one or more fields of an existing test.
|
||||
|
||||
Only leads or admins can update general test fields.
|
||||
@@ -294,7 +294,7 @@ def update_test_classification(
|
||||
payload: TestClassificationUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(require_role("admin")),
|
||||
):
|
||||
) -> TestOut:
|
||||
"""Update the data classification label for a test (admin only)."""
|
||||
with UnitOfWork(db) as uow:
|
||||
test = crud_get_test_or_raise(db, test_id)
|
||||
@@ -324,7 +324,7 @@ def update_test_red(
|
||||
payload: TestRedUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(require_any_role("red_tech", "red_lead")),
|
||||
):
|
||||
) -> TestOut:
|
||||
"""Red Team updates their fields (allowed in ``draft`` and ``red_executing``)."""
|
||||
update_data = payload.model_dump(exclude_unset=True)
|
||||
with UnitOfWork(db) as uow:
|
||||
@@ -354,7 +354,7 @@ def update_test_blue(
|
||||
payload: TestBlueUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(require_any_role("blue_tech", "blue_lead")),
|
||||
):
|
||||
) -> TestOut:
|
||||
"""Blue Team updates their fields (allowed only in ``blue_evaluating``)."""
|
||||
update_data = payload.model_dump(exclude_unset=True)
|
||||
with UnitOfWork(db) as uow:
|
||||
@@ -383,7 +383,7 @@ def start_execution(
|
||||
test_id: uuid.UUID,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(require_any_role("red_tech", "red_lead")),
|
||||
):
|
||||
) -> TestOut:
|
||||
"""Move a test from ``draft`` to ``red_executing``."""
|
||||
test = crud_get_test_or_raise(db, test_id)
|
||||
with UnitOfWork(db) as uow:
|
||||
@@ -403,7 +403,7 @@ def submit_red(
|
||||
test_id: uuid.UUID,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(require_any_role("red_tech", "red_lead")),
|
||||
):
|
||||
) -> TestOut:
|
||||
"""Red Team finalises — move from ``red_executing`` to ``blue_evaluating``."""
|
||||
test = crud_get_test_or_raise(db, test_id)
|
||||
with UnitOfWork(db) as uow:
|
||||
@@ -423,7 +423,7 @@ def submit_blue(
|
||||
test_id: uuid.UUID,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(require_any_role("blue_tech", "blue_lead")),
|
||||
):
|
||||
) -> TestOut:
|
||||
"""Blue Team finalises — move from ``blue_evaluating`` to ``in_review``."""
|
||||
test = crud_get_test_or_raise(db, test_id)
|
||||
with UnitOfWork(db) as uow:
|
||||
@@ -443,7 +443,7 @@ def pause_timer(
|
||||
test_id: uuid.UUID,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(require_any_role("red_tech", "blue_tech", "red_lead", "blue_lead")),
|
||||
):
|
||||
) -> TestOut:
|
||||
"""Pause the running timer for the current phase (red_executing or blue_evaluating)."""
|
||||
test = crud_get_test_or_raise(db, test_id)
|
||||
with UnitOfWork(db) as uow:
|
||||
@@ -463,7 +463,7 @@ def resume_timer(
|
||||
test_id: uuid.UUID,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(require_any_role("red_tech", "blue_tech", "red_lead", "blue_lead")),
|
||||
):
|
||||
) -> TestOut:
|
||||
"""Resume the paused timer for the current phase."""
|
||||
test = crud_get_test_or_raise(db, test_id)
|
||||
with UnitOfWork(db) as uow:
|
||||
@@ -484,7 +484,7 @@ def validate_red(
|
||||
payload: TestRedValidate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(require_any_role("red_lead")),
|
||||
):
|
||||
) -> TestOut:
|
||||
"""Red Lead approves or rejects the red side of a test."""
|
||||
test = crud_get_test_with_technique(db, test_id)
|
||||
with UnitOfWork(db) as uow:
|
||||
@@ -511,7 +511,7 @@ def validate_blue(
|
||||
payload: TestBlueValidate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(require_any_role("blue_lead")),
|
||||
):
|
||||
) -> TestOut:
|
||||
"""Blue Lead approves or rejects the blue side of a test."""
|
||||
test = crud_get_test_with_technique(db, test_id)
|
||||
with UnitOfWork(db) as uow:
|
||||
@@ -537,7 +537,7 @@ def reopen(
|
||||
test_id: uuid.UUID,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(require_any_role("red_lead", "blue_lead")),
|
||||
):
|
||||
) -> TestOut:
|
||||
"""Reopen a rejected test, moving it back to ``draft``."""
|
||||
test = crud_get_test_or_raise(db, test_id)
|
||||
with UnitOfWork(db) as uow:
|
||||
@@ -558,7 +558,7 @@ def update_remediation(
|
||||
payload: TestRemediationUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(require_any_role("red_lead", "blue_lead")),
|
||||
):
|
||||
) -> TestOut:
|
||||
"""Update remediation fields on a test.
|
||||
|
||||
When ``remediation_status`` transitions to ``'completed'``, an automatic
|
||||
@@ -602,7 +602,7 @@ def get_test_timeline(
|
||||
test_id: uuid.UUID,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
) -> list:
|
||||
"""Return the chronological audit-log history for a test."""
|
||||
return crud_get_test_timeline(db, test_id)
|
||||
|
||||
@@ -617,7 +617,7 @@ def get_retest_chain(
|
||||
test_id: uuid.UUID,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
) -> list:
|
||||
"""Return the full chain of retests (original + all retests) for a test."""
|
||||
chain = wf_get_retest_chain(db, test_id)
|
||||
if not chain:
|
||||
|
||||
Reference in New Issue
Block a user