Skip to content

[aw][code health] Two Pydantic fields suppress reportUnknownVariableType instead of using default_factory=lambda: [] #790

@microsasa

Description

@microsasa

Root Cause

Two Pydantic Field declarations in models.py use default_factory=list for complex generic list types and suppress the resulting pyright warning with # pyright: ignore[reportUnknownVariableType]:

# models.py — AssistantMessageData
toolRequests: list[ToolRequest] = Field(default_factory=list)  # pyright: ignore[reportUnknownVariableType]

# models.py — SessionSummary
shutdown_cycles: list[tuple[datetime | None, SessionShutdownData]] = Field(  # pyright: ignore[reportUnknownVariableType]
    default_factory=list
)

The coding guidelines already address this for dataclasses.field:

In dataclasses.field, use default_factory=lambda: [] (not default_factory=list) for mutable defaults — this avoids a known pyright false-positive.

The same fix applies to Pydantic Field when the element type is a complex generic (e.g. list[ToolRequest] or list[tuple[...]]). Pyright can infer the correct type from the field annotation when lambda: [] is used, but cannot do so reliably when the bare list builtin is passed. Simple list[str] fields work fine with default_factory=list; it is only complex generics that require the lambda.

The coding guidelines note "In Pydantic Field, default_factory=list is fine" — this is accurate for simple element types but incomplete for complex generics. The rule should be extended (or the code fixed) to cover this case.

Fix

Replace default_factory=list with default_factory=lambda: [] for both fields:

# AssistantMessageData
toolRequests: list[ToolRequest] = Field(default_factory=lambda: [])

# SessionSummary
shutdown_cycles: list[tuple[datetime | None, SessionShutdownData]] = Field(
    default_factory=lambda: []
)

This removes both # pyright: ignore suppressions, keeps pyright in strict mode without exceptions, and is consistent with the dataclasses convention already mandated by the coding guidelines.

Update the "In Pydantic Field, default_factory=list is fine" line in CODING_GUIDELINES.md to note that complex generic list types require lambda: [].

Testing Requirement

  • All existing model tests must pass without modification — the behaviour is identical; only the default factory closure changes.
  • Verify that pyright no longer reports reportUnknownVariableType on these fields (i.e. run make check cleanly after the change, with the # pyright: ignore comments removed).

Generated by Code Health Analysis · ● 2.4M ·

Metadata

Metadata

Assignees

No one assigned

    Labels

    awCreated by agentic workflowaw-dispatchedIssue has been dispatched to implementercode-healthCode cleanup and maintenance

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions