Skip to main content
Version: dev 🚧

AvroBase

fastkafka.encoder.AvroBase​

View source

This is base pydantic class that will add some methods

init​

__init__(
__pydantic_self__, data
)

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

__init__ uses __pydantic_self__ instead of the more common self for the first arg to allow self as a field name.

avro_schema​

View source
@classmethod
avro_schema(
by_alias=True, namespace=None
)

Returns the Avro schema for the Pydantic class.

Parameters:

NameTypeDescriptionDefault
by_aliasboolGenerate schemas using aliases defined. Defaults to True.True
namespaceOptional[str]Optional namespace string for schema generation.None

Returns:

TypeDescription
Dict[str, Any]The Avro schema for the model.

avro_schema_for_pydantic_class​

View source
@classmethod
avro_schema_for_pydantic_class(
pydantic_model, by_alias=True, namespace=None
)

Returns the Avro schema for the given Pydantic class.

Parameters:

NameTypeDescriptionDefault
pydantic_modelType[pydantic.main.BaseModel]The Pydantic class.required
by_aliasboolGenerate schemas using aliases defined. Defaults to True.True
namespaceOptional[str]Optional namespace string for schema generation.None

Returns:

TypeDescription
Dict[str, Any]The Avro schema for the model.

avro_schema_for_pydantic_object​

View source
@classmethod
avro_schema_for_pydantic_object(
pydantic_model, by_alias=True, namespace=None
)

Returns the Avro schema for the given Pydantic object.

Parameters:

NameTypeDescriptionDefault
pydantic_modelBaseModelThe Pydantic object.required
by_aliasboolGenerate schemas using aliases defined. Defaults to True.True
namespaceOptional[str]Optional namespace string for schema generation.None

Returns:

TypeDescription
Dict[str, Any]The Avro schema for the model.

construct​

@classmethod
construct(
_fields_set=None, values
)

copy​

copy(
self, include=None, exclude=None, update=None, deep=False
)

Returns a copy of the model.

!!! warning "Deprecated" This method is now deprecated; use model_copy instead.

If you need include or exclude, use:

data = self.model_dump(include=include, exclude=exclude, round_trip=True)
data = {**data, **(update or {})}
copied = self.model_validate(data)

Parameters:

NameTypeDescriptionDefault
includeAbstractSetIntStrMappingIntStrAnyNone
excludeAbstractSetIntStrMappingIntStrAnyNone
update`Dict[str, Any]None`Optional dictionary of field-value pairs to override field valuesin the copied model.
deepboolIf True, the values of fields that are Pydantic models will be deep copied.False

Returns:

TypeDescription
ModelA copy of the model with included, excluded and updated fields as specified.

dict​

dict(
self,
include=None,
exclude=None,
by_alias=False,
exclude_unset=False,
exclude_defaults=False,
exclude_none=False,
)

from_orm​

@classmethod
from_orm(
obj
)

json​

json(
self,
include=None,
exclude=None,
by_alias=False,
exclude_unset=False,
exclude_defaults=False,
exclude_none=False,
encoder=PydanticUndefined,
models_as_dict=PydanticUndefined,
dumps_kwargs,
)

model_computed_fields​

@property
model_computed_fields(
self
)

Get the computed fields of this model instance.

Returns:

TypeDescription
dict[str, ComputedFieldInfo]A dictionary of computed field names and their corresponding ComputedFieldInfo objects.

model_construct​

@classmethod
model_construct(
_fields_set=None, values
)

Creates a new instance of the Model class with validated data.

Creates a new model setting __dict__ and __pydantic_fields_set__ from trusted or pre-validated data. Default values are respected, but no other validation is performed. Behaves as if Config.extra = 'allow' was set since it adds all passed values

Parameters:

NameTypeDescriptionDefault
_fields_setset[str]NoneThe set of field names accepted for the Model instance.
valuesAnyTrusted or pre-validated data dictionary.required

Returns:

TypeDescription
ModelA new instance of the Model class with validated data.

model_copy​

model_copy(
self, update=None, deep=False
)

Usage docs: https://docs.pydantic.dev/2.2/usage/serialization/#model_copy

Returns a copy of the model.

Parameters:

NameTypeDescriptionDefault
updatedict[str, Any]NoneValues to change/add in the new model. Note: the data is not validatedbefore creating the new model. You should trust this data.
deepboolSet to True to make a deep copy of the model.False

Returns:

TypeDescription
ModelNew model instance.

model_dump​

model_dump(
self,
mode='python',
include=None,
exclude=None,
by_alias=False,
exclude_unset=False,
exclude_defaults=False,
exclude_none=False,
round_trip=False,
warnings=True,
)

Usage docs: https://docs.pydantic.dev/2.2/usage/serialization/#modelmodel_dump

Generate a dictionary representation of the model, optionally specifying which fields to include or exclude.

Parameters:

NameTypeDescriptionDefault
modeLiteral['json', 'python']strThe mode in which to_python should run.If mode is 'json', the dictionary will only contain JSON serializable types.If mode is 'python', the dictionary may contain any Python objects.
includeIncExA list of fields to include in the output.None
excludeIncExA list of fields to exclude from the output.None
by_aliasboolWhether to use the field's alias in the dictionary key if defined.False
exclude_unsetboolWhether to exclude fields that are unset or None from the output.False
exclude_defaultsboolWhether to exclude fields that are set to their default value from the output.False
exclude_noneboolWhether to exclude fields that have a value of None from the output.False
round_tripboolWhether to enable serialization and deserialization round-trip support.False
warningsboolWhether to log warnings when invalid fields are encountered.True

Returns:

