No ForwardRef in model_fields annotations with list['Model'] field type with legacy-style annotations (w/o from __future__ import annotations)
Author: AntonOvsyannikovCreated Dec 4, 2025Updated Sep 16, 2026
Labelsbug V2topic-annotations
Initial Checks
- I confirm that I'm using Pydantic V2
Description
There is no ForwardRef in model_fields annotations with list['Model'] field type with legacy-style annotations (w/o from __future__ import annotations) when forward model name is declared as string literal.
So model_rebuild() does not work correctly, and after rebuild there is no actual type in field annotation.
If we take a look into captured annotations, we'll see that event before rebuild annotation is not a ForwardRef.
class Foo(BaseModel):
bar1: 'Bar'
bar2: list['Bar']
class Bar(BaseModel):
pass
print(Foo.model_fields['bar1'].annotation) # ForwardRef('Bar') OK
print(Foo.model_fields['bar2'].annotation) # list['Bar'] ???
Foo.model_rebuild()
print(Foo.model_fields['bar1'].annotation) # <class '__main__.Bar'> OK
print(Foo.model_fields['bar2'].annotation) # list['Bar'] ???with from __future__ import annotations PEP563 annotations everything works as expected
class Foo(BaseModel):
bar1: Bar
bar2: list[Bar]
class Bar(BaseModel):
pass
print(Foo.model_fields['bar1'].annotation) # ForwardRef('Bar') OK
print(Foo.model_fields['bar2'].annotation) # ForwardRef('list[Bar]') OK
Foo.model_rebuild()
print(Foo.model_fields['bar1'].annotation) # <class '__main__.Bar'> OK
print(Foo.model_fields['bar2'].annotation) # list[__main__.Bar] OKExample Code
# from __future__ import annotations
import sys
import pydantic
from pydantic import BaseModel
print(sys.version) # 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)]
print(pydantic.version.VERSION) # 2.12.5
class Foo(BaseModel):
bar1: 'Bar'
bar2: list['Bar']
class Bar(BaseModel):
pass
print(Foo.model_fields['bar1'].annotation) # ForwardRef('Bar') OK
print(Foo.model_fields['bar2'].annotation) # list['Bar'] ???
print()
Foo.model_rebuild()
print(Foo.model_fields['bar1'].annotation) # <class '__main__.Bar'> OK
print(Foo.model_fields['bar2'].annotation) # list['Bar'] ???Python, Pydantic & OS Version
> python -c "import pydantic.version; print(pydantic.version.version_info())"
pydantic version: 2.12.5
pydantic-core version: 2.41.5
pydantic-core build: profile=release pgo=false
python version: 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)]
platform: Windows-10-10.0.26200-SP0
related packages: fastapi-0.123.5 mypy-1.16.1 typing_extensions-4.15.0
commit: unknownSource: pydantic/pydantic