Skip to content

Commit dfa6c67

Browse files
authored
Mention the attribute name in model fields deprecation message (#11674)
1 parent cbf4202 commit dfa6c67

2 files changed

Lines changed: 5 additions & 3 deletions

File tree

pydantic/_internal/_utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import dataclasses
99
import keyword
10+
import sys
1011
import typing
1112
import warnings
1213
import weakref
@@ -420,8 +421,9 @@ def __get__(self, instance: None, objtype: type[_ModelT]) -> _RT: ...
420421
def __get__(self, instance: _ModelT, objtype: type[_ModelT]) -> _RT: ...
421422
def __get__(self, instance: _ModelT | None, objtype: type[_ModelT]) -> _RT:
422423
if instance is not None:
424+
attr_name = self.fget.__name__ if sys.version_info >= (3, 10) else self.fget.__func__.__name__
423425
warnings.warn(
424-
'Accessing this attribute on the instance is deprecated, and will be removed in Pydantic V3. '
426+
f'Accessing the {attr_name!r} attribute on the instance is deprecated. '
425427
'Instead, you should access this attribute from the model class.',
426428
category=PydanticDeprecatedSince211,
427429
stacklevel=2,

tests/test_deprecated.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,10 @@ def area(self) -> int:
330330
m = Model(x=1)
331331
assert len(Model.model_fields) == 2
332332

333-
with pytest.warns(PydanticDeprecatedSince211):
333+
with pytest.warns(PydanticDeprecatedSince211, match="Accessing the 'model_fields' attribute"):
334334
assert len(m.model_fields) == 2
335335

336-
with pytest.warns(PydanticDeprecatedSince211):
336+
with pytest.warns(PydanticDeprecatedSince211, match="Accessing the 'model_computed_fields' attribute"):
337337
assert len(m.model_computed_fields) == 1
338338

339339
match = '^The `__fields__` attribute is deprecated, use `model_fields` instead.'

0 commit comments

Comments
 (0)