Skip to content

Commit 808a681

Browse files
committed
fix: handle deprecation in pydantic 2.10
Otherwise we get: ``` pydantic.warnings.PydanticDeprecatedSince211: Accessing the 'model_fields' attribute on the instance is deprecated. Instead, you should access this attribute from the model class. Deprecated in Pydantic V2.11 to be removed in V3.0. ```
1 parent e46dd97 commit 808a681

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

src/pydantic_compat/_shared.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from packaging.version import Version
99

1010
PYDANTIC2 = Version(pydantic.version.VERSION) >= Version("2")
11+
PYDANTIC2_10 = Version(pydantic.version.VERSION) >= Version("2.10")
1112
FIELD_KWARGS = {
1213
p.name
1314
for p in signature(pydantic.Field).parameters.values()

src/pydantic_compat/_v2/mixin.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
from pydantic import BaseModel
66
from pydantic._internal import _model_construction
77

8-
from pydantic_compat._shared import V2_RENAMED_CONFIG_KEYS, check_mixin_order
8+
from pydantic_compat._shared import (
9+
PYDANTIC2_10,
10+
V2_RENAMED_CONFIG_KEYS,
11+
check_mixin_order,
12+
)
913

1014
if TYPE_CHECKING:
1115
from pydantic import ConfigDict
@@ -93,7 +97,10 @@ def parse_raw(cls: type[Model], *args: Any, **kwargs: Any) -> Any:
9397
# this is needed in addition to the metaclass patch in __init_subclass__
9498
@property
9599
def __fields__(self: Model) -> Dict[str, Any]: # noqa: UP006
96-
return self.model_fields
100+
if PYDANTIC2_10:
101+
return self.__class__.model_fields
102+
else:
103+
return self.model_fields
97104

98105
@property
99106
def __fields_set__(self: Model) -> set[str]:

tests/test_base_model.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55

66
from pydantic_compat import PYDANTIC2, PydanticCompatMixin
7+
from pydantic_compat._shared import PYDANTIC2_10
78

89

910
class Model(PydanticCompatMixin, pydantic.BaseModel):
@@ -64,7 +65,8 @@ def test_v1_attributes() -> None:
6465

6566
def test_v2_attributes() -> None:
6667
m = Model()
67-
assert "x" in m.model_fields
68+
if not PYDANTIC2_10:
69+
assert "x" in m.model_fields
6870
assert "x" in Model.model_fields
6971
assert "x" not in m.model_fields_set
7072
m.x = 2

0 commit comments

Comments
 (0)