new·Earn with mozg — 20% of every monthSend somebody here and take a fifth of every plan payment they make, for as long as they keep paying — not a bounty on the first invoice. Your handle is the link, the window is thirty days, and the commission lands on your balance the second they pay. Free to join: if you have signed in, you already have the link. mozg.sh/earnall news →
mozg.beta
Sign in

Pydantic · all subjects

model_definition & inheritance

19 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

BaseModel method name changes: dict to model_dump

In Pydantic V1, the method `dict()` is used to convert a model to a dictionary. In Pydantic V2, this has been renamed to `model_dump()`.

BaseModel method name changes: json to model_dump_json

In Pydantic V1, the method `json()` is used to convert a model to JSON. In Pydantic V2, this has been renamed to `model_dump_json()`.

BaseModel method name changes: parse_obj to model_validate

In Pydantic V1, the method `parse_obj()` is used to parse data into a model. In Pydantic V2, this has been renamed to `model_validate()`.

BaseModel method name changes: parse_raw to model_validate_json

In Pydantic V1, the method `parse_raw()` is used to parse raw JSON data. In Pydantic V2, this has been replaced with `model_validate_json()`. The methods `parse_raw` and `parse_file` are now deprecated.

BaseModel method name changes: from_orm to model_validate with from_attributes

In Pydantic V1, the method `from_orm()` is used to create a model from ORM objects. In Pydantic V2, this method has been deprecated. Instead, use `model_validate()` with `from_attributes=True` set in the model config.

BaseModel method name changes: construct to model_construct

In Pydantic V1, the method `construct()` is used to create a model instance without validation. In Pydantic V2, this has been renamed to `model_construct()`.

BaseModel method name changes: copy to model_copy

In Pydantic V1, the method `copy()` is used to create a copy of a model instance. In Pydantic V2, this has been renamed to `model_copy()`.

BaseModel attribute name changes: __fields__ to model_fields

In Pydantic V1, `__fields__` is used to access model field definitions. In Pydantic V2, this has been renamed to `model_fields`.

BaseModel attribute name changes: __validators__ to __pydantic_validator__

In Pydantic V1, `__validators__` is used to access validators. In Pydantic V2, this has been renamed to `__pydantic_validator__`.

BaseModel attribute name changes: __private_attributes__ to __pydantic_private__

In Pydantic V1, `__private_attributes__` is used to access private attributes. In Pydantic V2, this has been renamed to `__pydantic_private__`.

Model equality comparison changes in V2

In Pydantic V2, model equality has changed: models can only be equal to other BaseModel instances; two models are equal if they have the same type, field values, extra values (when model_config['extra'] == 'allow'), and private attribute values; models are no longer equal to dicts containing their data; and models with different values of private attributes are no longer equal.

RootModel replaces __root__ field

In Pydantic V2, the `__root__` field for specifying custom root models has been replaced with a new `RootModel` type. RootModel types no longer support the `arbitrary_types_allowed` config setting.

GetterDict removed in V2

GetterDict has been removed in Pydantic V2 as it was an implementation detail of orm_mode, which has been removed.

Constructor arguments copied for validation in V2

In Pydantic V2, arguments passed to the constructor are copied in order to perform validation and coercion. This is notable when passing mutable objects as arguments, and differs from V1 behavior.

GenericModel removed, use Generic with BaseModel

In Pydantic V2, the `pydantic.generics.GenericModel` class is removed. Instead, create generic BaseModel subclasses by adding Generic as a parent class: `class MyGenericModel(BaseModel, Generic[T]): ...`

Do not use parametrized generics in isinstance checks

In Pydantic V2, avoid using parametrized generics in isinstance checks (e.g., `isinstance(my_model, MyGenericModel[int])`). Use the non-parametrized class instead (e.g., `isinstance(my_model, MyGenericModel)`). If parametrized isinstance checks are needed, subclass the parametrized generic.

Dataclass as field no longer accepts tuples

In Pydantic V2, when dataclasses (Pydantic or vanilla) are used as fields, they no longer accept tuples as validation inputs; dicts should be used instead.

__post_init__ called after validation in V2 dataclasses

In Pydantic V2 dataclasses, `__post_init__` is called after validation, unlike in V1 where it was called before. The `__post_init_post_parse__` method has been removed as it is now redundant.

Pydantic dataclasses no longer use underlying BaseModel

In Pydantic V2, dataclasses no longer have a `__pydantic_model__` attribute and no longer use an underlying BaseModel. Use TypeAdapter to wrap the dataclass to perform validation, generate JSON schema, or access other functionality that previously required `__pydantic_model__`.

Give your agent this brain