Skip to content

Commit 445c9b2

Browse files
qwintblack-sliver
andauthored
Settings: Handle empty Groups (#4576)
* export empty groups as an empty dict instead of crashing * Update settings.py Co-authored-by: black-sliver <59490463+black-sliver@users.noreply.github.com> * check instance values from self as well * Apply suggestions from code review Co-authored-by: black-sliver <59490463+black-sliver@users.noreply.github.com> --------- Co-authored-by: black-sliver <59490463+black-sliver@users.noreply.github.com>
1 parent 67e8877 commit 445c9b2

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

settings.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def changed(self) -> bool:
109109
def get_type_hints(cls) -> Dict[str, Any]:
110110
"""Returns resolved type hints for the class"""
111111
if cls._type_cache is None:
112-
if not isinstance(next(iter(cls.__annotations__.values())), str):
112+
if not cls.__annotations__ or not isinstance(next(iter(cls.__annotations__.values())), str):
113113
# non-str: assume already resolved
114114
cls._type_cache = cls.__annotations__
115115
else:
@@ -270,11 +270,15 @@ def dump(self, f: TextIO, level: int = 0) -> None:
270270
# fetch class to avoid going through getattr
271271
cls = self.__class__
272272
type_hints = cls.get_type_hints()
273+
entries = [e for e in self]
274+
if not entries:
275+
# write empty dict for empty Group with no instance values
276+
cls._dump_value({}, f, indent=" " * level)
273277
# validate group
274278
for name in cls.__annotations__.keys():
275279
assert hasattr(cls, name), f"{cls}.{name} is missing a default value"
276280
# dump ordered members
277-
for name in self:
281+
for name in entries:
278282
attr = cast(object, getattr(self, name))
279283
attr_cls = type_hints[name] if name in type_hints else attr.__class__
280284
attr_cls_origin = typing.get_origin(attr_cls)

0 commit comments

Comments
 (0)