Skip to content

Tags

albert.resources.tags

TagEntity

Bases: str, Enum

TagEntity is an enumeration of possible tag entities.

Attributes:

Name Type Description
INVENTORY
COMPANY

INVENTORY

INVENTORY = 'Inventory'

COMPANY

COMPANY = 'Company'

Tag

Bases: BaseResource

Tag is a Pydantic model representing a tag entity.

Attributes:

Name Type Description
tag str

The name of the tag.

id str | None

The Albert ID of the tag. Set when the tag is retrieved from Albert.

Methods:

Name Description
from_string

Creates a Tag object from a string.

tag

tag: str = Field(
    alias=AliasChoices("name", "tagName"),
    serialization_alias="name",
)

id

id: str | None = Field(
    None,
    alias=AliasChoices("albertId", "tagId"),
    serialization_alias="albertId",
)

from_string

from_string(tag: str) -> Tag

Creates a Tag object from a string.

Parameters:

Name Type Description Default
tag str

The name of the tag.

required

Returns:

Type Description
Tag

The Tag object created from the string.

Source code in src/albert/resources/tags.py
@classmethod
def from_string(cls, tag: str) -> "Tag":
    """
    Creates a Tag object from a string.

    Parameters
    ----------
    tag : str
        The name of the tag.

    Returns
    -------
    Tag
        The Tag object created from the string.
    """
    return cls(tag=tag)

BaseTaggedEntity

Bases: BaseResource

BaseTaggedEntity is a Pydantic model that includes functionality for handling tags as either Tag objects or strings.

Attributes:

Name Type Description
tags List[Tag | str] | None

A list of Tag objects or strings representing tags.

Methods:

Name Description
convert_tags

tags

tags: list[SerializeAsEntityLink[Tag]] | None = Field(
    None, alias="Tags"
)

convert_tags

convert_tags(data: dict[str, Any]) -> dict[str, Any]
Source code in src/albert/resources/tags.py
@model_validator(mode="before")  # must happen before to keep type validation
@classmethod
def convert_tags(cls, data: dict[str, Any]) -> dict[str, Any]:
    if not isinstance(data, dict):
        return data
    tags = data.get("tags")
    if not tags:
        tags = data.get("Tags")
    if tags:
        new_tags = []
        for t in tags:
            if isinstance(t, Tag):
                new_tags.append(t)
            elif isinstance(t, str):
                new_tags.append(Tag.from_string(t))
            elif isinstance(t, dict):
                new_tags.append(Tag(**t))
            else:
                # We do not expect this else to be hit because tags should only be Tag or str
                logger.warning(f"Unexpected value for Tag. {t} of type {type(t)}")
                continue
        data["tags"] = new_tags
    return data