Skip to content

Commit 5a2a621

Browse files
Skip pydantic.v1 import on Python 3.14+ to avoid UserWarning (#4283)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 5265c74 commit 5a2a621

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

RELEASE.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Release type: patch
2+
3+
Fix compatibility with Python 3.14 when using the Pydantic integration with Pydantic V2. Previously, importing `strawberry.experimental.pydantic` on Python 3.14 would trigger:
4+
5+
UserWarning: Core Pydantic V1 functionality isn't compatible with Python 3.14 or greater.
6+
7+
This is now fixed by avoiding `pydantic.v1` imports on Python 3.14+.

strawberry/experimental/pydantic/_compat.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import dataclasses
2+
import sys
23
from collections.abc import Callable
34
from dataclasses import dataclass
45
from decimal import Decimal
@@ -22,6 +23,9 @@
2223
def _get_base_model_types() -> tuple[type[Any], ...]:
2324
model_types: list[type[Any]] = [BaseModel]
2425

26+
if sys.version_info >= (3, 14):
27+
return tuple(model_types)
28+
2529
try:
2630
from pydantic.v1 import BaseModel as BaseModelV1
2731
except ImportError:

tests/experimental/pydantic/schema/test_1_and_2.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import importlib
12
import sys
23
import textwrap
4+
import warnings
35

46
import pytest
57

@@ -132,3 +134,19 @@ class Query:
132134
"""
133135

134136
assert str(schema) == textwrap.dedent(expected_schema).strip()
137+
138+
139+
@pytest.mark.skipif(
140+
sys.version_info < (3, 14),
141+
reason="This test is only relevant on Python 3.14+",
142+
)
143+
@needs_pydantic_v2
144+
def test_no_pydantic_v1_warning_on_python_314():
145+
import strawberry.experimental.pydantic._compat as compat_module
146+
147+
with warnings.catch_warnings(record=True) as caught:
148+
warnings.simplefilter("always")
149+
importlib.reload(compat_module)
150+
151+
pydantic_v1_warnings = [w for w in caught if "Pydantic V1" in str(w.message)]
152+
assert pydantic_v1_warnings == []

0 commit comments

Comments
 (0)