Skip to content

Tighten execution_context typing from Any to proper forward reference #195

Description

@mlieberman85

Context

#189 added execution_context fields to two dataclasses with loose typing:

packages/darnit/src/darnit/sieve/handler_registry.py:104:

execution_context: Any | None = None

packages/darnit/src/darnit/sieve/models.py:55:

execution_context: Optional["ExecutionContext"] = None  # already uses forward ref

The CheckContext side uses a proper "ExecutionContext" forward reference (wrapped in TYPE_CHECKING), but HandlerContext uses Any | None. This means custom handler authors lose type-check assistance when calling handler_ctx.execution_context.get_or_run_tool(...).

Proposal

Change HandlerContext.execution_context to use a proper forward reference matching CheckContext:

if TYPE_CHECKING:
    from darnit.core.models import ExecutionContext

@dataclass
class HandlerContext:
    ...
    execution_context: "ExecutionContext | None" = None

Why the loose typing was chosen

Likely to avoid a circular import between sieve/handler_registry.py and core/models.py. TYPE_CHECKING + string annotation is the standard workaround and is already used in sieve/models.py.

Acceptance criteria

  • HandlerContext.execution_context type is tightened to ExecutionContext | None
  • No runtime circular import introduced
  • mypy/pyright can infer .get_or_run_tool() on the attribute
  • Existing tests still pass

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions