perf: cache the class attributes

This commit is contained in:
Éloi Rivard 2024-04-07 19:22:54 +02:00
parent 2cab4bfa66
commit 6251455a1d
No known key found for this signature in database
GPG key ID: 7EDA204EA57DD184

View file

@ -44,8 +44,11 @@ class Model:
the value MUST be the same as the value of :attr:`~canaille.backends.models.Model.created`.
"""
_attributes = None
@classproperty
def attributes(cls):
if not cls._attributes:
annotations = ChainMap(
*(
typing.get_type_hints(klass)
@ -54,11 +57,12 @@ class Model:
)
)
# only keep types that are not typing.ClassVar
return {
cls._attributes = {
key: value
for key, value in annotations.items()
if typing.get_origin(value) is not typing.ClassVar
}
return cls._attributes
class BackendModel: