Skip to content

trait

Defines the base trait model and representation.

IncompatibleTraitVersionError

Bases: Exception

Incompatible trait version exception.

This exception is raised when the trait version is incompatible with the current version of the trait.

Source code in client/ayon_core/pipeline/traits/trait.py
92
93
94
95
96
97
class IncompatibleTraitVersionError(Exception):
    """Incompatible trait version exception.

    This exception is raised when the trait version is incompatible with the
    current version of the trait.
    """

LooseMatchingTraitError

Bases: Exception, Generic[T]

Loose matching trait exception.

This exception is raised when the trait is found with a loose matching criteria.

Source code in client/ayon_core/pipeline/traits/trait.py
112
113
114
115
116
117
118
119
120
class LooseMatchingTraitError(Exception, Generic[T]):
    """Loose matching trait exception.

    This exception is raised when the trait is found with a loose matching
    criteria.
    """

    found_trait: T
    expected_id: str

MissingTraitError

Bases: TypeError

Missing trait error exception.

This exception is raised when the trait is missing.

Source code in client/ayon_core/pipeline/traits/trait.py
143
144
145
146
147
class MissingTraitError(TypeError):
    """Missing trait error exception.

    This exception is raised when the trait is missing.
    """

TraitBase dataclass

Bases: ABC

Base trait model.

This model must be used as a base for all trait models. id, name, and description are abstract attributes that must be implemented in the derived classes.

Source code in client/ayon_core/pipeline/traits/trait.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
@dataclass
class TraitBase(ABC):
    """Base trait model.

    This model must be used as a base for all trait models.
    ``id``, ``name``, and ``description`` are abstract attributes that must be
    implemented in the derived classes.
    """

    @property
    @abstractmethod
    def id(self) -> str:
        """Abstract attribute for ID."""
        ...

    @property
    @abstractmethod
    def name(self) -> str:
        """Abstract attribute for name."""
        ...

    @property
    @abstractmethod
    def description(self) -> str:
        """Abstract attribute for description."""
        ...

    def validate_trait(self, representation: Representation) -> None:  # noqa: PLR6301
        """Validate the trait.

        This method should be implemented in the derived classes to validate
        the trait data. It can be used by traits to validate against other
        traits in the representation.

        Args:
            representation (Representation): Representation instance.

        """
        return

    @classmethod
    def get_version(cls) -> Optional[int]:
        # sourcery skip: use-named-expression
        """Get a trait version from ID.

        This assumes Trait ID ends with `.v{version}`. If not, it will
        return None.

        Returns:
            Optional[int]: Trait version

        """
        version_regex = r"v(\d+)$"
        match = re.search(version_regex, str(cls.id))
        return int(match[1]) if match else None

    @classmethod
    def get_versionless_id(cls) -> str:
        """Get a trait ID without a version.

        Returns:
            str: Trait ID without a version.

        """
        return re.sub(r"\.v\d+$", "", str(cls.id))

    def as_dict(self) -> dict:
        """Return a trait as a dictionary.

        Returns:
            dict: Trait as dictionary.

        """
        return asdict(self)

description abstractmethod property

Abstract attribute for description.

id abstractmethod property

Abstract attribute for ID.

name abstractmethod property

Abstract attribute for name.

as_dict()

Return a trait as a dictionary.

Returns:

Name Type Description
dict dict

Trait as dictionary.

Source code in client/ayon_core/pipeline/traits/trait.py
82
83
84
85
86
87
88
89
def as_dict(self) -> dict:
    """Return a trait as a dictionary.

    Returns:
        dict: Trait as dictionary.

    """
    return asdict(self)

get_version() classmethod

Get a trait version from ID.

This assumes Trait ID ends with .v{version}. If not, it will return None.

Returns:

Type Description
Optional[int]

Optional[int]: Trait version

Source code in client/ayon_core/pipeline/traits/trait.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
@classmethod
def get_version(cls) -> Optional[int]:
    # sourcery skip: use-named-expression
    """Get a trait version from ID.

    This assumes Trait ID ends with `.v{version}`. If not, it will
    return None.

    Returns:
        Optional[int]: Trait version

    """
    version_regex = r"v(\d+)$"
    match = re.search(version_regex, str(cls.id))
    return int(match[1]) if match else None

get_versionless_id() classmethod

Get a trait ID without a version.

Returns:

Name Type Description
str str

Trait ID without a version.

Source code in client/ayon_core/pipeline/traits/trait.py
72
73
74
75
76
77
78
79
80
@classmethod
def get_versionless_id(cls) -> str:
    """Get a trait ID without a version.

    Returns:
        str: Trait ID without a version.

    """
    return re.sub(r"\.v\d+$", "", str(cls.id))

validate_trait(representation)

Validate the trait.

This method should be implemented in the derived classes to validate the trait data. It can be used by traits to validate against other traits in the representation.

Parameters:

Name Type Description Default
representation Representation

Representation instance.

required
Source code in client/ayon_core/pipeline/traits/trait.py
43
44
45
46
47
48
49
50
51
52
53
54
def validate_trait(self, representation: Representation) -> None:  # noqa: PLR6301
    """Validate the trait.

    This method should be implemented in the derived classes to validate
    the trait data. It can be used by traits to validate against other
    traits in the representation.

    Args:
        representation (Representation): Representation instance.

    """
    return

TraitValidationError

Bases: Exception

Trait validation error exception.

This exception is raised when the trait validation fails.

Source code in client/ayon_core/pipeline/traits/trait.py
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
class TraitValidationError(Exception):
    """Trait validation error exception.

    This exception is raised when the trait validation fails.
    """

    def __init__(self, scope: str, message: str):
        """Initialize the exception.

        We could determine the scope from the stack in the future,
        provided the scope is always Trait name.

        Args:
            scope (str): Scope of the error.
            message (str): Error message.

        """
        super().__init__(f"{scope}: {message}")

__init__(scope, message)

Initialize the exception.

We could determine the scope from the stack in the future, provided the scope is always Trait name.

Parameters:

Name Type Description Default
scope str

Scope of the error.

required
message str

Error message.

required
Source code in client/ayon_core/pipeline/traits/trait.py
129
130
131
132
133
134
135
136
137
138
139
140
def __init__(self, scope: str, message: str):
    """Initialize the exception.

    We could determine the scope from the stack in the future,
    provided the scope is always Trait name.

    Args:
        scope (str): Scope of the error.
        message (str): Error message.

    """
    super().__init__(f"{scope}: {message}")

UpgradableTraitError

Bases: Exception, Generic[T]

Upgradable trait version exception.

This exception is raised when the trait can upgrade existing data meant for older versions of the trait. It must implement an upgrade method that will take old trait data as an argument to handle the upgrade.

Source code in client/ayon_core/pipeline/traits/trait.py
100
101
102
103
104
105
106
107
108
109
class UpgradableTraitError(Exception, Generic[T]):
    """Upgradable trait version exception.

    This exception is raised when the trait can upgrade existing data
    meant for older versions of the trait. It must implement an `upgrade`
    method that will take old trait data as an argument to handle the upgrade.
    """

    trait: T
    old_data: dict