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 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
|
import platform
import re
from collections.abc import Iterable
from datetime import date, timedelta
from pathlib import Path
from types import SimpleNamespace
from typing import Any, Literal
import pytest
from pydantic_core import CoreSchema, core_schema
from pydantic import (
BaseModel,
ConfigDict,
Field,
GetCoreSchemaHandler,
GetJsonSchemaHandler,
PydanticDeprecatedSince20,
PydanticDeprecatedSince211,
PydanticUserError,
ValidationError,
computed_field,
conlist,
root_validator,
)
from pydantic.config import Extra
from pydantic.deprecated.decorator import validate_arguments
from pydantic.deprecated.json import custom_pydantic_encoder, pydantic_encoder, timedelta_isoformat
from pydantic.deprecated.parse import load_file, load_str_bytes
from pydantic.deprecated.tools import parse_obj_as, schema_json_of, schema_of
from pydantic.functional_serializers import model_serializer
from pydantic.json_schema import JsonSchemaValue
from pydantic.type_adapter import TypeAdapter
# `pytest.warns/raises()` is thread unsafe. As these tests are meant to be
# removed in V3, we just mark all tests as thread unsafe
pytestmark = pytest.mark.thread_unsafe
def deprecated_from_orm(model_type: type[BaseModel], obj: Any) -> Any:
with pytest.warns(
PydanticDeprecatedSince20,
match=re.escape(
"The `from_orm` method is deprecated; set `model_config['from_attributes']=True` "
'and use `model_validate` instead.'
),
):
return model_type.from_orm(obj)
def test_from_attributes_root():
class PokemonCls:
def __init__(self, *, en_name: str, jp_name: str):
self.en_name = en_name
self.jp_name = jp_name
class Pokemon(BaseModel):
model_config = ConfigDict(from_attributes=True)
en_name: str
jp_name: str
with pytest.warns(
PydanticDeprecatedSince20, match='Pydantic V1 style `@root_validator` validators are deprecated.'
):
class PokemonList(BaseModel):
root: list[Pokemon]
@root_validator(pre=True)
@classmethod
def populate_root(cls, values):
return {'root': values}
@model_serializer(mode='wrap')
def _serialize(self, handler, info):
data = handler(self)
if info.mode == 'json':
return data['root']
else:
return data
@classmethod
def model_modify_json_schema(cls, json_schema):
return json_schema['properties']['root']
model_config = ConfigDict(from_attributes=True)
pika = PokemonCls(en_name='Pikachu', jp_name='ピカチュウ')
bulbi = PokemonCls(en_name='Bulbasaur', jp_name='フシギダネ')
pokemons = deprecated_from_orm(PokemonList, [pika, bulbi])
assert pokemons.root == [
Pokemon(en_name='Pikachu', jp_name='ピカチュウ'),
Pokemon(en_name='Bulbasaur', jp_name='フシギダネ'),
]
with pytest.warns(
PydanticDeprecatedSince20, match='Pydantic V1 style `@root_validator` validators are deprecated.'
):
class PokemonDict(BaseModel):
root: dict[str, Pokemon]
model_config = ConfigDict(from_attributes=True)
@root_validator(pre=True)
@classmethod
def populate_root(cls, values):
return {'root': values}
@model_serializer(mode='wrap')
def _serialize(self, handler, info):
data = handler(self)
if info.mode == 'json':
return data['root']
else:
return data
@classmethod
def model_modify_json_schema(cls, json_schema):
return json_schema['properties']['root']
pokemons = deprecated_from_orm(PokemonDict, {'pika': pika, 'bulbi': bulbi})
assert pokemons.root == {
'pika': Pokemon(en_name='Pikachu', jp_name='ピカチュウ'),
'bulbi': Pokemon(en_name='Bulbasaur', jp_name='フシギダネ'),
}
def test_from_attributes():
class PetCls:
def __init__(self, *, name: str, species: str):
self.name = name
self.species = species
class PersonCls:
def __init__(self, *, name: str, age: float = None, pets: list[PetCls]):
self.name = name
self.age = age
self.pets = pets
class Pet(BaseModel):
model_config = ConfigDict(from_attributes=True)
name: str
species: str
class Person(BaseModel):
model_config = ConfigDict(from_attributes=True)
name: str
age: float = None
pets: list[Pet]
bones = PetCls(name='Bones', species='dog')
orion = PetCls(name='Orion', species='cat')
anna = PersonCls(name='Anna', age=20, pets=[bones, orion])
anna_model = deprecated_from_orm(Person, anna)
assert anna_model.model_dump() == {
'name': 'Anna',
'pets': [{'name': 'Bones', 'species': 'dog'}, {'name': 'Orion', 'species': 'cat'}],
'age': 20.0,
}
def test_not_from_attributes():
class Pet(BaseModel):
name: str
species: str
with pytest.raises(PydanticUserError):
deprecated_from_orm(Pet, None)
def test_object_with_getattr():
class FooGetAttr:
def __getattr__(self, key: str):
if key == 'foo':
return 'Foo'
else:
raise AttributeError
class Model(BaseModel):
model_config = ConfigDict(from_attributes=True)
foo: str
bar: int = 1
class ModelInvalid(BaseModel):
model_config = ConfigDict(from_attributes=True)
foo: str
bar: int
foo = FooGetAttr()
model = deprecated_from_orm(Model, foo)
assert model.foo == 'Foo'
assert model.bar == 1
assert model.model_dump(exclude_unset=True) == {'foo': 'Foo'}
with pytest.raises(ValidationError):
deprecated_from_orm(ModelInvalid, foo)
def test_properties():
class XyProperty:
x = 4
@property
def y(self):
return '5'
class Model(BaseModel):
model_config = ConfigDict(from_attributes=True)
x: int
y: int
model = deprecated_from_orm(Model, XyProperty())
assert model.x == 4
assert model.y == 5
@pytest.mark.parametrize('extra', ['ignore', 'forbid', 'allow'])
def test_extra_allow_from_orm(extra: Literal['ignore', 'forbid', 'allow']):
class TestCls:
x = 1
y = 2
class Model(BaseModel):
model_config = ConfigDict(from_attributes=True, extra=extra)
x: int
model = deprecated_from_orm(Model, TestCls())
assert model.x == 1
assert not hasattr(model, 'y')
@pytest.mark.filterwarnings('ignore:Pydantic V1 style `@root_validator` validators are deprecated.*:DeprecationWarning')
def test_root_validator():
validator_value = None
class TestCls:
x = 1
y = 2
class Model(BaseModel):
model_config = ConfigDict(from_attributes=True)
x: int
y: int
z: int
@root_validator(pre=True)
def change_input_data(cls, value):
nonlocal validator_value
validator_value = value
return {'x': value.x, 'y': value.y, 'z': value.x + value.y}
model = deprecated_from_orm(Model, TestCls())
assert model.model_dump() == {'x': 1, 'y': 2, 'z': 3}
# assert isinstance(validator_value, GetterDict)
assert isinstance(validator_value, TestCls)
def test_nested_orm():
class User(BaseModel):
model_config = ConfigDict(from_attributes=True)
first_name: str
last_name: str
class State(BaseModel):
model_config = ConfigDict(from_attributes=True)
user: User
# Pass an "orm instance"
deprecated_from_orm(State, SimpleNamespace(user=SimpleNamespace(first_name='John', last_name='Appleseed')))
# Pass dictionary data directly
State(**{'user': {'first_name': 'John', 'last_name': 'Appleseed'}})
def test_parse_raw_pass():
class Model(BaseModel):
x: int
y: int
with pytest.warns(PydanticDeprecatedSince20) as all_warnings:
model = Model.parse_raw('{"x": 1, "y": 2}')
assert model.model_dump() == {'x': 1, 'y': 2}
assert len(all_warnings) == 2
expected_warnings = [
'The `parse_raw` method is deprecated; if your data is JSON use `model_validate_json`, otherwise load the data then use `model_validate` instead',
'`load_str_bytes` is deprecated',
]
assert [w.message.message for w in all_warnings] == expected_warnings
@pytest.mark.skipif(platform.python_implementation() == 'PyPy', reason='Different error str on PyPy')
def test_parse_raw_pass_fail():
class Model(BaseModel):
x: int
y: int
with pytest.warns(PydanticDeprecatedSince20) as all_warnings:
with pytest.raises(ValidationError, match='1 validation error for Model') as exc_info:
Model.parse_raw('invalid')
assert len(all_warnings) == 2
expected_warnings = [
'The `parse_raw` method is deprecated; if your data is JSON use `model_validate_json`, otherwise load the data then use `model_validate` instead',
'`load_str_bytes` is deprecated',
]
assert [w.message.message for w in all_warnings] == expected_warnings
# insert_assert(exc_info.value.errors(include_url=False))
assert exc_info.value.errors(include_url=False) == [
{
'type': 'value_error.jsondecode',
'loc': ('__root__',),
'msg': 'Expecting value: line 1 column 1 (char 0)',
'input': 'invalid',
}
]
def test_fields():
class Model(BaseModel):
x: int
y: int = 2
@computed_field
@property
def area(self) -> int:
return self.x * self.y
m = Model(x=1)
assert len(Model.model_fields) == 2
with pytest.warns(PydanticDeprecatedSince211, match="Accessing the 'model_fields' attribute"):
assert len(m.model_fields) == 2
with pytest.warns(PydanticDeprecatedSince211, match="Accessing the 'model_computed_fields' attribute"):
assert len(m.model_computed_fields) == 1
match = '^The `__fields__` attribute is deprecated, use the `model_fields` class property instead.'
with pytest.warns(PydanticDeprecatedSince20, match=match):
assert len(Model.__fields__) == 2
with pytest.warns(PydanticDeprecatedSince20, match=match):
assert len(m.__fields__) == 2
def test_fields_set():
class Model(BaseModel):
x: int
y: int = 2
m = Model(x=1)
assert m.model_fields_set == {'x'}
match = '^The `__fields_set__` attribute is deprecated, use `model_fields_set` instead.'
with pytest.warns(PydanticDeprecatedSince20, match=match):
assert m.__fields_set__ == {'x'}
def test_fields_dir():
class Model(BaseModel):
x: int
y: int = 2
assert '__fields__' not in dir(Model)
@pytest.mark.parametrize('attribute,value', [('allow', 'allow'), ('ignore', 'ignore'), ('forbid', 'forbid')])
def test_extra_used_as_enum(
attribute: str,
value: str,
) -> None:
with pytest.warns(
PydanticDeprecatedSince20,
match=re.escape("`pydantic.config.Extra` is deprecated, use literal values instead (e.g. `extra='allow'`)"),
):
assert getattr(Extra, attribute) == value
def test_field_min_items_deprecation():
m = '`min_items` is deprecated and will be removed. use `min_length` instead'
with pytest.warns(PydanticDeprecatedSince20, match=m):
class Model(BaseModel):
x: list[int] = Field(None, min_items=1)
with pytest.raises(ValidationError) as exc_info:
Model(x=[])
assert exc_info.value.errors(include_url=False) == [
{
'type': 'too_short',
'loc': ('x',),
'msg': 'List should have at least 1 item after validation, not 0',
'input': [],
'ctx': {'field_type': 'List', 'min_length': 1, 'actual_length': 0},
}
]
def test_field_min_items_with_min_length():
m = '`min_items` is deprecated and will be removed. use `min_length` instead'
with pytest.warns(PydanticDeprecatedSince20, match=m):
class Model(BaseModel):
x: list[int] = Field(None, min_items=1, min_length=2)
with pytest.raises(ValidationError) as exc_info:
Model(x=[1])
assert exc_info.value.errors(include_url=False) == [
{
'type': 'too_short',
'loc': ('x',),
'msg': 'List should have at least 2 items after validation, not 1',
'input': [1],
'ctx': {'field_type': 'List', 'min_length': 2, 'actual_length': 1},
}
]
def test_field_max_items():
m = '`max_items` is deprecated and will be removed. use `max_length` instead'
with pytest.warns(PydanticDeprecatedSince20, match=m):
class Model(BaseModel):
x: list[int] = Field(None, max_items=1)
with pytest.raises(ValidationError) as exc_info:
Model(x=[1, 2])
assert exc_info.value.errors(include_url=False) == [
{
'type': 'too_long',
'loc': ('x',),
'msg': 'List should have at most 1 item after validation, not 2',
'input': [1, 2],
'ctx': {'field_type': 'List', 'max_length': 1, 'actual_length': 2},
}
]
def test_field_max_items_with_max_length():
m = '`max_items` is deprecated and will be removed. use `max_length` instead'
with pytest.warns(PydanticDeprecatedSince20, match=m):
class Model(BaseModel):
x: list[int] = Field(None, max_items=1, max_length=2)
with pytest.raises(ValidationError) as exc_info:
Model(x=[1, 2, 3])
assert exc_info.value.errors(include_url=False) == [
{
'type': 'too_long',
'loc': ('x',),
'msg': 'List should have at most 2 items after validation, not 3',
'input': [1, 2, 3],
'ctx': {'field_type': 'List', 'max_length': 2, 'actual_length': 3},
}
]
def test_field_const():
with pytest.raises(PydanticUserError, match='`const` is removed. use `Literal` instead'):
class Model(BaseModel):
x: str = Field('test', const=True)
def test_field_include_deprecation():
with pytest.warns(PydanticDeprecatedSince20) as all_warnings:
class Model(BaseModel):
x: int = Field(include=True)
assert len(all_warnings) == 2
expected_warnings = [
"Using extra keyword arguments on `Field` is deprecated and will be removed. Use `json_schema_extra` instead. (Extra keys: 'include')",
'`include` is deprecated and does nothing. It will be removed, use `exclude` instead',
]
assert [w.message.message for w in all_warnings] == expected_warnings
def test_unique_items_items():
with pytest.raises(PydanticUserError, match='`unique_items` is removed. use `Set` instead'):
class Model(BaseModel):
x: list[int] = Field(None, unique_items=True)
def test_unique_items_conlist():
with pytest.raises(PydanticUserError, match='`unique_items` is removed. use `Set` instead'):
class Model(BaseModel):
x: conlist(int, unique_items=True)
def test_allow_mutation():
m = '`allow_mutation` is deprecated and will be removed. use `frozen` instead'
with pytest.warns(PydanticDeprecatedSince20, match=m):
class Model(BaseModel):
model_config = ConfigDict(validate_assignment=True)
x: int = Field(allow_mutation=False)
y: int = Field(allow_mutation=True)
m = Model(x=1, y=2)
assert m.x == 1
with pytest.raises(ValidationError) as exc_info:
m.x = 2
assert exc_info.value.errors(include_url=False) == [
{'input': 2, 'loc': ('x',), 'msg': 'Field is frozen', 'type': 'frozen_field'}
]
m.y = 3
assert m.y == 3
def test_field_regex():
with pytest.raises(PydanticUserError, match='`regex` is removed. use `pattern` instead'):
class Model(BaseModel):
x: str = Field('test', regex=r'^test$')
def test_modify_schema_error():
with pytest.raises(
PydanticUserError,
match='The `__modify_schema__` method is not supported in Pydantic v2. '
'Use `__get_pydantic_json_schema__` instead in class `Model`.',
):
class Model(BaseModel):
@classmethod
def __modify_schema__(cls, field_schema: dict[str, Any]) -> None:
pass
def test_modify_schema_on_nested_class_error() -> None:
class SomeLongName:
@classmethod
def __modify_schema__(cls, field_schema):
pass
with pytest.raises(
PydanticUserError,
match='The `__modify_schema__` method is not supported in Pydantic v2. '
'Use `__get_pydantic_json_schema__` instead in class `SomeLongName`.',
):
class B(BaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True)
a: SomeLongName
def test_v1_v2_custom_type_compatibility() -> None:
"""Create a custom type that works with V1 and V2"""
class MyType:
@classmethod
def __get_pydantic_core_schema__(cls, source_type: Any, handler: GetCoreSchemaHandler) -> CoreSchema:
return core_schema.int_schema()
@classmethod
def __get_pydantic_json_schema__(
cls, core_schema: CoreSchema, handler: GetJsonSchemaHandler
) -> JsonSchemaValue:
return {'anyOf': [{'type': 'string'}, {'type': 'number'}]}
@classmethod
def __modify_schema__(cls, field_schema: dict[str, Any]) -> None:
raise NotImplementedError # not actually called, we just want to make sure the method can exist
@classmethod
def __get_validators__(cls) -> Iterable[Any]:
raise NotImplementedError # not actually called, we just want to make sure the method can exist
yield
ta = TypeAdapter(MyType)
assert ta.validate_python('123') == 123
assert ta.json_schema() == {'anyOf': [{'type': 'string'}, {'type': 'number'}]}
def test_v1_get_validators():
class CustomDate(date):
@classmethod
def __get_validators__(cls):
yield cls.validate1
yield cls.validate2
@classmethod
def validate1(cls, v, i):
print(v)
if v.year < 2000:
raise ValueError('Invalid year')
return v
@classmethod
def validate2(cls, v, i):
return date.today().replace(month=1, day=1)
with pytest.warns(
PydanticDeprecatedSince20,
match='^`__get_validators__` is deprecated and will be removed, use `__get_pydantic_core_schema__` instead.',
):
class Model(BaseModel):
x: CustomDate
with pytest.raises(ValidationError, match='Value error, Invalid year'):
Model(x=date(1999, 1, 1))
m = Model(x=date.today())
assert m.x.day == 1
def test_v1_get_validators_invalid_validator():
class InvalidValidator:
@classmethod
def __get_validators__(cls):
yield cls.has_wrong_arguments
@classmethod
def has_wrong_arguments(cls):
pass
with pytest.warns(
PydanticDeprecatedSince20,
match='^`__get_validators__` is deprecated and will be removed, use `__get_pydantic_core_schema__` instead.',
):
class InvalidValidatorModel(BaseModel):
x: InvalidValidator
with pytest.raises(TypeError, match='takes 1 positional argument but 3 were given'):
InvalidValidatorModel(x=1)
def test_field_extra_arguments():
m = re.escape(
'Using extra keyword arguments on `Field` is deprecated and will be removed. Use `json_schema_extra` instead. '
"(Extra keys: 'test', 'foo')"
)
with pytest.warns(PydanticDeprecatedSince20, match=m):
class Model(BaseModel):
x: str = Field('test', test='test', foo='bar')
assert Model.model_json_schema(by_alias=True)['properties'] == {
'x': {'default': 'test', 'foo': 'bar', 'test': 'test', 'title': 'X', 'type': 'string'}
}
def test_field_extra_does_not_rewrite_json_schema_extra():
m = 'Using extra keyword arguments on `Field` is deprecated and will be removed. Use `json_schema_extra` instead'
with pytest.warns(PydanticDeprecatedSince20, match=m):
class Model(BaseModel):
x: str = Field('test', test='test', json_schema_extra={'test': 'json_schema_extra value'})
assert Model.model_json_schema(by_alias=True)['properties'] == {
'x': {'default': 'test', 'test': 'json_schema_extra value', 'title': 'X', 'type': 'string'}
}
class SimpleModel(BaseModel):
x: int
def test_dict():
m = SimpleModel(x=1)
with pytest.warns(PydanticDeprecatedSince20, match=r'^The `dict` method is deprecated; use `model_dump` instead\.'):
assert m.dict() == {'x': 1}
def test_json():
m = SimpleModel(x=1)
with pytest.warns(
PydanticDeprecatedSince20, match=r'^The `json` method is deprecated; use `model_dump_json` instead\.'
):
assert m.json() == '{"x":1}'
with pytest.warns(PydanticDeprecatedSince20):
with pytest.raises(TypeError, match='The `encoder` argument is no longer supported'):
m.json(encoder=1)
with pytest.raises(TypeError, match='The `models_as_dict` argument is no longer supported'):
m.json(models_as_dict=True)
with pytest.raises(TypeError, match='`dumps_kwargs` keyword arguments are no longer supported.'):
m.json(foo=4)
def test_parse_obj():
with pytest.warns(
PydanticDeprecatedSince20, match='^The `parse_obj` method is deprecated; use `model_validate` instead.'
):
m = SimpleModel.parse_obj({'x': 1})
assert m.model_dump() == {'x': 1}
def test_parse_file(tmp_path):
path = tmp_path / 'test.json'
path.write_text('{"x": 12}')
with pytest.warns(PydanticDeprecatedSince20) as all_warnings:
assert SimpleModel.parse_file(str(path)).model_dump() == {'x': 12}
assert len(all_warnings) == 4
expected_warnings = [
'The `parse_file` method is deprecated; load the data from file, then if your data is JSON use `model_validate_json`, otherwise `model_validate` instead',
'`load_file` is deprecated',
'`load_str_bytes` is deprecated',
'The `parse_obj` method is deprecated; use `model_validate` instead',
]
assert [w.message.message for w in all_warnings] == expected_warnings
def test_construct():
with pytest.warns(
PydanticDeprecatedSince20, match='The `construct` method is deprecated; use `model_construct` instead.'
):
m = SimpleModel.construct(x='not an int')
assert m.x == 'not an int'
def test_json_schema():
m = SimpleModel(x=1)
with pytest.warns(
PydanticDeprecatedSince20, match='^The `schema` method is deprecated; use `model_json_schema` instead.'
):
assert m.schema() == {
'title': 'SimpleModel',
'type': 'object',
'properties': {'x': {'title': 'X', 'type': 'integer'}},
'required': ['x'],
}
def test_validate():
with pytest.warns(
PydanticDeprecatedSince20, match='^The `validate` method is deprecated; use `model_validate` instead.'
):
m = SimpleModel.validate({'x': 1})
assert m.model_dump() == {'x': 1}
def test_update_forward_refs():
with pytest.warns(PydanticDeprecatedSince20, match='^The `update_forward_refs` method is deprecated;'):
SimpleModel.update_forward_refs()
def test_copy_and_set_values():
m = SimpleModel(x=1)
with pytest.warns(
PydanticDeprecatedSince20, match='^The private method `_copy_and_set_values` will be removed and '
):
m2 = m._copy_and_set_values(values={'x': 2}, fields_set={'x'}, deep=False)
assert m2.x == 2
def test_get_value():
m = SimpleModel(x=1)
with pytest.warns(PydanticDeprecatedSince20, match='^The private method `_get_value` will be removed and '):
v = m._get_value(
[1, 2, 3],
to_dict=False,
by_alias=False,
include=None,
exclude=None,
exclude_unset=False,
exclude_defaults=False,
exclude_none=False,
)
assert v == [1, 2, 3]
def test_deprecated_module(tmp_path: Path) -> None:
class Model(BaseModel):
x: int
with pytest.warns(PydanticDeprecatedSince20) as all_warnings:
assert hasattr(parse_obj_as, '__deprecated__')
parse_obj_as(Model, {'x': 1})
assert hasattr(schema_json_of, '__deprecated__')
schema_json_of(Model)
assert hasattr(schema_of, '__deprecated__')
schema_of(Model)
assert hasattr(load_str_bytes, '__deprecated__')
load_str_bytes('{"x": 1}')
assert hasattr(load_file, '__deprecated__')
file = tmp_path / 'main.py'
file.write_text('{"x": 1}')
load_file(file)
assert hasattr(pydantic_encoder, '__deprecated__')
pydantic_encoder(Model(x=1))
assert hasattr(custom_pydantic_encoder, '__deprecated__')
custom_pydantic_encoder({int: lambda x: str(x)}, Model(x=1))
assert hasattr(timedelta_isoformat, '__deprecated__')
timedelta_isoformat(timedelta(seconds=1))
def test(a: int, b: int):
pass
validate_arguments()(test)
assert len(all_warnings) == 12
expected_warnings = [
'`parse_obj_as` is deprecated. Use `pydantic.TypeAdapter.validate_python` instead',
'`schema_json_of` is deprecated. Use `pydantic.TypeAdapter.json_schema` instead',
'`schema_of` is deprecated. Use `pydantic.TypeAdapter.json_schema` instead',
'`schema_of` is deprecated. Use `pydantic.TypeAdapter.json_schema` instead',
'`load_str_bytes` is deprecated',
'`load_file` is deprecated',
'`load_str_bytes` is deprecated',
'`pydantic_encoder` is deprecated, use `pydantic_core.to_jsonable_python` instead',
'`custom_pydantic_encoder` is deprecated, use `BaseModel.model_dump` instead',
'`pydantic_encoder` is deprecated, use `pydantic_core.to_jsonable_python` instead',
'`timedelta_isoformat` is deprecated',
'The `validate_arguments` method is deprecated; use `validate_call` instead',
]
assert [w.message.message for w in all_warnings] == expected_warnings
def test_deprecated_color():
from pydantic.color import Color
with pytest.warns(
PydanticDeprecatedSince20, match='The `Color` class is deprecated, use `pydantic_extra_types` instead.'
):
Color('red')
def test_deprecated_payment():
from pydantic import PaymentCardNumber
with pytest.warns(
PydanticDeprecatedSince20,
match='The `PaymentCardNumber` class is deprecated, use `pydantic_extra_types` instead.',
):
PaymentCardNumber('4242424242424242')
|