Skip to content

Aliases

plateforme.core.schema.aliases

This module provides utilities for managing schema aliases within the Plateforme framework using Pydantic features.

AliasPath dataclass

AliasPath(entry: str | int, *extra: str | int)

Bases: AliasPath

A data class to specify a path to a model field using aliases.

This class is used in models and resources to specify a path to a field using aliases. It is typically used for indexing into nested data structures like lists or dictionaries within JSON payloads.

Attributes:

Name Type Description
path list[int | str]

A list representing the path to the target field using string keys for dictionary access or integer indices for list access.

Examples:

>>> from plateforme import BaseModel, Field
>>> class User(BaseModel):
...     first_name: str = Field(validation_alias=AliasPath('names', 0))
...     last_name: str = Field(validation_alias=AliasPath('names', 1))
>>> user_data = {'names': ['John', 'Doe']}
>>> user = User.model_validate(user_data)
>>> print(user)
first_name='John' last_name='Doe'
Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/aliases.py
def __init__(self, entry: str | int, *extra: str | int) -> None:
    self.path = [entry] + list(extra)

convert_to_aliases

convert_to_aliases() -> list[str | int]

Converts arguments to a list of string or integer aliases.

Returns:

Type Description
list[str | int]

The list of aliases.

Source code in .venv/lib/python3.12/site-packages/pydantic/aliases.py
def convert_to_aliases(self) -> list[str | int]:
    """Converts arguments to a list of string or integer aliases.

    Returns:
        The list of aliases.
    """
    return self.path

convert_to_alias

convert_to_alias() -> str

Converts arguments to an alias string.

It joins the path elements with underscores to create a single string representing the alias path.

Returns:

Type Description
str

The alias path as an alias string.

Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/aliases.py
def convert_to_alias(self) -> str:
    """Converts arguments to an alias string.

    It joins the path elements with underscores to create a single string
    representing the alias path.

    Returns:
        The alias path as an alias string.
    """
    return '_'.join(map(str, self.path))

AliasChoices dataclass

AliasChoices(
    choice: str | AliasPath, *extra: str | AliasPath
)

Bases: AliasChoices

A data class to specify a list of possible aliases for a model field.

This class is used in models and resources to specify one or multiple possible aliases for a field, allowing the model to accept different payload structures with variable field names but identical meanings.

Attributes:

Name Type Description
choices list[str | AliasPath]

A list of strings representing the alternative names for the model field. It can also include AliasPath objects for more complex aliasing scenarios.

Examples:

>>> from plateforme import BaseModel, Field
>>> class User(BaseModel):
...     first_name: str = Field(
...         validation_alias=AliasChoices('first_name', 'fname'))
...     last_name: str = Field(
...         validation_alias=AliasChoices('last_name', 'lname'))
>>> user_data = {'fname': 'John', 'lname': 'Doe'}
>>> user = User.model_validate(user_data)
>>> print(user)
first_name='John' last_name='Doe'
>>> user_data_alt = {'first_name': 'John', 'lname': 'Doe'}
>>> user = User.model_validate(user_data_alt)
>>> print(user)
first_name='John' last_name='Doe'
Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/aliases.py
def __init__(
    self, choice: str | AliasPath, *extra: str | AliasPath,
) -> None:
    self.choices = [choice] + list(extra)

convert_to_aliases

convert_to_aliases() -> list[list[str | int]]

Converts arguments to a list of lists containing string or integer aliases.

Returns:

Type Description
list[list[str | int]]

The list of aliases.

Source code in .venv/lib/python3.12/site-packages/pydantic/aliases.py
def convert_to_aliases(self) -> list[list[str | int]]:
    """Converts arguments to a list of lists containing string or integer aliases.

    Returns:
        The list of aliases.
    """
    aliases: list[list[str | int]] = []
    for c in self.choices:
        if isinstance(c, AliasPath):
            aliases.append(c.convert_to_aliases())
        else:
            aliases.append([c])
    return aliases

convert_to_alias

convert_to_alias() -> str

Converts arguments to an alias string.

It returns the first choice as an alias string. If the first choice is an AliasPath, it converts it to an alias string.

Returns:

Type Description
str

The first choice as an alias string.

Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/aliases.py
def convert_to_alias(self) -> str:
    """Converts arguments to an alias string.

    It returns the first choice as an alias string. If the first choice
    is an `AliasPath`, it converts it to an alias string.

    Returns:
        The first choice as an alias string.
    """
    first_choice = self.choices[0]
    if isinstance(first_choice, AliasPath):
        return first_choice.convert_to_alias()
    return str(first_choice)

AliasGenerator dataclass

AliasGenerator(
    alias: Callable[[str], str] | None = None,
    validation_alias: Callable[
        [str], str | AliasPath | AliasChoices
    ]
    | None = None,
    serialization_alias: Callable[[str], str] | None = None,
)

Bases: AliasGenerator

A data class used to specify generators to create various aliases.

It is a class that allows to specify multiple alias generators for a models and resources. It can be used to specify different alias generators for validation and serialization.

This is particularly useful in scenarios where it is necessary to generate different naming conventions for loading and saving data, but it is not desired to specify the validation and serialization aliases for each field individually.

Attributes:

Name Type Description
alias Callable[[str], str] | None

A callable that takes a field name and returns an alias for it.

validation_alias Callable[[str], str | AliasPath | AliasChoices] | None

A callable that takes a field name and returns a validation alias for it.

serialization_alias Callable[[str], str] | None

A callable that takes a field name and returns a serialization alias for it.

generate_aliases

generate_aliases(
    field_name: str,
) -> tuple[
    str | None,
    str | AliasPath | AliasChoices | None,
    str | None,
]

Generate aliases for a field.

It generates alias, validation_alias, and serialization_alias for a field using the class generators.

Returns:

Type Description
tuple[str | None, str | AliasPath | AliasChoices | None, str | None]

A tuple of three aliases - validation, alias, and serialization.

Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/aliases.py
def generate_aliases(self, field_name: str) -> tuple[
    str | None,
    str | AliasPath | AliasChoices | None,
    str | None
]:
    """Generate aliases for a field.

    It generates `alias`, `validation_alias`, and `serialization_alias`
    for a field using the class generators.

    Returns:
        A tuple of three aliases - validation, alias, and serialization.
    """
    alias = self._generate_alias('alias', (str,), field_name)
    validation_alias = self._generate_alias(
        'validation_alias', (str, AliasChoices, AliasPath), field_name
    )
    serialization_alias = self._generate_alias(
        'serialization_alias', (str,), field_name
    )

    return alias, validation_alias, serialization_alias  # type: ignore