TypeDescription
dict[str, Any]A dictionary representation of the model.

model_dump_json​

model_dump_json(
self,
indent=None,
include=None,
exclude=None,
by_alias=False,
exclude_unset=False,
exclude_defaults=False,
exclude_none=False,
round_trip=False,
warnings=True,
)

Usage docs: https://docs.pydantic.dev/2.2/usage/serialization/#modelmodel_dump_json

Generates a JSON representation of the model using Pydantic's to_json method.

Parameters:

NameTypeDescriptionDefault
indentintNoneIndentation to use in the JSON output. If None is passed, the output will be compact.
includeIncExField(s) to include in the JSON output. Can take either a string or set of strings.None
excludeIncExField(s) to exclude from the JSON output. Can take either a string or set of strings.None
by_aliasboolWhether to serialize using field aliases.False
exclude_unsetboolWhether to exclude fields that have not been explicitly set.False
exclude_defaultsboolWhether to exclude fields that have the default value.False
exclude_noneboolWhether to exclude fields that have a value of None.False
round_tripboolWhether to use serialization/deserialization between JSON and class instance.False
warningsboolWhether to show any warnings that occurred during serialization.True

Returns:

TypeDescription
strA JSON string representation of the model.

model_extra​

@property
model_extra(
self
)

Get extra fields set during validation.

Returns:

TypeDescription
`dict[str, Any]None`

model_fields_set​

@property
model_fields_set(
self
)

Returns the set of fields that have been set on this model instance.

Returns:

TypeDescription
set[str]A set of strings representing the fields that have been set,i.e. that were not filled from defaults.

model_json_schema​

@classmethod
model_json_schema(
by_alias=True,
ref_template='#/$defs/{model}',
schema_generator=<class 'pydantic.json_schema.GenerateJsonSchema'>,
mode='validation',
)

Generates a JSON schema for a model class.

Parameters:

NameTypeDescriptionDefault
by_aliasboolWhether to use attribute aliases or not.True
ref_templatestrThe reference template.'#/$defs/{model}'
schema_generatortype[GenerateJsonSchema]To override the logic used to generate the JSON schema, as a subclass ofGenerateJsonSchema with your desired modifications<class 'pydantic.json_schema.GenerateJsonSchema'>
modeJsonSchemaModeThe mode in which to generate the schema.'validation'

Returns:

TypeDescription
dict[str, Any]The JSON schema for the given model class.

model_parametrized_name​

@classmethod
model_parametrized_name(
params
)

Compute the class name for parametrizations of generic classes.

This method can be overridden to achieve a custom naming scheme for generic BaseModels.

Parameters:

NameTypeDescriptionDefault
paramstuple[type[Any], ...]Tuple of types of the class. Given a generic classModel with 2 type variables and a concrete model Model[str, int],the value (str, int) would be passed to params.required

Returns:

TypeDescription
strString representing the new class where params are passed to cls as type variables.

Exceptions:

TypeDescription
TypeErrorRaised when trying to generate concrete names for non-generic models.

model_post_init​

model_post_init(
self, _BaseModel__context
)

Override this method to perform additional initialization after __init__ and model_construct.

This is useful if you want to do some validation that requires the entire model to be initialized.

model_rebuild​

@classmethod
model_rebuild(
force=False,
raise_errors=True,
_parent_namespace_depth=2,
_types_namespace=None,
)

Try to rebuild the pydantic-core schema for the model.

This may be necessary when one of the annotations is a ForwardRef which could not be resolved during the initial attempt to build the schema, and automatic rebuilding fails.

Parameters:

NameTypeDescriptionDefault
forceboolWhether to force the rebuilding of the model schema, defaults to False.False
raise_errorsboolWhether to raise errors, defaults to True.True
_parent_namespace_depthintThe depth level of the parent namespace, defaults to 2.2
_types_namespacedict[str, Any]NoneThe types namespace, defaults to None.

Returns:

TypeDescription
`boolNone`

model_validate​

@classmethod
model_validate(
obj, strict=None, from_attributes=None, context=None
)

Validate a pydantic model instance.

Parameters:

NameTypeDescriptionDefault
objAnyThe object to validate.required
strictboolNoneWhether to raise an exception on invalid fields.
from_attributesboolNoneWhether to extract data from object attributes.
contextdict[str, Any]NoneAdditional context to pass to the validator.

Returns:

TypeDescription
ModelThe validated model instance.

Exceptions:

TypeDescription
ValidationErrorIf the object could not be validated.

model_validate_json​

@classmethod
model_validate_json(
json_data, strict=None, context=None
)

Validate the given JSON data against the Pydantic model.

Parameters:

NameTypeDescriptionDefault
json_datastrbytesbytearray
strictboolNoneWhether to enforce types strictly.
contextdict[str, Any]NoneExtra variables to pass to the validator.

Returns:

TypeDescription
ModelThe validated Pydantic model.

Exceptions:

TypeDescription
ValueErrorIf json_data is not a JSON string.

parse_file​

@classmethod
parse_file(
path,
content_type=None,
encoding='utf8',
proto=None,
allow_pickle=False,
)

parse_obj​

@classmethod
parse_obj(
obj
)

parse_raw​

@classmethod
parse_raw(
b,
content_type=None,
encoding='utf8',
proto=None,
allow_pickle=False,
)

schema​

@classmethod
schema(
by_alias=True, ref_template='#/$defs/{model}'
)

schema_json​

@classmethod
schema_json(
by_alias=True, ref_template='#/$defs/{model}', dumps_kwargs
)

update_forward_refs​

@classmethod
update_forward_refs(
localns
)

validate​

@classmethod
validate(
value
)