Browse Source

🐛 Fix support for Pydantic v2.0, small changes in their final release (#9771)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Sebastián Ramírez <[email protected]>
pull/9778/head
Pastukhov Nikita 2 years ago
committed by GitHub
parent
commit
d5952d6db5
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      fastapi/_compat.py
  2. 4
      tests/test_path.py
  3. 26
      tests/test_tutorial/test_body/test_tutorial001.py
  4. 26
      tests/test_tutorial/test_body/test_tutorial001_py310.py
  5. 2
      tests/test_tutorial/test_dataclasses/test_tutorial001.py

10
fastapi/_compat.py

@ -47,8 +47,6 @@ if PYDANTIC_V2:
from pydantic import PydanticSchemaGenerationError as PydanticSchemaGenerationError
from pydantic import TypeAdapter
from pydantic import ValidationError as ValidationError
from pydantic._internal._fields import Undefined as Undefined
from pydantic._internal._fields import _UndefinedType
from pydantic._internal._schema_generation_shared import ( # type: ignore[attr-defined]
GetJsonSchemaHandler as GetJsonSchemaHandler,
)
@ -58,15 +56,16 @@ if PYDANTIC_V2:
from pydantic.json_schema import GenerateJsonSchema as GenerateJsonSchema
from pydantic.json_schema import JsonSchemaValue as JsonSchemaValue
from pydantic_core import CoreSchema as CoreSchema
from pydantic_core import ErrorDetails
from pydantic_core import MultiHostUrl as MultiHostUrl
from pydantic_core import PydanticUndefined, PydanticUndefinedType
from pydantic_core import Url as Url
from pydantic_core.core_schema import (
general_plain_validator_function as general_plain_validator_function,
)
Required = Undefined
UndefinedType = _UndefinedType
Required = PydanticUndefined
Undefined = PydanticUndefined
UndefinedType = PydanticUndefinedType
evaluate_forwardref = eval_type_lenient
Validator = Any
@ -303,7 +302,6 @@ else:
lenient_issubclass as lenient_issubclass, # noqa: F401
)
ErrorDetails = Dict[str, Any] # type: ignore[assignment,misc]
GetJsonSchemaHandler = Any # type: ignore[assignment,misc]
JsonSchemaValue = Dict[str, Any] # type: ignore[misc]
CoreSchema = Any # type: ignore[assignment,misc]

4
tests/test_path.py

@ -145,7 +145,7 @@ def test_path_float_foobar():
{
"type": "float_parsing",
"loc": ["path", "item_id"],
"msg": "Input should be a valid number, unable to parse string as an number",
"msg": "Input should be a valid number, unable to parse string as a number",
"input": "foobar",
"url": match_pydantic_error_url("float_parsing"),
}
@ -174,7 +174,7 @@ def test_path_float_True():
{
"type": "float_parsing",
"loc": ["path", "item_id"],
"msg": "Input should be a valid number, unable to parse string as an number",
"msg": "Input should be a valid number, unable to parse string as a number",
"input": "True",
"url": match_pydantic_error_url("float_parsing"),
}

26
tests/test_tutorial/test_body/test_tutorial001.py

