Fields
plateforme.core.schema.fields
This module provides utilities for managing fields information for configuration wrappers, models and resources within the Plateforme framework using Pydantic features.
The FieldInfo
class is an extension of Pydantic's field information, it
incorporates additional parameters like unique
, linked
, target
, and
others, enabling the definition of fields with extended behaviors specific to
the Plateforme framework.
The Field
and ConfigField
function creates and extends field information
with Pydantic's validation system, it allows the definition of configuration
wrappers, models and resources attributes with additional behaviors specific to
the Plateforme framework.
Examples:
>>> from plateforme import BaseModel, Field
...
>>> class FooModel(BaseModel):
... name: str = Field(
... ...,
... unique=True,
... title='Foo Model',
... description='Name of the foo instance',
... )
...
>>> class BarModel(BaseModel):
... name: str = Field(
... ...,
... unique=True,
... title='Bar Model',
... description='Name of the bar instance',
... )
... foo: FooModel | None = Field(title='Foo Reference')
Note
See also thenBaseModel
and BaseResource
for more information on data
modeling features, including associations between models implemented into
the field information.
SourceFieldInfoDict
Bases: TypedDict
, Generic[Object]
A source field information dictionary.
A dictionary that holds the source configuration parameters for a field information instance. It is not meant to be exposed for parameterization to the user directly.
source
instance-attribute
source: str | None
The source representation of the field definition.
Defaults to None
.
owner
instance-attribute
owner: type[Object] | None
The owner of the field. It is inferred from the class where the field is
defined if available else it defaults to None
.
name
instance-attribute
name: str | None
The name of the field. It must adhere to a specific ALIAS
pattern as
defined in the framework's regular expressions repository.
Defaults to None
.
annotation
instance-attribute
The type annotation of the field. Defaults to Undefined
.
default
instance-attribute
default: Any
The default value of the field. If not set and no default factory is
provided, the field is considered required. Defaults to Undefined
.
collection
instance-attribute
collection: Literal['list', 'set'] | None
The collection type of the target association. It can be either an
unconstrained collection list
, or a collection of unique elements
set
, otherwise it defaults to None
if the association is not a
collection.
target
instance-attribute
target: ResourceType | str | None
The linked field target BaseResource
object type.
Defaults to None
.
association
instance-attribute
association: Association | None
The Association
object that defines the association between the owner
and target resources. Defaults to None
.
BaseFieldInfoDict
Bases: TypedDict
A base field information dictionary.
A dictionary that holds the base configuration parameters for a field information instance. It includes the basic parameters that are common to all field information instances.
default_factory
instance-attribute
The factory function used to construct the default value of the field.
If both the default value and the default factory are set, an error is
raised. Defaults to None
.
alias
instance-attribute
alias: str | None
The alias name of the field. It must adhere to a specific ALIAS
pattern as defined in the framework's regular expressions repository.
Defaults to None
.
alias_priority
instance-attribute
alias_priority: int | None
The priority of the field's alias. This affects whether an alias
generator is used or not. Defaults to None
.
title
instance-attribute
title: str | None
The human-readable name of the field. It must adhere to a specific
TITLE
pattern as defined in the framework's regular expressions
repository. Defaults to None
.
description
instance-attribute
description: str | None
The description of the field. Defaults to None
and uses the
function's docstring if available.
examples
instance-attribute
List of examples of the field. Defaults to None
.
deprecated
instance-attribute
A deprecation message, an instance of Deprecated
, or a boolean. If
True
, a default deprecation message will be emitted when accessing the
field. Defaults to None
.
repr
instance-attribute
repr: bool
Whether or not to include the field in the representation.
Defaults to True
.
init
instance-attribute
init: bool
Whether the field should be included in the constructor of the
dataclass. Defaults to True
.
init_var
instance-attribute
init_var: bool | None
Whether the field should only be included in the constructor of the
dataclass, and not stored. Defaults to None
.
kw_only
instance-attribute
kw_only: bool | None
Whether or not the field should be a keyword-only argument in the
constructor of the model. Defaults to None
.
ModelFieldInfoDict
Bases: TypedDict
A model field information dictionary.
A dictionary that holds the model configuration parameters for a field information instance. It includes additional parameters that are specific to Pydantic field information.
validation_alias
instance-attribute
validation_alias: str | AliasPath | AliasChoices | None
The validation alias name of the field. It must adhere to a specific
ALIAS
pattern as defined in the framework's regular expressions
repository. Defaults to None
(alias name of the field).
serialization_alias
instance-attribute
serialization_alias: str | None
The serialization alias name of the field. It must adhere to a specific
ALIAS
pattern as defined in the framework's regular expressions
repository. Defaults to None
(alias name of the field).
exclude
instance-attribute
exclude: bool | None
Whether or not to exclude the field from the model serialization.
Defaults to None
.
discriminator
instance-attribute
discriminator: str | Discriminator | None
Field name for discriminating the type in a tagged union.
Defaults to None
.
json_schema_extra
instance-attribute
json_schema_extra: JsonSchemaExtra | None
Dictionary or callable to provide extra JSON schema properties.
Defaults to None
.
validate_default
instance-attribute
validate_default: bool | None
Whether or not to validate the default value of the field.
Defaults to None
.
pattern
instance-attribute
pattern: str | None
A regular expression pattern that the field value must match.
Defaults to None
.
strict
instance-attribute
strict: bool | None
Whether or not to enforce strict validation of the field value.
Defaults to None
.
multiple_of
instance-attribute
multiple_of: float | None
The value must be a multiple of this number. Defaults to None
.
allow_inf_nan
instance-attribute
allow_inf_nan: bool | None
Whether or not to allow infinity and NaN values.
Defaults to None
.
max_digits
instance-attribute
max_digits: int | None
The maximum number of digits in the field value.
Defaults to None
.
decimal_places
instance-attribute
decimal_places: int | None
The number of decimal places in the field value.
Defaults to None
.
min_length
instance-attribute
min_length: int | None
The minimum length of the field value. Defaults to None
.
max_length
instance-attribute
max_length: int | None
The maximum length of the field value. Defaults to None
.
union_mode
instance-attribute
union_mode: Literal['smart', 'left_to_right'] | None
The union mode for the field value. Defaults to None
.
recursive_guard
instance-attribute
recursive_guard: str | None
A recursive guard to handle infinite recursion when validating and
serializing fields. Defaults to None
.
ResourceFieldInfoDict
Bases: TypedDict
A resource field information dictionary.
A dictionary that holds the resource configuration parameters for a field information instance. It includes additional parameters that are specific to resource field information within the Plateforme framework.
slug
instance-attribute
slug: str | None
The slug name of the field. It must adhere to a specific SLUG
pattern as defined in the framework's regular expressions repository.
Defaults to None
.
indexed
instance-attribute
indexed: bool | None
Whether or not the field is indexed. Defaults to None
.
target_ref
instance-attribute
target_ref: bool | None
Whether the field can accept a reference to the target resource instead
of the resource schema model itself. Defaults to None
.
target_schemas
instance-attribute
Either a tuple of the linked field target DiscriminatedModel
schema
models to use for the annotation resolution, or a boolean to indicate
whether the target schema models should be resolved.
Defaults to None
.
association_alias
instance-attribute
association_alias: str | None
The association alias identifier. By default, this is determined by
concatenating the owner and target resource alias with an underscore (see
the Association
class). Defaults to None
.
rel_attribute
instance-attribute
rel_attribute: str | None
The attribute name of the foreign key column implemented within the
owner resource. This is optional and only applies in a scenario where a
foreign key column is required for managing the association. If the field
is not linked or the association (like a many-to-many association) does not
require a direct foreign key column, this parameter defaults to None
.
Conversely, if a link exists and the association necessitates a foreign key
column (as per the two-way association analysis), this parameter defaults
to the alias of the linked field, appended with _id
.
Defaults to None
.
rel_backref
instance-attribute
rel_backref: BackrefFieldInfoDict | None
The corresponding back referencing target linked field counterpart of
the association. This argument can be used to define a back reference
configuration using a BackrefFieldInfoDict
dictionary when no counterpart
exists in the target resource. This is useful in scenarios where there is
no control over the target resource definition, and the back reference must
be implemented in the owner resource. Otherwise, it is recommended to
define the back reference directly in the target resource.
If the target resource does not have a counterpart linked field defined for
the given association, the field information attribute initializes to
None
, otherwise it stores the target counterpart ResourceFieldInfo
instance, either generated from the target resourcedefinition, or by the
provided back reference dictionary configuration. Defaults to None
.
rel_cascade
instance-attribute
The cascade options for the relationship. The possible cascading options
are 'delete'
, 'delete-orphan'
, 'expunge'
, 'merge'
,
'refresh-expire'
, 'save-update'
, and all
, where the latter is a
shorthand for 'save-update, merge, refresh-expire, expunge, delete'
,
and is often used as in 'all, delete-orphan'
to indicate that related
objects should follow along with the parent object in all cases, and be
deleted when de-associated.
Defaults to None
for non-linked fields, otherwise True
for linked
fields which resolves to:
- 'save-update, merge'
when the association is bidirectional,
- '..., delete, delete-orphan'
when the target resource link is
identifying,
- '..., refresh-expire, expunge'
when the target resource link is
indexing.
rel_load
instance-attribute
The lazy loading options for the relationship.
Defaults to None
for non-linked fields, otherwise True
for linked
fields which resolves to:
- 'joined'
when the association is indexing one of the resources,
- 'selectin'
when the resource owner field is not a collection,
- 'select'
otherwise (lazy loading).
rel_extra
instance-attribute
Additional association relationship configuration parameters.
Defaults to None
.
column_extra
instance-attribute
Additional column configuration parameters. Defaults to None
.
data_type
instance-attribute
data_type: TypeEngine | None
The data type engine to use for the field. It is automatically inferred
from the field annotation if not explicitly set. Defaults to None
.
data_collation
instance-attribute
data_collation: str | None
The collation of the field used for strings within the database.
Defaults to None
.
data_none_as_null
instance-attribute
data_none_as_null: bool | None
Whether or not to treat None
as NULL
in the database for JSON
data types. Defaults to None
.
BackrefFieldInfoDict
Bases: TypedDict
A backref field information configuration dictionary.
A dictionary that holds the configuration parameters for a field information back reference instance. It is used to define the keyword arguments when creating a field information in a scenario where the target resource does not have a counterpart linked field for a given association.
name
instance-attribute
name: str | None
The name of the field. It must adhere to a specific ALIAS
pattern as
defined in the framework's regular expressions repository.
Defaults to None
.
default
instance-attribute
default: Any
The default value of the field. If not set and no default factory is
provided, the field is considered required. Defaults to Undefined
.
default_factory
instance-attribute
The factory function used to construct the default value of the field.
If both the default value and the default factory are set, an error is
raised. Defaults to None
.
alias
instance-attribute
alias: str | None
The alias name of the field. It must adhere to a specific ALIAS
pattern as defined in the framework's regular expressions repository.
Defaults to None
.
alias_priority
instance-attribute
alias_priority: int | None
The priority of the field's alias. This affects whether an alias
generator is used or not. Defaults to None
.
title
instance-attribute
title: str | None
The human-readable name of the field. It must adhere to a specific
TITLE
pattern as defined in the framework's regular expressions
repository. Defaults to None
.
description
instance-attribute
description: str | None
The description of the field. Defaults to None
and uses the
function's docstring if available.
examples
instance-attribute
List of examples of the field. Defaults to None
.
deprecated
instance-attribute
A deprecation message, an instance of Deprecated
, or a boolean. If
True
, a default deprecation message will be emitted when accessing the
field. Defaults to None
.
repr
instance-attribute
repr: bool
Whether or not to include the field in the representation.
Defaults to True
.
init
instance-attribute
init: bool
Whether the field should be included in the constructor of the
dataclass. Defaults to True
.
init_var
instance-attribute
init_var: bool | None
Whether the field should only be included in the constructor of the
dataclass, and not stored. Defaults to None
.
kw_only
instance-attribute
kw_only: bool | None
Whether or not the field should be a keyword-only argument in the
constructor of the model. Defaults to None
.
validation_alias
instance-attribute
validation_alias: str | AliasPath | AliasChoices | None
The validation alias name of the field. It must adhere to a specific
ALIAS
pattern as defined in the framework's regular expressions
repository. Defaults to None
(alias name of the field).
serialization_alias
instance-attribute
serialization_alias: str | None
The serialization alias name of the field. It must adhere to a specific
ALIAS
pattern as defined in the framework's regular expressions
repository. Defaults to None
(alias name of the field).
exclude
instance-attribute
exclude: bool | None
Whether or not to exclude the field from the model serialization.
Defaults to None
.
discriminator
instance-attribute
discriminator: str | Discriminator | None
Field name for discriminating the type in a tagged union.
Defaults to None
.
json_schema_extra
instance-attribute
json_schema_extra: JsonSchemaExtra | None
Dictionary or callable to provide extra JSON schema properties.
Defaults to None
.
validate_default
instance-attribute
validate_default: bool | None
Whether or not to validate the default value of the field.
Defaults to None
.
recursive_guard
instance-attribute
recursive_guard: str | None
A recursive guard to handle infinite recursion when validating and
serializing fields. Defaults to None
.
slug
instance-attribute
slug: str | None
The slug name of the field. It must adhere to a specific SLUG
pattern as defined in the framework's regular expressions repository.
Defaults to None
.
indexed
instance-attribute
indexed: bool | None
Whether or not the field is indexed. Defaults to None
.
collection
instance-attribute
collection: Literal['list', 'set'] | None
The collection type of the target association. It can be either an
unconstrained collection list
, or a collection of unique elements
set
, otherwise it defaults to None
if the association is not a
collection.
rel_attribute
instance-attribute
rel_attribute: str | None
The attribute name of the foreign key column implemented within the
owner resource. This is optional and only applies in a scenario where a
foreign key column is required for managing the association. If the field
is not linked or the association (like a many-to-many association) does not
require a direct foreign key column, this parameter defaults to None
.
Conversely, if a link exists and the association necessitates a foreign key
column (as per the two-way association analysis), this parameter defaults
to the alias of the linked field, appended with _id
.
Defaults to None
.
rel_cascade
instance-attribute
The cascade options for the relationship. The possible cascading options
are 'delete'
, 'delete-orphan'
, 'expunge'
, 'merge'
,
'refresh-expire'
, 'save-update'
, and all
, where the latter is a
shorthand for 'save-update, merge, refresh-expire, expunge, delete'
,
and is often used as in 'all, delete-orphan'
to indicate that related
objects should follow along with the parent object in all cases, and be
deleted when de-associated.
Defaults to None
for non-linked fields, otherwise True
for linked
fields which resolves to:
- 'save-update, merge'
when the association is bidirectional,
- '..., delete, delete-orphan'
when the target resource link is
identifying,
- '..., refresh-expire, expunge'
when the target resource link is
indexing.
rel_load
instance-attribute
The lazy loading options for the relationship.
Defaults to None
for non-linked fields, otherwise True
for linked
fields which resolves to:
- 'joined'
when the association is indexing one of the resources,
- 'selectin'
when the resource owner field is not a collection,
- 'select'
otherwise (lazy loading).
rel_extra
instance-attribute
Additional association relationship configuration parameters.
Defaults to None
.
FieldInfoDict
Bases: SourceFieldInfoDict[Object]
, BaseFieldInfoDict
, ModelFieldInfoDict
, ResourceFieldInfoDict
, Generic[Object]
A global field information configuration dictionary.
A dictionary that provides all the configuration parameters for a field
information instance within the Plateforme framework. It combines the
SourceFieldInfoDict
, BaseFieldInfoDict
, ModelFieldInfoDict
, and
ResourceFieldInfoDict
configuration dictionaries into a single
comprehensive dictionary.
slug
instance-attribute
slug: str | None
The slug name of the field. It must adhere to a specific SLUG
pattern as defined in the framework's regular expressions repository.
Defaults to None
.
indexed
instance-attribute
indexed: bool | None
Whether or not the field is indexed. Defaults to None
.
target_ref
instance-attribute
target_ref: bool | None
Whether the field can accept a reference to the target resource instead
of the resource schema model itself. Defaults to None
.
target_schemas
instance-attribute
Either a tuple of the linked field target DiscriminatedModel
schema
models to use for the annotation resolution, or a boolean to indicate
whether the target schema models should be resolved.
Defaults to None
.
association_alias
instance-attribute
association_alias: str | None
The association alias identifier. By default, this is determined by
concatenating the owner and target resource alias with an underscore (see
the Association
class). Defaults to None
.
rel_attribute
instance-attribute
rel_attribute: str | None
The attribute name of the foreign key column implemented within the
owner resource. This is optional and only applies in a scenario where a
foreign key column is required for managing the association. If the field
is not linked or the association (like a many-to-many association) does not
require a direct foreign key column, this parameter defaults to None
.
Conversely, if a link exists and the association necessitates a foreign key
column (as per the two-way association analysis), this parameter defaults
to the alias of the linked field, appended with _id
.
Defaults to None
.
rel_backref
instance-attribute
rel_backref: BackrefFieldInfoDict | None
The corresponding back referencing target linked field counterpart of
the association. This argument can be used to define a back reference
configuration using a BackrefFieldInfoDict
dictionary when no counterpart
exists in the target resource. This is useful in scenarios where there is
no control over the target resource definition, and the back reference must
be implemented in the owner resource. Otherwise, it is recommended to
define the back reference directly in the target resource.
If the target resource does not have a counterpart linked field defined for
the given association, the field information attribute initializes to
None
, otherwise it stores the target counterpart ResourceFieldInfo
instance, either generated from the target resourcedefinition, or by the
provided back reference dictionary configuration. Defaults to None
.
rel_cascade
instance-attribute
The cascade options for the relationship. The possible cascading options
are 'delete'
, 'delete-orphan'
, 'expunge'
, 'merge'
,
'refresh-expire'
, 'save-update'
, and all
, where the latter is a
shorthand for 'save-update, merge, refresh-expire, expunge, delete'
,
and is often used as in 'all, delete-orphan'
to indicate that related
objects should follow along with the parent object in all cases, and be
deleted when de-associated.
Defaults to None
for non-linked fields, otherwise True
for linked
fields which resolves to:
- 'save-update, merge'
when the association is bidirectional,
- '..., delete, delete-orphan'
when the target resource link is
identifying,
- '..., refresh-expire, expunge'
when the target resource link is
indexing.
rel_load
instance-attribute
The lazy loading options for the relationship.
Defaults to None
for non-linked fields, otherwise True
for linked
fields which resolves to:
- 'joined'
when the association is indexing one of the resources,
- 'selectin'
when the resource owner field is not a collection,
- 'select'
otherwise (lazy loading).
rel_extra
instance-attribute
Additional association relationship configuration parameters.
Defaults to None
.
column_extra
instance-attribute
Additional column configuration parameters. Defaults to None
.
data_type
instance-attribute
data_type: TypeEngine | None
The data type engine to use for the field. It is automatically inferred
from the field annotation if not explicitly set. Defaults to None
.
data_collation
instance-attribute
data_collation: str | None
The collation of the field used for strings within the database.
Defaults to None
.
data_none_as_null
instance-attribute
data_none_as_null: bool | None
Whether or not to treat None
as NULL
in the database for JSON
data types. Defaults to None
.
data_extra
instance-attribute
Additional data column configuration parameters.
Defaults to None
.
validation_alias
instance-attribute
validation_alias: str | AliasPath | AliasChoices | None
The validation alias name of the field. It must adhere to a specific
ALIAS
pattern as defined in the framework's regular expressions
repository. Defaults to None
(alias name of the field).
serialization_alias
instance-attribute
serialization_alias: str | None
The serialization alias name of the field. It must adhere to a specific
ALIAS
pattern as defined in the framework's regular expressions
repository. Defaults to None
(alias name of the field).
exclude
instance-attribute
exclude: bool | None
Whether or not to exclude the field from the model serialization.
Defaults to None
.
discriminator
instance-attribute
discriminator: str | Discriminator | None
Field name for discriminating the type in a tagged union.
Defaults to None
.
json_schema_extra
instance-attribute
json_schema_extra: JsonSchemaExtra | None
Dictionary or callable to provide extra JSON schema properties.
Defaults to None
.
validate_default
instance-attribute
validate_default: bool | None
Whether or not to validate the default value of the field.
Defaults to None
.
pattern
instance-attribute
pattern: str | None
A regular expression pattern that the field value must match.
Defaults to None
.
strict
instance-attribute
strict: bool | None
Whether or not to enforce strict validation of the field value.
Defaults to None
.
multiple_of
instance-attribute
multiple_of: float | None
The value must be a multiple of this number. Defaults to None
.
allow_inf_nan
instance-attribute
allow_inf_nan: bool | None
Whether or not to allow infinity and NaN values.
Defaults to None
.
max_digits
instance-attribute
max_digits: int | None
The maximum number of digits in the field value.
Defaults to None
.
decimal_places
instance-attribute
decimal_places: int | None
The number of decimal places in the field value.
Defaults to None
.
min_length
instance-attribute
min_length: int | None
The minimum length of the field value. Defaults to None
.
max_length
instance-attribute
max_length: int | None
The maximum length of the field value. Defaults to None
.
union_mode
instance-attribute
union_mode: Literal['smart', 'left_to_right'] | None
The union mode for the field value. Defaults to None
.
recursive_guard
instance-attribute
recursive_guard: str | None
A recursive guard to handle infinite recursion when validating and
serializing fields. Defaults to None
.
default_factory
instance-attribute
The factory function used to construct the default value of the field.
If both the default value and the default factory are set, an error is
raised. Defaults to None
.
alias
instance-attribute
alias: str | None
The alias name of the field. It must adhere to a specific ALIAS
pattern as defined in the framework's regular expressions repository.
Defaults to None
.
alias_priority
instance-attribute
alias_priority: int | None
The priority of the field's alias. This affects whether an alias
generator is used or not. Defaults to None
.
title
instance-attribute
title: str | None
The human-readable name of the field. It must adhere to a specific
TITLE
pattern as defined in the framework's regular expressions
repository. Defaults to None
.
description
instance-attribute
description: str | None
The description of the field. Defaults to None
and uses the
function's docstring if available.
examples
instance-attribute
List of examples of the field. Defaults to None
.
deprecated
instance-attribute
A deprecation message, an instance of Deprecated
, or a boolean. If
True
, a default deprecation message will be emitted when accessing the
field. Defaults to None
.
repr
instance-attribute
repr: bool
Whether or not to include the field in the representation.
Defaults to True
.
init
instance-attribute
init: bool
Whether the field should be included in the constructor of the
dataclass. Defaults to True
.
init_var
instance-attribute
init_var: bool | None
Whether the field should only be included in the constructor of the
dataclass, and not stored. Defaults to None
.
kw_only
instance-attribute
kw_only: bool | None
Whether or not the field should be a keyword-only argument in the
constructor of the model. Defaults to None
.
source
instance-attribute
source: str | None
The source representation of the field definition.
Defaults to None
.
owner
instance-attribute
owner: type[Object] | None
The owner of the field. It is inferred from the class where the field is
defined if available else it defaults to None
.
name
instance-attribute
name: str | None
The name of the field. It must adhere to a specific ALIAS
pattern as
defined in the framework's regular expressions repository.
Defaults to None
.
annotation
instance-attribute
The type annotation of the field. Defaults to Undefined
.
default
instance-attribute
default: Any
The default value of the field. If not set and no default factory is
provided, the field is considered required. Defaults to Undefined
.
collection
instance-attribute
collection: Literal['list', 'set'] | None
The collection type of the target association. It can be either an
unconstrained collection list
, or a collection of unique elements
set
, otherwise it defaults to None
if the association is not a
collection.
target
instance-attribute
target: ResourceType | str | None
The linked field target BaseResource
object type.
Defaults to None
.
association
instance-attribute
association: Association | None
The Association
object that defines the association between the owner
and target resources. Defaults to None
.
FieldInfoFromConfigDict
Bases: BaseFieldInfoDict
A field information configuration dictionary for a config function.
It is used to define a field information instance with the ConfigField
function for configuration wrappers.
default_factory
instance-attribute
The factory function used to construct the default value of the field.
If both the default value and the default factory are set, an error is
raised. Defaults to None
.
alias
instance-attribute
alias: str | None
The alias name of the field. It must adhere to a specific ALIAS
pattern as defined in the framework's regular expressions repository.
Defaults to None
.
alias_priority
instance-attribute
alias_priority: int | None
The priority of the field's alias. This affects whether an alias
generator is used or not. Defaults to None
.
title
instance-attribute
title: str | None
The human-readable name of the field. It must adhere to a specific
TITLE
pattern as defined in the framework's regular expressions
repository. Defaults to None
.
description
instance-attribute
description: str | None
The description of the field. Defaults to None
and uses the
function's docstring if available.
examples
instance-attribute
List of examples of the field. Defaults to None
.
deprecated
instance-attribute
A deprecation message, an instance of Deprecated
, or a boolean. If
True
, a default deprecation message will be emitted when accessing the
field. Defaults to None
.
repr
instance-attribute
repr: bool
Whether or not to include the field in the representation.
Defaults to True
.
init
instance-attribute
init: bool
Whether the field should be included in the constructor of the
dataclass. Defaults to True
.
init_var
instance-attribute
init_var: bool | None
Whether the field should only be included in the constructor of the
dataclass, and not stored. Defaults to None
.
kw_only
instance-attribute
kw_only: bool | None
Whether or not the field should be a keyword-only argument in the
constructor of the model. Defaults to None
.
FieldInfoFromFieldDict
Bases: BaseFieldInfoDict
, ModelFieldInfoDict
, ResourceFieldInfoDict
A field information configuration dictionary for a field function.
It is used to define a field information instance with the Field
function
for models and resources.
slug
instance-attribute
slug: str | None
The slug name of the field. It must adhere to a specific SLUG
pattern as defined in the framework's regular expressions repository.
Defaults to None
.
indexed
instance-attribute
indexed: bool | None
Whether or not the field is indexed. Defaults to None
.
target_ref
instance-attribute
target_ref: bool | None
Whether the field can accept a reference to the target resource instead
of the resource schema model itself. Defaults to None
.
target_schemas
instance-attribute
Either a tuple of the linked field target DiscriminatedModel
schema
models to use for the annotation resolution, or a boolean to indicate
whether the target schema models should be resolved.
Defaults to None
.
association_alias
instance-attribute
association_alias: str | None
The association alias identifier. By default, this is determined by
concatenating the owner and target resource alias with an underscore (see
the Association
class). Defaults to None
.
rel_attribute
instance-attribute
rel_attribute: str | None
The attribute name of the foreign key column implemented within the
owner resource. This is optional and only applies in a scenario where a
foreign key column is required for managing the association. If the field
is not linked or the association (like a many-to-many association) does not
require a direct foreign key column, this parameter defaults to None
.
Conversely, if a link exists and the association necessitates a foreign key
column (as per the two-way association analysis), this parameter defaults
to the alias of the linked field, appended with _id
.
Defaults to None
.
rel_backref
instance-attribute
rel_backref: BackrefFieldInfoDict | None
The corresponding back referencing target linked field counterpart of
the association. This argument can be used to define a back reference
configuration using a BackrefFieldInfoDict
dictionary when no counterpart
exists in the target resource. This is useful in scenarios where there is
no control over the target resource definition, and the back reference must
be implemented in the owner resource. Otherwise, it is recommended to
define the back reference directly in the target resource.
If the target resource does not have a counterpart linked field defined for
the given association, the field information attribute initializes to
None
, otherwise it stores the target counterpart ResourceFieldInfo
instance, either generated from the target resourcedefinition, or by the
provided back reference dictionary configuration. Defaults to None
.
rel_cascade
instance-attribute
The cascade options for the relationship. The possible cascading options
are 'delete'
, 'delete-orphan'
, 'expunge'
, 'merge'
,
'refresh-expire'
, 'save-update'
, and all
, where the latter is a
shorthand for 'save-update, merge, refresh-expire, expunge, delete'
,
and is often used as in 'all, delete-orphan'
to indicate that related
objects should follow along with the parent object in all cases, and be
deleted when de-associated.
Defaults to None
for non-linked fields, otherwise True
for linked
fields which resolves to:
- 'save-update, merge'
when the association is bidirectional,
- '..., delete, delete-orphan'
when the target resource link is
identifying,
- '..., refresh-expire, expunge'
when the target resource link is
indexing.
rel_load
instance-attribute
The lazy loading options for the relationship.
Defaults to None
for non-linked fields, otherwise True
for linked
fields which resolves to:
- 'joined'
when the association is indexing one of the resources,
- 'selectin'
when the resource owner field is not a collection,
- 'select'
otherwise (lazy loading).
rel_extra
instance-attribute
Additional association relationship configuration parameters.
Defaults to None
.
column_extra
instance-attribute
Additional column configuration parameters. Defaults to None
.
data_type
instance-attribute
data_type: TypeEngine | None
The data type engine to use for the field. It is automatically inferred
from the field annotation if not explicitly set. Defaults to None
.
data_collation
instance-attribute
data_collation: str | None
The collation of the field used for strings within the database.
Defaults to None
.
data_none_as_null
instance-attribute
data_none_as_null: bool | None
Whether or not to treat None
as NULL
in the database for JSON
data types. Defaults to None
.
data_extra
instance-attribute
Additional data column configuration parameters.
Defaults to None
.
validation_alias
instance-attribute
validation_alias: str | AliasPath | AliasChoices | None
The validation alias name of the field. It must adhere to a specific
ALIAS
pattern as defined in the framework's regular expressions
repository. Defaults to None
(alias name of the field).
serialization_alias
instance-attribute
serialization_alias: str | None
The serialization alias name of the field. It must adhere to a specific
ALIAS
pattern as defined in the framework's regular expressions
repository. Defaults to None
(alias name of the field).
exclude
instance-attribute
exclude: bool | None
Whether or not to exclude the field from the model serialization.
Defaults to None
.
discriminator
instance-attribute
discriminator: str | Discriminator | None
Field name for discriminating the type in a tagged union.
Defaults to None
.
json_schema_extra
instance-attribute
json_schema_extra: JsonSchemaExtra | None
Dictionary or callable to provide extra JSON schema properties.
Defaults to None
.
validate_default
instance-attribute
validate_default: bool | None
Whether or not to validate the default value of the field.
Defaults to None
.
pattern
instance-attribute
pattern: str | None
A regular expression pattern that the field value must match.
Defaults to None
.
strict
instance-attribute
strict: bool | None
Whether or not to enforce strict validation of the field value.
Defaults to None
.
multiple_of
instance-attribute
multiple_of: float | None
The value must be a multiple of this number. Defaults to None
.
allow_inf_nan
instance-attribute
allow_inf_nan: bool | None
Whether or not to allow infinity and NaN values.
Defaults to None
.
max_digits
instance-attribute
max_digits: int | None
The maximum number of digits in the field value.
Defaults to None
.
decimal_places
instance-attribute
decimal_places: int | None
The number of decimal places in the field value.
Defaults to None
.
min_length
instance-attribute
min_length: int | None
The minimum length of the field value. Defaults to None
.
max_length
instance-attribute
max_length: int | None
The maximum length of the field value. Defaults to None
.
union_mode
instance-attribute
union_mode: Literal['smart', 'left_to_right'] | None
The union mode for the field value. Defaults to None
.
recursive_guard
instance-attribute
recursive_guard: str | None
A recursive guard to handle infinite recursion when validating and
serializing fields. Defaults to None
.
default_factory
instance-attribute
The factory function used to construct the default value of the field.
If both the default value and the default factory are set, an error is
raised. Defaults to None
.
alias
instance-attribute
alias: str | None
The alias name of the field. It must adhere to a specific ALIAS
pattern as defined in the framework's regular expressions repository.
Defaults to None
.
alias_priority
instance-attribute
alias_priority: int | None
The priority of the field's alias. This affects whether an alias
generator is used or not. Defaults to None
.
title
instance-attribute
title: str | None
The human-readable name of the field. It must adhere to a specific
TITLE
pattern as defined in the framework's regular expressions
repository. Defaults to None
.
description
instance-attribute
description: str | None
The description of the field. Defaults to None
and uses the
function's docstring if available.
examples
instance-attribute
List of examples of the field. Defaults to None
.
deprecated
instance-attribute
A deprecation message, an instance of Deprecated
, or a boolean. If
True
, a default deprecation message will be emitted when accessing the
field. Defaults to None
.
repr
instance-attribute
repr: bool
Whether or not to include the field in the representation.
Defaults to True
.
init
instance-attribute
init: bool
Whether the field should be included in the constructor of the
dataclass. Defaults to True
.
init_var
instance-attribute
init_var: bool | None
Whether the field should only be included in the constructor of the
dataclass, and not stored. Defaults to None
.
kw_only
instance-attribute
kw_only: bool | None
Whether or not the field should be a keyword-only argument in the
constructor of the model. Defaults to None
.
FieldInfoMeta
FieldInfo
FieldInfo(**kwargs: Unpack[FieldInfoDict[Any]])
Bases: Representation
, FieldInfo
, Generic[Object]
A field information.
A class that holds information about a field. The FieldInfo
class is used
however a field is defined, whether or not a field function is explicitly
used within a BaseModel
, BaseResource
, or ConfigWrapper
subclass.
Attributes:
Name | Type | Description |
---|---|---|
source |
str | None
|
The source representation of the field definition.
Defaults to |
owner |
type[Object]
|
The owner of the field. It is inferred from the class where the
field is defined if available else it defaults to |
name |
str
|
The name of the field. It must adhere to a specific |
annotation |
type[Any] | None
|
The type annotation of the field.
Defaults to |
default |
Any
|
The default value of the field. If not set and no default
factory is provided, the field is considered required.
Defaults to |
default_factory |
Callable[[], Any] | None
|
The factory function used to construct the default
value of the field. If both the default value and the default
factory are set, an error is raised. Defaults to |
alias |
str
|
The alias name of the field. It must adhere to a specific
|
alias_priority |
int | None
|
The priority of the field's alias. This affects whether
an alias generator is used or not. Defaults to |
title |
str
|
The human-readable name of the field. It must adhere to a
specific |
description |
str | None
|
The description of the field. Defaults to |
examples |
list[Any] | None
|
List of examples of the field. Defaults to |
deprecated |
Deprecated | str | bool | None
|
A deprecation message, an instance of |
frozen |
bool | None
|
Whether or not the field is frozen. Defaults to |
repr |
bool
|
Whether or not to include the field in the representation.
Defaults to |
init |
bool
|
Whether the field should be included in the constructor of the
dataclass. Defaults to |
init_var |
bool | None
|
Whether the field should only be included in the constructor
of the dataclass, and not stored. Defaults to |
kw_only |
bool | None
|
Whether or not the field should be a keyword-only argument in
the constructor of the model. Defaults to |
validation_alias |
str | AliasPath | AliasChoices | None
|
The validation alias name of the field. It must
adhere to a specific |
serialization_alias |
str | None
|
The serialization alias name of the field. It
must adhere to a specific |
exclude |
bool | None
|
Whether or not to exclude the field from the model
serialization. Defaults to |
discriminator |
str | Discriminator | None
|
Field name for discriminating the type in a tagged
union. Defaults to |
json_schema_extra |
JsonSchemaExtra | None
|
Dictionary or callable to provide extra JSON schema
properties. Defaults to |
validate_default |
bool | None
|
Whether or not to validate the default value of the
field. Defaults to |
metadata |
list[Any]
|
List of metadata constraints. Defaults to an empty list. |
recursive_guard |
str | None
|
A recursive guard to handle infinite recursion when
validating and serializing fields. Defaults to |
slug |
str
|
The slug name of the field. It must adhere to a specific |
unique |
bool | None
|
Whether or not the field is unique. Defaults to |
indexed |
bool | None
|
Whether or not the field is indexed. Defaults to |
linked |
bool | None
|
Whether or not the field is linked. Defaults to |
collection |
Literal['list', 'set'] | None
|
The collection type of the target association. It can be
either an unconstrained collection |
target |
ResourceType | str | None
|
The linked field target |
target_ref |
bool | None
|
Whether the field can accept a reference to the target
resource instead of the resource schema model itself.
Defaults to |
target_schemas |
tuple[str, ...] | bool | None
|
Either a tuple of the linked field target
|
association |
Association | None
|
The |
association_alias |
str | None
|
The association alias identifier. By default, this
is determined by concatenating the owner and target resource alias
with an underscore (see the |
rel_attribute |
str | None
|
The attribute name of the foreign key column implemented
within the owner resource. This is optional and only applies in a
scenario where a foreign key column is required for managing the
association. If the field is not linked or the association (like a
many-to-many association) does not require a direct foreign key
column, this parameter defaults to |
rel_backref |
ResourceFieldInfo | None
|
The corresponding back referencing target linked field
counterpart of the association. If the target resource does not
have a counterpart linked field defined for the given association,
it initializes to |
rel_cascade |
str | bool | None
|
The cascade options for the relationship. The possible
cascading options are |
rel_load |
LoadRule | bool | None
|
The lazy loading options for the relationship.
Defaults to |
rel_extra |
dict[str, Any] | None
|
Additional association relationship configuration
parameters. Defaults to |
column_extra |
dict[str, Any] | None
|
Additional column configuration parameters.
Defaults to |
data_type |
TypeEngine | None
|
The data type engine to use for the field. It is
automatically inferred from the field annotation if not explicitly
set. Defaults to |
data_collation |
str | None
|
The collation of the field used for strings within the
database. Defaults to |
data_none_as_null |
bool | None
|
Whether or not to treat |
data_extra |
dict[str, Any] | None
|
Additional data column configuration parameters.
Defaults to |
Initialize the field information.
It initializes the field information instance with the given keyword
arguments. This constructor is not meant to be called directly, use the
field functions like Field
for models and resources, or ConfigField
for configuration wrappers instead to create a field information
instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
Unpack[FieldInfoDict[Any]]
|
Additional keyword arguments. See the field information
configuration dictionary |
{}
|
Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/fields.py
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 |
|
get_default
Get the default value.
We expose an option for whether to call the default_factory (if present), as calling it may
result in side effects that we want to avoid. However, there are times when it really should
be called (namely, when instantiating a model via model_construct
).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
call_default_factory
|
bool
|
Whether to call the default_factory or not. Defaults to |
False
|
Returns:
Type | Description |
---|---|
Any
|
The default value, calling the default factory if requested or |
Source code in .venv/lib/python3.12/site-packages/pydantic/fields.py
rebuild_annotation
rebuild_annotation() -> Any
Attempts to rebuild the original annotation for use in function signatures.
If metadata is present, it adds it to the original annotation using
Annotated
. Otherwise, it returns the original annotation as-is.
Note that because the metadata has been flattened, the original annotation
may not be reconstructed exactly as originally provided, e.g. if the original
type had unrecognized annotations, or was annotated with a call to pydantic.Field
.
Returns:
Type | Description |
---|---|
Any
|
The rebuilt annotation. |
Source code in .venv/lib/python3.12/site-packages/pydantic/fields.py
apply_typevars_map
apply_typevars_map(
typevars_map: dict[Any, Any] | None,
types_namespace: dict[str, Any] | None,
) -> None
Apply a typevars_map
to the annotation.
This method is used when analyzing parametrized generic types to replace typevars with their concrete types.
This method applies the typevars_map
to the annotation in place.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
typevars_map
|
dict[Any, Any] | None
|
A dictionary mapping type variables to their concrete types. |
required |
types_namespace
|
dict | None
|
A dictionary containing related types to the annotated type. |
required |
See Also
pydantic._internal._generics.replace_types is used for replacing the typevars with their concrete types.
Source code in .venv/lib/python3.12/site-packages/pydantic/fields.py
from_annotation
classmethod
from_annotation(
annotation: type[Any],
*,
owner: type[Object] | None = Undefined,
name: str | None = Undefined,
) -> Self
Creates a field info instance from a bare annotation.
Examples:
It is used internally to create a FieldInfo
from a bare
annotation like this:
We also account for the case where the annotation can be an
instance of Annotated
and where one of the (not first) arguments
in Annotated
is an instance of FieldInfo
, e.g.:
>>> import annotated_types
... from typing import Annotated
... from plateforme import BaseModel, Field
>>> class MyModel(BaseModel):
... foo: Annotated[int, annotated_types.Gt(42)]
... bar: Annotated[int, Field(gt=42)]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
annotation
|
type[Any]
|
An annotation object. |
required |
owner
|
type[Object] | None
|
The owner of the field. Defaults to |
Undefined
|
name
|
str | None
|
The name of the field. Defaults to |
Undefined
|
Returns:
Type | Description |
---|---|
Self
|
A new field info instance with the given annotation and metadata. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/fields.py
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 |
|
from_annotated_attribute
classmethod
from_annotated_attribute(
annotation: type[Any],
default: Any,
*,
owner: type[Object] | None = Undefined,
name: str | None = Undefined,
) -> Self
Create a field info instance from an annotation with default value.
Examples:
It is used internally to create a FieldInfo
from an annotated
attribute like this:
>>> import annotated_types
... from typing import Annotated
... from plateforme import BaseModel, Field
>>> class MyModel(BaseModel):
... foo: int = 4 # <-- like this
... baz: int = Field(4, gt=4) # or this
... baz: Annotated[int, annotated_types.Gt(4)] = 4 # or this
Parameters:
Name | Type | Description | Default |
---|---|---|---|
annotation
|
type[Any]
|
The type annotation of the field. |
required |
default
|
Any
|
The default value of the field. |
required |
owner
|
type[Object] | None
|
The owner of the field. Defaults to |
Undefined
|
name
|
str | None
|
The name of the field. Defaults to |
Undefined
|
Returns:
Type | Description |
---|---|
Self
|
A new field info instance with the given annotation and default |
Self
|
value. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/fields.py
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 |
|
from_field
classmethod
from_field(
default: Any = Undefined,
**kwargs: Unpack[FieldInfoFromFieldDict],
) -> Self
Create a field info object instance with a field function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
default
|
Any
|
Since this is replacing the field's default, its first
argument is used to set the default, use ellipsis |
Undefined
|
**kwargs
|
Unpack[FieldInfoFromFieldDict]
|
Additional keyword arguments. See the field information
configuration dictionary |
{}
|
Raises:
Type | Description |
---|---|
TypeError
|
If |
Returns:
Type | Description |
---|---|
Self
|
A new field info instance with the given parameters. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/fields.py
from_field_info
classmethod
from_field_info(
base: FieldInfo, **overrides: Unpack[FieldInfoDict[Any]]
) -> Self
Create a field info object instance from a base field info.
It constructs a new field info instance from a base field info instance with additional keyword arguments to override the base field info attributes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base
|
FieldInfo
|
The base field info to create the field info instance from. |
required |
**overrides
|
Unpack[FieldInfoDict[Any]]
|
Additional keyword arguments to override the base
field info attributes. See the field information configuration
dictionary |
{}
|
Returns:
Type | Description |
---|---|
Self
|
A new field info instance with the given parameters. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/fields.py
merge_field_infos
classmethod
merge_field_infos(
*field_infos: FieldInfo,
**overrides: Unpack[FieldInfoDict[Any]],
) -> Self
Merge field info instances keeping only explicitly set attributes.
It merges multiple FieldInfo
instances into a single one, keeping
only the attributes that are explicitly set in each instance. The later
FieldInfo
instances override the earlier ones, while the overrides
keyword arguments are applied to the merged FieldInfo
instance.
FIXME: When multiple field info instances are merged, Pydantic uses its
own FieldInfo
class to merge them. This is not ideal as it does not
take into account the FieldInfo
class hierarchy. This impacts
typically fields with annotation that uses the Field
functions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*field_infos
|
FieldInfo
|
The field info instances to merge. |
()
|
**overrides
|
Unpack[FieldInfoDict[Any]]
|
Additional keyword arguments to override the merged
field info attributes. See the field information configuration
dictionary |
{}
|
Returns:
Type | Description |
---|---|
Self
|
A new field info instance with the merged attributes. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/fields.py
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 |
|
build
Build the field information target and annotation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
force
|
bool
|
Whether to force the field information building even if it
is already complete. Defaults to |
False
|
raise_errors
|
bool
|
Whether to raise errors if the field information
cannot be built. Defaults to |
True
|
Returns:
Type | Description |
---|---|
bool | None
|
|
bool | None
|
if the field information cannot be built, and |
bool | None
|
information is already complete. |
Raises:
Type | Description |
---|---|
PlateformeError
|
If the target is not a valid resource or if the target schema is not found in the target resource. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/fields.py
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 |
|
build_recursive_guard
build_recursive_guard() -> str | None
Build the field information recursive guard.
Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/fields.py
build_target_annotation
Build the field information target annotation and metadata.
For fields that are linked to another resource, this method returns
either a tuple containing the target type itself or a selector
reference to the target type, or a tuple containing the target schema
model with optional selector reference annotation when target_schemas
and target_ref
are both set.
Returns:
Type | Description |
---|---|
tuple[Any, ...] | None
|
A tuple containing the field information target annotation and |
tuple[Any, ...] | None
|
metadata, or |
Raises:
Type | Description |
---|---|
PlateformeError
|
If no resource schema models matches the specified target schema models of the field information. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/fields.py
create_column
Create a SQLAlchemy column object from the field information.
Raises:
Type | Description |
---|---|
TypeError
|
If the field is linked. |
ValueError
|
If the owner is not a valid resource. |
PlateformeError
|
If the field annotation or data type engine is invalid. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/fields.py
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 |
|
entries
Return the field information entries based on the specified scope.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scope
|
Literal['all', 'set']
|
The scope of the field information dictionary to return. It
can be either |
'all'
|
Returns:
Type | Description |
---|---|
dict[str, Any]
|
A dictionary containing the field information entries based on the |
dict[str, Any]
|
specified scope. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/fields.py
is_cascading
is_cascading(*options: CascadeRule) -> bool | None
Check if the field association cascades the provided options.
It defines whether or not to cascade operations to the field associated target resource. This determines if operations on the field owner resource should extend to its target resource. It is used to infer if it is possible to simultaneously create, read, update, upsert, or delete associated target resource through the same endpoint, effectively treating the associated target data as part of the owner resource's data model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*options
|
CascadeRule
|
The cascade rules to check against. The possible
options are Those can be set using the |
()
|
Returns:
Type | Description |
---|---|
bool | None
|
Whether the field association cascades the provided options, or |
bool | None
|
|
Raises:
Type | Description |
---|---|
ValueError
|
If the field |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/fields.py
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 |
|
is_eagerly
is_eagerly() -> bool | None
Check if the field association loading is eager.
It defines whether or not to load eagerly the field associated target resource data when loading the field owner resource. It is used in resource schemas and endpoints to infer if the target resource data should be returned and traversed along with the owner resource data.
Returns:
Type | Description |
---|---|
bool | None
|
Whether the field association loading is eager, or |
bool | None
|
field is not linked. |
Raises:
Type | Description |
---|---|
ValueError
|
If the field |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/fields.py
is_identifying
is_identifying() -> bool | None
Check if the field exists within its owner identifiers.
The field is considered identifying if either the field unique
attribute is set to True
, or if the field alias
is found within a
composite identifier of its owner resource.
Returns:
Type | Description |
---|---|
bool | None
|
Whether the field is identifying its owner, or |
bool | None
|
field is not attached to a resource. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/fields.py
is_indexing
is_indexing() -> bool | None
Check if the field exists within its owner indexes.
The field is considered indexing if either the field indexed
or
unique
attribute is set to True
, or if the field alias
is found
within a composite index of its owner resource.
Returns:
Type | Description |
---|---|
bool | None
|
Whether the field is indexing its owner, or |
bool | None
|
is not attached to a resource. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/fields.py
is_nullable
is_nullable() -> bool
Check if the field is nullable.
It is considered nullable if the field accepts None
as a value.
Returns:
Type | Description |
---|---|
bool
|
Whether the field is nullable. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/fields.py
is_required
is_required() -> bool
Check if the field is required.
It is considered required if it does not have a default value, i.e. it
is not set to Undefined
and does not have a default factory.
Returns:
Type | Description |
---|---|
bool
|
Whether the field is required. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/fields.py
ComputedFieldInfo
dataclass
ComputedFieldInfo(
*,
wrapped_property: property,
return_type: Any,
alias: str,
alias_priority: int | None,
title: str,
description: str | None,
examples: list[Any] | None,
json_schema_extra: JsonSchemaDict
| Callable[[JsonSchemaDict], None]
| None,
repr: bool,
name: str,
slug: str,
deprecated: Deprecated | str | bool | None,
)
Bases: ComputedFieldInfo
A computed field information.
A container for data from @computed_field
so it can access while
building the pydantic-core
schema.
Attributes:
Name | Type | Description |
---|---|---|
decorator_repr |
str
|
A class variable representing the decorator string
|
wrapped_property |
property
|
The wrapped computed field property. |
name |
str
|
The name of the field. It must adhere to a specific |
return_type |
Any
|
An optional return type for serialization logic to expect
when serializing to JSON. If included, this must be correct,
otherwise a |
alias |
str
|
The alias name of the field. It must adhere to a specific
|
alias_priority |
int | None
|
The priority of the field's alias. This affects whether
an alias generator is used or not. Defaults to |
slug |
str
|
The slug name of the field. It must adhere to a specific |
title |
str
|
The human-readable name of the field. It must adhere to a
specific |
description |
str | None
|
The description of the field. Defaults to |
examples |
list[Any] | None
|
List of examples of the field. Defaults to |
deprecated |
Deprecated | str | bool | None
|
A deprecation message, an instance of |
repr |
bool
|
Whether or not to include the field in the representation.
Defaults to |
json_schema_extra |
JsonSchemaDict | Callable[[JsonSchemaDict], None] | None
|
Dictionary or callable to provide extra JSON schema
properties. Defaults to |
PrivateAttr
PrivateAttr(
default: Any = PydanticUndefined,
*,
default_factory: Callable[[], Any] | None = None,
) -> Any
Usage docs: https://docs.pydantic.dev/2.6/concepts/models/#private-model-attributes
Indicates that an attribute is intended for private use and not handled during normal validation/serialization.
Private attributes are not validated by Pydantic, so it's up to you to ensure they are used in a type-safe manner.
Private attributes are stored in __private_attributes__
on the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
default
|
Any
|
The attribute's default value. Defaults to Undefined. |
PydanticUndefined
|
default_factory
|
Callable[[], Any] | None
|
Callable that will be
called when a default value is needed for this attribute.
If both |
None
|
Returns:
Type | Description |
---|---|
Any
|
An instance of |
Raises:
Type | Description |
---|---|
ValueError
|
If both |
Source code in .venv/lib/python3.12/site-packages/pydantic/fields.py
ConfigField
ConfigField(
default: Any = Undefined,
**kwargs: Unpack[FieldInfoFromConfigDict],
) -> Any
Create a field information instance for a configuration field.
Used to provide extra information about a configuration field for a configuration wrapper.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
default
|
Any
|
Since this is replacing the field's default, its first
argument is used to set the default, use ellipsis |
Undefined
|
**kwargs
|
Unpack[FieldInfoFromConfigDict]
|
Additional keyword arguments. See the field information
configuration dictionary |
{}
|
Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/fields.py
Field
Field(
default: Any = Undefined,
**kwargs: Unpack[FieldInfoFromFieldDict],
) -> Any
Create a field information instance for a model or resource field.
Used to provide extra information about a field, either for the model or
resource schema, or complex validation. Some arguments apply only to number
fields (int
, float
, Decimal
) and some apply only to str
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
default
|
Any
|
Since this is replacing the field's default, its first
argument is used to set the default, use ellipsis |
Undefined
|
**kwargs
|
Unpack[FieldInfoFromFieldDict]
|
Additional keyword arguments. See the field information
configuration dictionary |
{}
|
Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/fields.py
computed_field
computed_field(
*,
return_type: Any = Undefined,
alias: str | None = None,
alias_priority: int | None = None,
slug: str | None = None,
title: str | None = None,
description: str | None = None,
examples: list[Any] | None = None,
deprecated: Deprecated | str | bool | None = None,
repr: bool = True,
json_schema_extra: JsonSchemaDict
| Callable[[JsonSchemaDict], None]
| None = None,
) -> Callable[[_TProperty], _TProperty]
computed_field(
__func: _TProperty | None = None,
*,
return_type: Any = Undefined,
alias: str | None = None,
alias_priority: int | None = None,
slug: str | None = None,
title: str | None = None,
description: str | None = None,
examples: list[Any] | None = None,
deprecated: Deprecated | str | bool | None = None,
repr: bool | None = None,
json_schema_extra: JsonSchemaDict
| Callable[[JsonSchemaDict], None]
| None = None,
) -> _TProperty | Callable[[_TProperty], _TProperty]
A decorator for computed fields.
It is used to include property
and cached_property
when serializing
models or dataclasses. This is useful for fields that are computed from
other fields, or for fields that are expensive to compute and should be
cached.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
__func
|
_TProperty | None
|
The property function to wrap. Defaults to |
None
|
return_type
|
Any
|
An optional return type for serialization logic to expect
when serializing to JSON. If included, this must be correct,
otherwise a |
Undefined
|
alias
|
str | None
|
The alias name of the field. It must adhere to a specific
|
None
|
alias_priority
|
int | None
|
The priority of the field's alias. This affects whether
an alias generator is used or not. Defaults to |
None
|
slug
|
str | None
|
The slug name of the field. It must adhere to a specific |
None
|
title
|
str | None
|
The human-readable name of the field. It must adhere to a
specific |
None
|
description
|
str | None
|
The description of the field. Defaults to |
None
|
examples
|
list[Any] | None
|
List of examples of the field. Defaults to |
None
|
deprecated
|
Deprecated | str | bool | None
|
A deprecation message, an instance of |
None
|
repr
|
bool | None
|
Whether or not to include the field in the representation.
Defaults to |
None
|
json_schema_extra
|
JsonSchemaDict | Callable[[JsonSchemaDict], None] | None
|
Dictionary or callable to provide extra JSON schema
properties. Defaults to |
None
|
Returns:
Type | Description |
---|---|
_TProperty | Callable[[_TProperty], _TProperty]
|
A proxy wrapper for the property. |
Raises:
Type | Description |
---|---|
PlateformeError
|
At least one identifier must be either inferred from
the function name or provided as an argument. If none are provided,
an error iis raised. The identifier is used to generate the field's
|
Examples:
>>> from plateforme import BaseModel, computed_field
>>> class Rectangle(BaseModel):
... width: int
... length: int
...
... @computed_field
... @property
... def area(self) -> int:
... return self.width * self.length
If applied to functions not yet decorated with @property
or
@cached_property
, the function is automatically wrapped with
property
. Although this is more concise, you will lose intellisense
in your IDE, and confuse static type checkers, thus explicit use of
@property
is recommended.
>>> from plateforme import BaseModel, computed_field
>>> class Square(BaseModel):
... width: float
...
... # converted to a "property" by "computed_field"
... @computed_field
... def area(self) -> float:
... return round(self.width**2, 2)
...
... @area.setter
... def area(self, new_area: float) -> None:
... self.width = new_area**0.5
...
... @computed_field(alias='the magic number', repr=False)
... def random_number(self) -> int:
... return random.randint(0, 1000)
>>> square.area = 4
... print(square.model_dump_json(by_alias=True))
{"width": 2.0, "area": 4.0, "the magic number": 3}
It is not possible to override a field from a parent class with a
computed_field
in the child class. MyPy complains about this behavior
if allowed, and dataclasses
doesn't allow this pattern either as
shown in the example below:
>>> try:
... class Child(Parent):
... @computed_field
... @property
... def a(self) -> str:
... return 'new a'
... except ValueError as e:
... print(repr(e))
ValueError('you can't override a field with a computed field')
The private properties decorated with @computed_field
have their
representation set to false repr=False
by default:
>>> from functools import cached_property
>>> from plateforme import BaseModel, computed_field
>>> class Model(BaseModel):
... foo: int
...
... @computed_field
... @cached_property
... def _private_cached_property(self) -> int:
... return -self.foo
...
... @computed_field
... @property
... def _private_property(self) -> int:
... return -self.foo
Note
Using MyPy, even with the @property
or @cached_property
applied
to your function before @computed_field
, it may throw a "Decorated
property not supported" error.
See https://github.com/python/mypy/issues/1362
Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/fields.py
2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 |
|