Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Settings: Handle empty Groups #4576

Merged
merged 4 commits into from
Feb 1, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def changed(self) -> bool:
def get_type_hints(cls) -> Dict[str, Any]:
"""Returns resolved type hints for the class"""
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
cls._type_cache = cls.__annotations__
else:
Expand Down Expand Up @@ -270,6 +270,9 @@ def dump(self, f: TextIO, level: int = 0) -> None:
# fetch class to avoid going through getattr
cls = self.__class__
type_hints = cls.get_type_hints()
if not type_hints:
# write empty dict for empty Group
cls._dump_value(type_hints, f, indent=" " * level)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds {} if the class is empty, but loading a yaml can add values to the instance that will then get dumped in line 289.

qwint marked this conversation as resolved.
Show resolved Hide resolved
# validate group
for name in cls.__annotations__.keys():
assert hasattr(cls, name), f"{cls}.{name} is missing a default value"
Expand Down
Loading