@ -101,7 +101,7 @@ def test_post_with_only_name_price(client: TestClient):
{
"type": "float_parsing",
"loc": ["body", "price"],
"msg": "Input should be a valid number, unable to parse string as an number",
"msg": "Input should be a valid number, unable to parse string as a number",
"input": "twenty",
"url": match_pydantic_error_url("float_parsing"),
}
@ -240,11 +240,11 @@ def test_post_form_for_json(client: TestClient):
{
"detail": [
{
"type": "dict_attributes_type",
"type": "model_attributes_type",
"loc": ["body"],
"msg": "Input should be a valid dictionary or instance to extract fields from",
"msg": "Input should be a valid dictionary or object to extract fields from",
"input": "name=Foo&price=50.5",
"url": match_pydantic_error_url("dict_attributes_type"),
"url": match_pydantic_error_url("model_attributes_type"),
}
]
}
@ -304,12 +304,12 @@ def test_wrong_headers(client: TestClient):
{
"detail": [
{
"type": "dict_attributes_type",
"type": "model_attributes_type",
"loc": ["body"],
"msg": "Input should be a valid dictionary or instance to extract fields from",
"msg": "Input should be a valid dictionary or object to extract fields from",
"input": '{"name": "Foo", "price": 50.5}',
"url": match_pydantic_error_url(
"dict_attributes_type"
"model_attributes_type"
), # "https://errors.pydantic.dev/0.38.0/v/dict_attributes_type",
}
]
@ -335,11 +335,11 @@ def test_wrong_headers(client: TestClient):
{
"detail": [
{
"type": "dict_attributes_type",
"type": "model_attributes_type",
"loc": ["body"],
"msg": "Input should be a valid dictionary or instance to extract fields from",
"msg": "Input should be a valid dictionary or object to extract fields from",
"input": '{"name": "Foo", "price": 50.5}',
"url": match_pydantic_error_url("dict_attributes_type"),
"url": match_pydantic_error_url("model_attributes_type"),
}
]
}
@ -363,11 +363,11 @@ def test_wrong_headers(client: TestClient):
{
"detail": [
{
"type": "dict_attributes_type",
"type": "model_attributes_type",
"loc": ["body"],
"msg": "Input should be a valid dictionary or instance to extract fields from",
"msg": "Input should be a valid dictionary or object to extract fields from",
"input": '{"name": "Foo", "price": 50.5}',
"url": match_pydantic_error_url("dict_attributes_type"),
"url": match_pydantic_error_url("model_attributes_type"),
}
]
}

26
tests/test_tutorial/test_body/test_tutorial001_py310.py

@ -109,7 +109,7 @@ def test_post_with_only_name_price(client: TestClient):
{
"type": "float_parsing",
"loc": ["body", "price"],
"msg": "Input should be a valid number, unable to parse string as an number",
"msg": "Input should be a valid number, unable to parse string as a number",
"input": "twenty",
"url": match_pydantic_error_url("float_parsing"),
}
@ -252,11 +252,11 @@ def test_post_form_for_json(client: TestClient):
{
"detail": [
{
"type": "dict_attributes_type",
"type": "model_attributes_type",
"loc": ["body"],
"msg": "Input should be a valid dictionary or instance to extract fields from",
"msg": "Input should be a valid dictionary or object to extract fields from",
"input": "name=Foo&price=50.5",
"url": match_pydantic_error_url("dict_attributes_type"),
"url": match_pydantic_error_url("model_attributes_type"),
}
]
}
@ -320,11 +320,11 @@ def test_wrong_headers(client: TestClient):
{
"detail": [
{
"type": "dict_attributes_type",
"type": "model_attributes_type",
"loc": ["body"],
"msg": "Input should be a valid dictionary or instance to extract fields from",
"msg": "Input should be a valid dictionary or object to extract fields from",
"input": '{"name": "Foo", "price": 50.5}',
"url": match_pydantic_error_url("dict_attributes_type"),
"url": match_pydantic_error_url("model_attributes_type"),
}
]
}
@ -349,11 +349,11 @@ def test_wrong_headers(client: TestClient):
{
"detail": [
{
"type": "dict_attributes_type",
"type": "model_attributes_type",
"loc": ["body"],
"msg": "Input should be a valid dictionary or instance to extract fields from",
"msg": "Input should be a valid dictionary or object to extract fields from",
"input": '{"name": "Foo", "price": 50.5}',
"url": match_pydantic_error_url("dict_attributes_type"),
"url": match_pydantic_error_url("model_attributes_type"),
}
]
}
@ -377,11 +377,11 @@ def test_wrong_headers(client: TestClient):
{
"detail": [
{
"type": "dict_attributes_type",
"type": "model_attributes_type",
"loc": ["body"],
"msg": "Input should be a valid dictionary or instance to extract fields from",
"msg": "Input should be a valid dictionary or object to extract fields from",
"input": '{"name": "Foo", "price": 50.5}',
"url": match_pydantic_error_url("dict_attributes_type"),
"url": match_pydantic_error_url("model_attributes_type"),
}
]
}

2
tests/test_tutorial/test_dataclasses/test_tutorial001.py

@ -27,7 +27,7 @@ def test_post_invalid_item():
{
"type": "float_parsing",
"loc": ["body", "price"],
"msg": "Input should be a valid number, unable to parse string as an number",
"msg": "Input should be a valid number, unable to parse string as a number",
"input": "invalid price",
"url": match_pydantic_error_url("float_parsing"),
}

Loading…
Cancel
Save