Browse Source

python 3.8 and pydantic 1 compat

pull/13464/head
sneakers-the-rat 3 months ago
parent
commit
a2ad8b187f
No known key found for this signature in database GPG Key ID: 6DCB96EF1E4D232D
  1. 34
      tests/test_forms_defaults.py

34
tests/test_forms_defaults.py

@ -1,17 +1,21 @@
from typing import Annotated, Optional from importlib.metadata import version
from typing import Optional
import pytest import pytest
from fastapi import FastAPI, Form from fastapi import FastAPI, Form
from pydantic import BaseModel, Field, model_validator from pydantic import BaseModel, Field
from starlette.testclient import TestClient from starlette.testclient import TestClient
from typing_extensions import Annotated
PYDANTIC_V2 = int(version("pydantic")[0]) >= 2
class Parent(BaseModel): if PYDANTIC_V2:
init_input: dict from pydantic import model_validator
# importantly, no default here else:
from pydantic import root_validator
@model_validator(mode="before")
def validate_inputs(cls, value: dict) -> dict: def _validate_input(value: dict) -> dict:
""" """
model validators in before mode should receive values passed model validators in before mode should receive values passed
to model instantiation before any further validation to model instantiation before any further validation
@ -26,6 +30,22 @@ class Parent(BaseModel):
return value return value
class Parent(BaseModel):
init_input: dict
# importantly, no default here
if PYDANTIC_V2:
@model_validator(mode="before")
def validate_inputs(cls, value: dict) -> dict:
return _validate_input(value)
else:
@root_validator(pre=True)
def validate_inputs(cls, value: dict) -> dict:
return _validate_input(value)
class StandardModel(Parent): class StandardModel(Parent):
default_true: bool = True default_true: bool = True
default_false: bool = False default_false: bool = False

Loading…
Cancel
Save