Skip to content

LoD Violation: Deep Dictionary Access in Tests #122

@coderabbitai

Description

@coderabbitai

Overview

This is a sub-task of #119 - Refactor: Address Law of Demeter violations for better encapsulation

Priority: Medium
Pattern: result["metadata"]["settings"]["key"] (3+ levels deep)

Violations Found

20+ instances across test files:

  • tests/test_schema_parser.py: Lines 362, 368, 371, 374, 388, 396
  • tests/unit/safety_system/test_safety.py: Lines 141-142, 453, 460
  • tests/unit/safety_system/test_filesystem_path_sanitization.py: Lines 152-153
  • tests/unit/fuzz_engine/strategy/test_schema_parser.py: Lines 361, 367
  • tests/integration/test_reports_integration.py: Lines 107, 163-166, 196-198

Refactoring Suggestion

def safe_get_nested(data: dict, *keys, default=None):
    result = data
    for key in keys:
        if not isinstance(result, dict):
            return default
        result = result.get(key, {})
    return result if result != {} else default

Acceptance Criteria

  • Choose refactoring approach (wrapper class vs helper function)
  • Implement chosen approach
  • Refactor all 20+ test assertions
  • Add tests for new helper/wrapper
  • All existing tests pass

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions