Skip to content

Commit 65d3bcf

Browse files
authored
Fix Pydantic 2.12+ compatibility for custom FieldInfo with Annotated types (#727)
Fix Pydantic 2.12+ compatibility for custom FieldInfo with Annotated types Pydantic 2.12+ converts custom FieldInfo subclasses to plain PydanticFieldInfo for fields using Annotated types with metadata (like Coordinates). This caused custom attributes like index, sortable, etc. to be lost. Fix: Capture original FieldInfo objects before Pydantic processes them and restore them when Pydantic has converted them to plain PydanticFieldInfo. * Exclude mypy on PyPy to avoid librt build failure mypy 1.19+ depends on librt which only has CPython wheels. When Poetry tries to install on PyPy, it falls back to building from source, which fails because librt uses CPython-specific C APIs.
1 parent 78b73b2 commit 65d3bcf

File tree

5 files changed

+137
-5
lines changed

5 files changed

+137
-5
lines changed

.github/workflows/docs-pages.yml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: Docs: Build and deploy MkDocs site
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
workflow_dispatch:
8+
9+
permissions:
10+
contents: read
11+
pages: write
12+
id-token: write
13+
14+
concurrency:
15+
group: "pages"
16+
cancel-in-progress: true
17+
18+
jobs:
19+
build:
20+
runs-on: ubuntu-latest
21+
steps:
22+
- name: Checkout
23+
uses: actions/checkout@v4
24+
25+
- name: Set up Python
26+
uses: actions/setup-python@v6
27+
with:
28+
python-version: "3.11"
29+
30+
- name: Install Poetry
31+
uses: snok/install-poetry@v1
32+
with:
33+
virtualenvs-create: true
34+
virtualenvs-in-project: true
35+
installer-parallel: true
36+
37+
- name: Load cached venv
38+
id: cached-poetry-dependencies
39+
uses: actions/cache@v4
40+
with:
41+
path: .venv
42+
key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
43+
44+
- name: Install dependencies
45+
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
46+
run: poetry install --no-interaction --no-root
47+
48+
- name: Install library
49+
run: poetry install --no-interaction
50+
51+
- name: Build MkDocs site
52+
run: poetry run mkdocs build
53+
54+
- name: Setup Pages
55+
uses: actions/configure-pages@v5
56+
57+
- name: Upload built site artifact
58+
uses: actions/upload-pages-artifact@v3
59+
with:
60+
path: site
61+
62+
deploy:
63+
runs-on: ubuntu-latest
64+
needs: build
65+
environment:
66+
name: github-pages
67+
url: ${{ steps.deployment.outputs.page_url }}
68+
steps:
69+
- name: Deploy to GitHub Pages
70+
id: deployment
71+
uses: actions/deploy-pages@v4
72+

aredis_om/model/model.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2068,6 +2068,15 @@ class ModelMeta(ModelMetaclass):
20682068
def __new__(cls, name, bases, attrs, **kwargs): # noqa C901
20692069
meta = attrs.pop("Meta", None)
20702070

2071+
# Capture original FieldInfo objects from attrs before Pydantic processes them.
2072+
# Pydantic 2.12+ may convert custom FieldInfo subclasses to plain PydanticFieldInfo
2073+
# for Annotated types, losing custom attributes like index, sortable, etc.
2074+
original_field_infos: Dict[str, FieldInfo] = {}
2075+
if PYDANTIC_V2:
2076+
for attr_name, attr_value in attrs.items():
2077+
if isinstance(attr_value, FieldInfo):
2078+
original_field_infos[attr_name] = attr_value
2079+
20712080
# Duplicate logic from Pydantic to filter config kwargs because if they are
20722081
# passed directly including the registry Pydantic will pass them over to the
20732082
# superclass causing an error
@@ -2141,13 +2150,31 @@ class Config:
21412150
model_fields = new_class.__fields__
21422151

21432152
for field_name, field in model_fields.items():
2153+
pydantic_field = field # Keep reference to Pydantic's processed field
21442154
if type(field) is PydanticFieldInfo:
2155+
# Pydantic converted our FieldInfo to a plain PydanticFieldInfo.
2156+
# This happens with Annotated types in Pydantic 2.12+.
2157+
# Use the original FieldInfo if we captured it, otherwise create a new one.
21452158
if PYDANTIC_V2:
2146-
field = FieldInfo(**field._attributes_set)
2159+
if field_name in original_field_infos:
2160+
# Use the original FieldInfo with custom attributes preserved
2161+
field = original_field_infos[field_name]
2162+
# Copy the annotation from Pydantic's processed field
2163+
# since it wasn't set on the original FieldInfo
2164+
if hasattr(pydantic_field, "annotation"):
2165+
field.annotation = pydantic_field.annotation
2166+
# Also copy metadata from Pydantic's field (validators, serializers, etc.)
2167+
if hasattr(pydantic_field, "metadata"):
2168+
field.metadata = pydantic_field.metadata
2169+
else:
2170+
field = FieldInfo(**field._attributes_set)
21472171
else:
21482172
# Pydantic v1 compatibility
21492173
field = FieldInfo()
21502174
setattr(new_class, field_name, field)
2175+
# Also update model_fields so schema generation uses our fixed field
2176+
if PYDANTIC_V2:
2177+
model_fields[field_name] = field
21512178

21522179
if is_indexed:
21532180
setattr(new_class, field_name, ExpressionProxy(field, []))

mkdocs.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
site_name: Redis OM Python
2+
site_description: Object mapping, and more, for Redis and Python.
3+
site_url: https://redis.github.io/redis-om-python/
4+
repo_url: https://github.com/redis/redis-om-python
5+
repo_name: redis-om-python
6+
7+
# Source Markdown lives in the existing docs/ directory.
8+
docs_dir: docs
9+
10+
# Use the built-in MkDocs theme for now. This avoids extra dependencies.
11+
theme:
12+
name: mkdocs
13+
14+
nav:
15+
- Home: index.md
16+
- Getting started: getting_started.md
17+
- Models: models.md
18+
- Connections: connections.md
19+
- Validation: validation.md
20+
- Redis modules: redis_modules.md
21+
- Migrations:
22+
- Overview: migrations.md
23+
- Upgrade 0.x to 1.x: migration_guide_0x_to_1x.md
24+
- FastAPI integration: fastapi_integration.md
25+
- Errors: errors.md
26+

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ setuptools = ">=70.0"
4949
pydantic-extra-types = "^2.10.5"
5050

5151
[tool.poetry.group.dev.dependencies]
52-
mypy = "^1.9.0"
52+
mypy = {version = "^1.9.0", markers = "platform_python_implementation == 'CPython'"}
5353
pytest = "^8.0.2"
5454
ipdb = "^0.13.9"
5555
black = "^24.2"
@@ -66,6 +66,7 @@ tox = "^4.14.1"
6666
tox-pyenv = "^1.1.0"
6767
codespell = "^2.2.0"
6868
pre-commit = {version = "^4.3.0", python = ">=3.9"}
69+
mkdocs = "^1.6.1"
6970

7071
[tool.poetry.scripts]
7172
# Unified CLI (new, recommended) - uses async components

tests/test_json_model.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,7 +1528,9 @@ class Product(JsonModel, index=True):
15281528
name: str = Field(index=True, sortable=True) # TAG field with sortable
15291529
category: str = Field(index=True, sortable=True) # TAG field with sortable
15301530
price: int = Field(index=True, sortable=True) # NUMERIC field with sortable
1531-
tags: List[str] = Field(index=True, sortable=True) # TAG field (list) with sortable
1531+
tags: List[str] = Field(
1532+
index=True, sortable=True
1533+
) # TAG field (list) with sortable
15321534

15331535
class Meta:
15341536
global_key_prefix = key_prefix
@@ -1543,9 +1545,13 @@ class Meta:
15431545
await Migrator().run()
15441546

15451547
# Create test data
1546-
product1 = Product(name="Zebra", category="Animals", price=100, tags=["wild", "africa"])
1548+
product1 = Product(
1549+
name="Zebra", category="Animals", price=100, tags=["wild", "africa"]
1550+
)
15471551
product2 = Product(name="Apple", category="Fruits", price=50, tags=["red", "sweet"])
1548-
product3 = Product(name="Banana", category="Fruits", price=30, tags=["yellow", "sweet"])
1552+
product3 = Product(
1553+
name="Banana", category="Fruits", price=30, tags=["yellow", "sweet"]
1554+
)
15491555

15501556
await product1.save()
15511557
await product2.save()

0 commit comments

Comments
 (0)