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>
This commit is contained in:
parent
67e8877143
commit
445c9b22d6
|
@ -109,7 +109,7 @@ class Group:
|
||||||
def get_type_hints(cls) -> Dict[str, Any]:
|
def get_type_hints(cls) -> Dict[str, Any]:
|
||||||
"""Returns resolved type hints for the class"""
|
"""Returns resolved type hints for the class"""
|
||||||
if cls._type_cache is None:
|
if cls._type_cache is None:
|
||||||
if not isinstance(next(iter(cls.__annotations__.values())), str):
|
if not cls.__annotations__ or not isinstance(next(iter(cls.__annotations__.values())), str):
|
||||||
# non-str: assume already resolved
|
# non-str: assume already resolved
|
||||||
cls._type_cache = cls.__annotations__
|
cls._type_cache = cls.__annotations__
|
||||||
else:
|
else:
|
||||||
|
@ -270,11 +270,15 @@ class Group:
|
||||||
# fetch class to avoid going through getattr
|
# fetch class to avoid going through getattr
|
||||||
cls = self.__class__
|
cls = self.__class__
|
||||||
type_hints = cls.get_type_hints()
|
type_hints = cls.get_type_hints()
|
||||||
|
entries = [e for e in self]
|
||||||
|
if not entries:
|
||||||
|
# write empty dict for empty Group with no instance values
|
||||||
|
cls._dump_value({}, f, indent=" " * level)
|
||||||
# validate group
|
# validate group
|
||||||
for name in cls.__annotations__.keys():
|
for name in cls.__annotations__.keys():
|
||||||
assert hasattr(cls, name), f"{cls}.{name} is missing a default value"
|
assert hasattr(cls, name), f"{cls}.{name} is missing a default value"
|
||||||
# dump ordered members
|
# dump ordered members
|
||||||
for name in self:
|
for name in entries:
|
||||||
attr = cast(object, getattr(self, name))
|
attr = cast(object, getattr(self, name))
|
||||||
attr_cls = type_hints[name] if name in type_hints else attr.__class__
|
attr_cls = type_hints[name] if name in type_hints else attr.__class__
|
||||||
attr_cls_origin = typing.get_origin(attr_cls)
|
attr_cls_origin = typing.get_origin(attr_cls)
|
||||||
|
|
Loading…
Reference in New Issue