Skip to content

Commit

Permalink
Include class fields even if they are only defined with a type hint.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamghill committed Oct 11, 2023
1 parent bdb6925 commit d27c84d
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion django_unicorn/components/unicorn_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from django_unicorn.settings import get_setting
from django_unicorn.utils import (
cache_full_tree,
get_type_hints,
is_non_string_sequence,
restore_from_cache,
)
Expand Down Expand Up @@ -581,9 +582,17 @@ def _attribute_names(self) -> List[str]:
"""
Gets publicly available attribute names. Cached in `_attribute_names_cache`.
"""

non_callables = [member[0] for member in inspect.getmembers(self, lambda x: not callable(x))]
attribute_names = [name for name in non_callables if self._is_public(name)]

# Add type hints for the component to the attribute names since in some
# instances they won't be returned from `getmembers`
for type_hint_attribute_name in get_type_hints(self).keys():
if self._is_public(type_hint_attribute_name):
if type_hint_attribute_name not in attribute_names:
attribute_names.append(type_hint_attribute_name)

return attribute_names

@timed
Expand All @@ -596,7 +605,7 @@ def _attributes(self) -> Dict[str, Any]:
attributes = {}

for attribute_name in attribute_names:
attributes[attribute_name] = getattr(self, attribute_name)
attributes[attribute_name] = getattr(self, attribute_name, None)

return attributes

Expand Down

0 comments on commit d27c84d

Please sign in to comment.