1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
from typing import Annotated
import pytest
from typing_extensions import Self, deprecated
from pydantic import BaseModel, Field, computed_field, field_validator, model_validator
def test_deprecated_fields():
class Model(BaseModel):
a: Annotated[int, Field(deprecated='')]
b: Annotated[int, Field(deprecated='This is deprecated')]
c: Annotated[int, Field(deprecated=None)]
assert Model.model_json_schema() == {
'properties': {
'a': {'deprecated': True, 'title': 'A', 'type': 'integer'},
'b': {'deprecated': True, 'title': 'B', 'type': 'integer'},
'c': {'title': 'C', 'type': 'integer'},
},
'required': ['a', 'b', 'c'],
'title': 'Model',
'type': 'object',
}
instance = Model(a=1, b=1, c=1)
pytest.warns(DeprecationWarning, lambda: instance.a, match='^$')
with pytest.warns(DeprecationWarning, match='^This is deprecated$'):
b = instance.b
assert b == 1
def test_deprecated_fields_deprecated_class():
class Model(BaseModel):
a: Annotated[int, deprecated('')]
b: Annotated[int, deprecated('This is deprecated')] = 1
c: Annotated[int, Field(deprecated=deprecated('This is deprecated'))] = 1
assert Model.model_json_schema() == {
'properties': {
'a': {'deprecated': True, 'title': 'A', 'type': 'integer'},
'b': {'default': 1, 'deprecated': True, 'title': 'B', 'type': 'integer'},
'c': {'default': 1, 'deprecated': True, 'title': 'C', 'type': 'integer'},
},
'required': ['a'],
'title': 'Model',
'type': 'object',
}
instance = Model(a=1)
pytest.warns(DeprecationWarning, lambda: instance.a, match='^$')
pytest.warns(DeprecationWarning, lambda: instance.b, match='^This is deprecated$')
pytest.warns(DeprecationWarning, lambda: instance.c, match='^This is deprecated$')
def test_deprecated_fields_field_validator():
class Model(BaseModel):
x: int = Field(deprecated='x is deprecated')
@field_validator('x')
@classmethod
def validate_x(cls, v: int) -> int:
return v * 2
instance = Model(x=1)
with pytest.warns(DeprecationWarning):
assert instance.x == 2
def test_deprecated_fields_model_validator():
class Model(BaseModel):
x: int = Field(deprecated='x is deprecated')
@model_validator(mode='after')
def validate_x(self) -> Self:
self.x = self.x * 2
return self
with pytest.warns(DeprecationWarning):
instance = Model(x=1)
assert instance.x == 2
def test_deprecated_fields_validate_assignment():
class Model(BaseModel):
x: int = Field(deprecated='x is deprecated')
model_config = {'validate_assignment': True}
instance = Model(x=1)
with pytest.warns(DeprecationWarning):
assert instance.x == 1
instance.x = 2
with pytest.warns(DeprecationWarning):
assert instance.x == 2
def test_computed_field_deprecated():
class Model(BaseModel):
@computed_field
@property
@deprecated('This is deprecated')
def p1(self) -> int:
return 1
@computed_field(deprecated='This is deprecated')
@property
@deprecated('This is deprecated (this message is overridden)')
def p2(self) -> int:
return 1
@computed_field(deprecated='')
@property
def p3(self) -> int:
return 1
@computed_field(deprecated='This is deprecated')
@property
def p4(self) -> int:
return 1
@computed_field
@deprecated('This is deprecated')
def p5(self) -> int:
return 1
assert Model.model_json_schema(mode='serialization') == {
'properties': {
'p1': {'deprecated': True, 'readOnly': True, 'title': 'P1', 'type': 'integer'},
'p2': {'deprecated': True, 'readOnly': True, 'title': 'P2', 'type': 'integer'},
'p3': {'deprecated': True, 'readOnly': True, 'title': 'P3', 'type': 'integer'},
'p4': {'deprecated': True, 'readOnly': True, 'title': 'P4', 'type': 'integer'},
'p5': {'deprecated': True, 'readOnly': True, 'title': 'P5', 'type': 'integer'},
},
'required': ['p1', 'p2', 'p3', 'p4', 'p5'],
'title': 'Model',
'type': 'object',
}
instance = Model()
pytest.warns(DeprecationWarning, lambda: instance.p1, match='^This is deprecated$')
pytest.warns(DeprecationWarning, lambda: instance.p2, match='^This is deprecated$')
pytest.warns(DeprecationWarning, lambda: instance.p4, match='^This is deprecated$')
pytest.warns(DeprecationWarning, lambda: instance.p5, match='^This is deprecated$')
with pytest.warns(DeprecationWarning, match='^$'):
p3 = instance.p3
assert p3 == 1
def test_computed_field_deprecated_deprecated_class():
class Model(BaseModel):
@computed_field(deprecated=deprecated('This is deprecated'))
@property
def p1(self) -> int:
return 1
@computed_field(deprecated=True)
@property
def p2(self) -> int:
return 2
@computed_field(deprecated='This is a deprecated string')
@property
def p3(self) -> int:
return 3
assert Model.model_json_schema(mode='serialization') == {
'properties': {
'p1': {'deprecated': True, 'readOnly': True, 'title': 'P1', 'type': 'integer'},
'p2': {'deprecated': True, 'readOnly': True, 'title': 'P2', 'type': 'integer'},
'p3': {'deprecated': True, 'readOnly': True, 'title': 'P3', 'type': 'integer'},
},
'required': ['p1', 'p2', 'p3'],
'title': 'Model',
'type': 'object',
}
instance = Model()
with pytest.warns(DeprecationWarning, match='^This is deprecated$'):
p1 = instance.p1
with pytest.warns(DeprecationWarning, match='^deprecated$'):
p2 = instance.p2
with pytest.warns(DeprecationWarning, match='^This is a deprecated string$'):
p3 = instance.p3
assert p1 == 1
assert p2 == 2
assert p3 == 3
def test_deprecated_with_boolean() -> None:
class Model(BaseModel):
a: Annotated[int, Field(deprecated=True)]
b: Annotated[int, Field(deprecated=False)]
assert Model.model_json_schema() == {
'properties': {
'a': {'deprecated': True, 'title': 'A', 'type': 'integer'},
'b': {'title': 'B', 'type': 'integer'},
},
'required': ['a', 'b'],
'title': 'Model',
'type': 'object',
}
instance = Model(a=1, b=1)
pytest.warns(DeprecationWarning, lambda: instance.a, match='deprecated')
def test_computed_field_deprecated_class_access() -> None:
class Model(BaseModel):
@computed_field(deprecated=True)
def prop(self) -> int:
return 1
assert isinstance(Model.prop, property)
def test_computed_field_deprecated_subclass() -> None:
"""https://github.com/pydantic/pydantic/issues/10384"""
class Base(BaseModel):
@computed_field(deprecated=True)
def prop(self) -> int:
return 1
class Sub(Base):
pass
def test_deprecated_field_forward_annotation() -> None:
"""https://github.com/pydantic/pydantic/issues/11390"""
class Model(BaseModel):
a: "Annotated[Test, deprecated('test')]" = 2
Test = int
Model.model_rebuild()
assert isinstance(Model.model_fields['a'].deprecated, deprecated)
assert Model.model_fields['a'].deprecated.message == 'test'
m = Model()
pytest.warns(DeprecationWarning, lambda: m.a, match='test')
def test_deprecated_field_with_assignment() -> None:
class Model(BaseModel):
# A buggy implementation made it so that deprecated wouldn't
# appear on the `FieldInfo`:
a: Annotated[int, deprecated('test')] = Field(default=1)
assert isinstance(Model.model_fields['a'].deprecated, deprecated)
assert Model.model_fields['a'].deprecated.message == 'test'
|