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>
220 lines
3.6 KiB
Python
220 lines
3.6 KiB
Python
from typing import ClassVar, Generic, List, Optional, TypeVar, Union
|
|
|
|
from pydantic import BaseModel, Field, create_model, validator
|
|
from pydantic.dataclasses import dataclass
|
|
from pydantic.generics import GenericModel
|
|
|
|
|
|
class Model(BaseModel):
|
|
x: float
|
|
y: str
|
|
|
|
class Config:
|
|
orm_mode = True
|
|
|
|
class NotConfig:
|
|
allow_mutation = False
|
|
|
|
|
|
class SelfReferencingModel(BaseModel):
|
|
submodel: Optional['SelfReferencingModel']
|
|
|
|
@property
|
|
def prop(self) -> None:
|
|
...
|
|
|
|
|
|
SelfReferencingModel.model_rebuild()
|
|
|
|
model = Model(x=1, y='y')
|
|
Model(x=1, y='y', z='z')
|
|
model.x = 2
|
|
model.from_orm(model)
|
|
|
|
self_referencing_model = SelfReferencingModel(submodel=SelfReferencingModel(submodel=None))
|
|
|
|
|
|
class InheritingModel(Model):
|
|
z: int = 1
|
|
|
|
|
|
InheritingModel.from_orm(model)
|
|
|
|
|
|
class ForwardReferencingModel(Model):
|
|
future: 'FutureModel'
|
|
|
|
|
|
class FutureModel(Model):
|
|
pass
|
|
|
|
|
|
ForwardReferencingModel.model_rebuild()
|
|
future_model = FutureModel(x=1, y='a')
|
|
forward_model = ForwardReferencingModel(x=1, y='a', future=future_model)
|
|
|
|
|
|
class NoMutationModel(BaseModel):
|
|
x: int
|
|
|
|
class Config:
|
|
allow_mutation = False
|
|
|
|
|
|
class MutationModel(NoMutationModel):
|
|
a = 1
|
|
|
|
class Config:
|
|
allow_mutation = True
|
|
orm_mode = True
|
|
|
|
|
|
MutationModel(x=1).x = 2
|
|
MutationModel.from_orm(model)
|
|
|
|
|
|
class OverrideModel(Model):
|
|
x: int
|
|
|
|
|
|
OverrideModel(x=1.5, y='b')
|
|
|
|
|
|
class Mixin:
|
|
def f(self) -> None:
|
|
pass
|
|
|
|
|
|
class MultiInheritanceModel(BaseModel, Mixin):
|
|
pass
|
|
|
|
|
|
MultiInheritanceModel().f()
|
|
|
|
|
|
class AliasModel(BaseModel):
|
|
x: str = Field(..., alias='y')
|
|
|
|
|
|
alias_model = AliasModel(y='hello')
|
|
assert alias_model.x == 'hello'
|
|
|
|
|
|
class ClassVarModel(BaseModel):
|
|
x: int
|
|
y: ClassVar[int] = 1
|
|
|
|
|
|
ClassVarModel(x=1)
|
|
|
|
|
|
class Config:
|
|
validate_assignment = True
|
|
|
|
|
|
@dataclass(config=Config)
|
|
class AddProject:
|
|
name: str
|
|
slug: Optional[str]
|
|
description: Optional[str]
|
|
|
|
|
|
p = AddProject(name='x', slug='y', description='z')
|
|
|
|
|
|
class TypeAliasAsAttribute(BaseModel):
|
|
__type_alias_attribute__ = Union[str, bytes]
|
|
|
|
|
|
class NestedModel(BaseModel):
|
|
class Model(BaseModel):
|
|
id: str
|
|
|
|
model: Model
|
|
|
|
|
|
_ = NestedModel.Model
|
|
|
|
|
|
DynamicModel = create_model('DynamicModel', __base__=Model)
|
|
|
|
dynamic_model = DynamicModel(x=1, y='y')
|
|
dynamic_model.x = 2
|
|
|
|
|
|
class FrozenModel(BaseModel):
|
|
x: int
|
|
|
|
class Config:
|
|
frozen = True
|
|
|
|
|
|
class NotFrozenModel(FrozenModel):
|
|
a: int = 1
|
|
|
|
class Config:
|
|
frozen = False
|
|
orm_mode = True
|
|
|
|
|
|
NotFrozenModel(x=1).x = 2
|
|
NotFrozenModel.from_orm(model)
|
|
|
|
|
|
class ModelWithSelfField(BaseModel):
|
|
self: str
|
|
|
|
|
|
def f(name: str) -> str:
|
|
return name
|
|
|
|
|
|
class ModelWithAllowReuseValidator(BaseModel):
|
|
name: str
|
|
_normalize_name = validator('name', allow_reuse=True)(f)
|
|
|
|
|
|
model_with_allow_reuse_validator = ModelWithAllowReuseValidator(name='xyz')
|
|
|
|
|
|
T = TypeVar('T')
|
|
|
|
|
|
class Response(GenericModel, Generic[T]):
|
|
data: T
|
|
error: Optional[str]
|
|
|
|
|
|
response = Response[Model](data=model, error=None)
|
|
|
|
|
|
class ModelWithAnnotatedValidator(BaseModel):
|
|
name: str
|
|
|
|
@validator('name')
|
|
def noop_validator_with_annotations(cls, name: str) -> str:
|
|
return name
|
|
|
|
|
|
def _default_factory_str() -> str:
|
|
...
|
|
|
|
|
|
def _default_factory_list() -> List[int]:
|
|
...
|
|
|
|
|
|
class FieldDefaultTestingModel(BaseModel):
|
|
# Required
|
|
a: int
|
|
b: int = Field()
|
|
c: int = Field(...)
|
|
|
|
# Default
|
|
d: int = Field(1)
|
|
|
|
# Default factory
|
|
g: List[int] = Field(default_factory=_default_factory_list)
|
|
h: str = Field(default_factory=_default_factory_str)
|
|
i: str = Field(default_factory=lambda: 'test')
|