Skip to content

Commit

Permalink
⬆ Upgrade pydantic-core to v2.2.0 (#6589)
Browse files Browse the repository at this point in the history
  • Loading branch information
lig committed Jul 11, 2023
1 parent a2f266a commit 47c5c63
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 187 deletions.
2 changes: 1 addition & 1 deletion docs/errors/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ except ValidationError as e:
'loc': ('foo',),
'msg': 'Value error, value must be "bar"',
'input': 'ber',
'ctx': {'error': 'value must be "bar"'},
'ctx': {'error': ValueError('value must be "bar"')},
'url': 'https://errors.pydantic.dev/2/v/value_error',
}
]
Expand Down
210 changes: 105 additions & 105 deletions pdm.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ requires-python = '>=3.7'
dependencies = [
'typing-extensions>=4.6.1',
'annotated-types>=0.4.0',
'pydantic-core==2.1.2',
"pydantic-core==2.2.0",
]
dynamic = ['version', 'readme']

Expand Down
37 changes: 25 additions & 12 deletions tests/test_assert_in_validators.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
"""
PYTEST_DONT_REWRITE
"""
import difflib
import pprint

import pytest
from dirty_equals import HasRepr

from pydantic import BaseModel, ValidationError, field_validator


def _pformat_lines(obj):
return pprint.pformat(obj).splitlines(keepends=True)


def _assert_eq(left, right):
if left != right:
pytest.fail('\n' + '\n'.join(difflib.ndiff(_pformat_lines(left), _pformat_lines(right))))


def test_assert_raises_validation_error():
class Model(BaseModel):
a: str
Expand All @@ -21,15 +34,15 @@ def check_a(cls, v):
with pytest.raises(ValidationError) as exc_info:
Model(a='snap')

expected_errors = [
{
'type': 'assertion_error',
'loc': ('a',),
'msg': 'Assertion failed, invalid a',
'input': 'snap',
'ctx': {'error': 'invalid a'},
}
]
actual_errors = exc_info.value.errors(include_url=False)
if expected_errors != actual_errors:
pytest.fail(f'Actual errors: {actual_errors}\nExpected errors: {expected_errors}')
_assert_eq(
[
{
'ctx': {'error': HasRepr(repr(AssertionError('invalid a')))},
'input': 'snap',
'loc': ('a',),
'msg': 'Assertion failed, invalid a',
'type': 'assertion_error',
}
],
exc_info.value.errors(include_url=False),
)
2 changes: 1 addition & 1 deletion tests/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1985,7 +1985,7 @@ class ValidatingModel(Model):
ValidatingModel()
assert exc_info.value.errors(include_url=False) == [
{
'ctx': {'error': 'assert -1 > 0'},
'ctx': {'error': HasRepr(repr(AssertionError('assert -1 > 0')))},
'input': -1,
'loc': ('x',),
'msg': 'Assertion failed, assert -1 > 0',
Expand Down
1 change: 1 addition & 0 deletions tests/test_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ def test_error_codes():
assert code_error_codes == documented_error_codes, 'Error codes in code and docs do not match'


@pytest.mark.xfail(reason='`enum` is not documented')
def test_validation_error_codes():
error_text = (DOCS_ROOT / 'usage/validation_errors.md').read_text()

Expand Down
26 changes: 13 additions & 13 deletions tests/test_generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,23 +130,23 @@ def validate_sum(cls, m):
Response[Dict[int, int]](data={1: 0})
assert exc_info.value.errors(include_url=False) == [
{
'type': 'value_error',
'ctx': {'error': HasRepr(repr(ValueError('some value is zero')))},
'input': {1: 0},
'loc': ('data',),
'msg': 'Value error, some value is zero',
'input': {1: 0},
'ctx': {'error': 'some value is zero'},
'type': 'value_error',
}
]

with pytest.raises(ValidationError) as exc_info:
Response[Dict[int, int]](data={1: 3, 2: 6})
assert exc_info.value.errors(include_url=False) == [
{
'type': 'value_error',
'ctx': {'error': HasRepr(repr(ValueError('sum too large')))},
'input': {'data': {1: 3, 2: 6}},
'loc': (),
'msg': 'Value error, sum too large',
'input': {'data': {1: 3, 2: 6}},
'ctx': {'error': 'sum too large'},
'type': 'value_error',
}
]

Expand Down Expand Up @@ -515,23 +515,23 @@ class Data(BaseModel):
Result[Data, Error](error=Error(message='error'), positive_number=-1)
assert exc_info.value.errors(include_url=False) == [
{
'type': 'value_error',
'loc': ('positive_number',),
'msg': 'Value error, Unknown error',
'ctx': {'error': HasRepr(repr(ValueError()))},
'input': -1,
'ctx': {'error': 'Unknown error'},
'loc': ('positive_number',),
'msg': 'Value error, ',
'type': 'value_error',
}
]

with pytest.raises(ValidationError) as exc_info:
Result[Data, Error](data=[Data(number=1, text='a')], error=Error(message='error'), positive_number=1)
assert exc_info.value.errors(include_url=False) == [
{
'type': 'value_error',
'ctx': {'error': HasRepr(repr(ValueError('Must not provide both data and error')))},
'input': Error(message='error'),
'loc': ('error',),
'msg': 'Value error, Must not provide both data and error',
'input': Error(message='error'),
'ctx': {'error': 'Must not provide both data and error'},
'type': 'value_error',
}
]

Expand Down
3 changes: 2 additions & 1 deletion tests/test_json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from uuid import UUID

import pytest
from dirty_equals import HasRepr
from pydantic_core import CoreSchema, SchemaValidator, core_schema, to_json
from typing_extensions import Annotated, Literal, TypedDict

Expand Down Expand Up @@ -1930,7 +1931,7 @@ def check_something(cls, v):
Model(something='hellox')
assert exc_info.value.errors(include_url=False) == [
{
'ctx': {'error': 'should not contain x'},
'ctx': {'error': HasRepr(repr(ValueError('should not contain x')))},
'input': 'hellox',
'loc': ('something',),
'msg': 'Value error, should not contain x',
Expand Down
9 changes: 2 additions & 7 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2599,15 +2599,10 @@ class Model(BaseModel):
Model(v=True)


def test_int_parsing_size_error():
def test_int_parsing_size():
i64_max = 9_223_372_036_854_775_807
v = TypeAdapter(int)

with pytest.raises(
ValidationError,
match=r'Unable to parse input string as an integer, exceeded maximum size \[type=int_parsing_size,',
):
v.validate_json(json.dumps(-i64_max * 2))
assert v.validate_json(json.dumps(-i64_max * 2)) == -18_446_744_073_709_551_614


def test_strict_float():
Expand Down

0 comments on commit 47c5c63

Please sign in to comment.