Skip to content

Commit

Permalink
perf: cache the class attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
azmeuk committed Apr 7, 2024
1 parent 2cab4bf commit 6251455
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions canaille/backends/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,25 @@ class Model:
the value MUST be the same as the value of :attr:`~canaille.backends.models.Model.created`.
"""

_attributes = None

@classproperty
def attributes(cls):
annotations = ChainMap(
*(
typing.get_type_hints(klass)
for klass in reversed(cls.__mro__)
if issubclass(klass, Model)
if not cls._attributes:
annotations = ChainMap(
*(
typing.get_type_hints(klass)
for klass in reversed(cls.__mro__)
if issubclass(klass, Model)
)
)
)
# only keep types that are not typing.ClassVar
return {
key: value
for key, value in annotations.items()
if typing.get_origin(value) is not typing.ClassVar
}
# only keep types that are not typing.ClassVar
cls._attributes = {
key: value
for key, value in annotations.items()
if typing.get_origin(value) is not typing.ClassVar
}
return cls._attributes


class BackendModel:
Expand Down

0 comments on commit 6251455

Please sign in to comment.