python config_error ai_generated true

pydantic.errors.PydanticUserError: A non-annotated attribute was detected: `children = []`. All model fields require a type annotation; if `children` is not meant to be a field, you may be able to resolve this error by annotating it as a `ClassVar` or updating `model_config['ignored_types']`.

ID: python/pydantic-recursion-loop-model

Also available as: JSON · Markdown · 中文
80%Fix Rate
90%Confidence
0Evidence
2024-07-08First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
2.x active — — —

Root Cause

A class-level assignment without a type annotation is treated as a field in Pydantic v2, even if it was intended as a constant or helper.

generic

中文

Pydantic v2 中,没有类型注解的类级赋值会被当作字段,即使原意为常量或辅助变量。

Workarounds

  1. 95% success
    Annotate as ClassVar: `from typing import ClassVar
    children: ClassVar[list] = []`
  2. 92% success
    Add a type annotation: `children: list[Node] = []` if it really is a field (use `Field(default_factory=list)`).

Dead Ends

Common approaches that don't work:

  1. 60% fail

    Setting the attribute inside `__init__` instead of at class level changes semantics and breaks serialization.

  2. 40% fail

    Adding `ClassVar` but keeping the mutable default still shares state across instances.