mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
594effa279
* working on core schema generation * adapting main.py * getting tests to run * fix tests * disable pyright, fix mypy * moving to class-based model generation * working on validators * change how models are created * start fixing test_main.py * fixing mypy * SelfType * recursive models working, more tests fixed * fix tests on <3.10 * get docs build to pass * starting to cleanup types.py * starting works on custom types * working on using annotated-types * using annoated types for constraints * lots of cleanup, fixing network tests * network tests passing 🎉 * working on types * working on types and cleanup * fixing UUID type, restructing again * more types and newer pydantic-core * working on Iterable * more test_types tests * support newer pydantic-core, fixing more test_types.py * working through more test_types.py * test_types.py at last passing locally 🎉 * fixing more tests in test_types.py * fix datetime_parse tests and linting * get tests running again, rename to test_datetime.py * renaming internal modules * working through mypy errors * fixing mypy * refactoring _generate_schema.py * test_main.py passing * uprev deps * fix conftest and linting? * importing Annotated * ltining * import Annotated from typing_extensions * fixing 3.7 compatibility * fixing tests on 3.9 * fix linting * fixing SecretField and 3.9 tests * customising get_type_hints * ignore warnings on 3.11 * spliting repr out of utils * removing unused bits of _repr, fix tests for 3.7 * more cleanup, removing many type aliases * clean up repr * support namedtuples and typeddicts * test is_union * removing errors, uprev pydantic-core * fix tests on 3.8 * fixing private attributes and model_post_init * renaming and cleanup * remove unnecessary PydanticMetadata inheritance * fixing forward refs and mypy tests * fix signatures, change how xfail works * revert mypy tests to 3.7 syntax * correct model title * try to fix tests * fixing ClassVar forward refs * uprev pydantic-core, new error format * add "force" argument to model_rebuild * Apply suggestions from code review Suggestions from @tiangolo and @hramezani 🙏 Co-authored-by: Hasan Ramezani <hasan.r67@gmail.com> Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com> * more suggestions from @tiangolo * extra -> json_schema_extra on Field Co-authored-by: Hasan Ramezani <hasan.r67@gmail.com> Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
95 lines
2.6 KiB
Python
95 lines
2.6 KiB
Python
from typing import Dict, List, Mapping, Union
|
|
|
|
import pytest
|
|
|
|
from pydantic import BaseModel, ValidationError
|
|
from pydantic.dataclasses import dataclass
|
|
from pydantic.tools import parse_obj_as, schema_json_of, schema_of
|
|
|
|
pytestmark = pytest.mark.xfail(reason='working on V2', strict=False)
|
|
|
|
|
|
@pytest.mark.parametrize('obj,type_,parsed', [('1', int, 1), (['1'], List[int], [1])])
|
|
def test_parse_obj(obj, type_, parsed):
|
|
assert parse_obj_as(type_, obj) == parsed
|
|
|
|
|
|
def test_parse_obj_as_model():
|
|
class Model(BaseModel):
|
|
x: int
|
|
y: bool
|
|
z: str
|
|
|
|
model_inputs = {'x': '1', 'y': 'true', 'z': 'abc'}
|
|
assert parse_obj_as(Model, model_inputs) == Model(**model_inputs)
|
|
|
|
|
|
def test_parse_obj_preserves_subclasses():
|
|
class ModelA(BaseModel):
|
|
a: Mapping[int, str]
|
|
|
|
class ModelB(ModelA):
|
|
b: int
|
|
|
|
model_b = ModelB(a={1: 'f'}, b=2)
|
|
|
|
parsed = parse_obj_as(List[ModelA], [model_b])
|
|
assert parsed == [model_b]
|
|
|
|
|
|
def test_parse_obj_fails():
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
parse_obj_as(int, 'a')
|
|
assert exc_info.value.errors() == [
|
|
{'loc': ('__root__',), 'msg': 'value is not a valid integer', 'type': 'type_error.integer'}
|
|
]
|
|
assert exc_info.value.model.__name__ == 'ParsingModel[int]'
|
|
|
|
|
|
def test_parsing_model_naming():
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
parse_obj_as(int, 'a')
|
|
assert str(exc_info.value).split('\n')[0] == '1 validation error for ParsingModel[int]'
|
|
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
parse_obj_as(int, 'a', type_name='ParsingModel')
|
|
assert str(exc_info.value).split('\n')[0] == '1 validation error for ParsingModel'
|
|
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
parse_obj_as(int, 'a', type_name=lambda type_: type_.__name__)
|
|
assert str(exc_info.value).split('\n')[0] == '1 validation error for int'
|
|
|
|
|
|
def test_parse_as_dataclass():
|
|
@dataclass
|
|
class PydanticDataclass:
|
|
x: int
|
|
|
|
inputs = {'x': '1'}
|
|
assert parse_obj_as(PydanticDataclass, inputs) == PydanticDataclass(1)
|
|
|
|
|
|
def test_parse_mapping_as():
|
|
inputs = {'1': '2'}
|
|
assert parse_obj_as(Dict[int, int], inputs) == {1: 2}
|
|
|
|
|
|
def test_schema():
|
|
assert schema_of(Union[int, str], title='IntOrStr') == {
|
|
'title': 'IntOrStr',
|
|
'anyOf': [{'type': 'integer'}, {'type': 'string'}],
|
|
}
|
|
assert schema_json_of(Union[int, str], title='IntOrStr', indent=2) == (
|
|
'{\n'
|
|
' "title": "IntOrStr",\n'
|
|
' "anyOf": [\n'
|
|
' {\n'
|
|
' "type": "integer"\n'
|
|
' },\n'
|
|
' {\n'
|
|
' "type": "string"\n'
|
|
' }\n'
|
|
' ]\n'
|
|
'}'
|
|
)
|