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

fix: get_attr_data doesn't handle default factory correctly #1134

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions news/945.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix handling of `attrs` classes that use a default factory (`attrs.Factory`).
16 changes: 14 additions & 2 deletions omegaconf/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,21 @@ def get_attr_data(obj: Any, allow_objects: Optional[bool] = None) -> Dict[str, A
if not is_type:
value = getattr(obj, name)
else:
value = attrib.default
if value == attr.NOTHING:
default = attrib.default
if default == attr.NOTHING:
value = MISSING
elif isinstance(default, attr.Factory): # type: ignore
if default.takes_self: # type: ignore
e = ConfigValueError(
"'takes_self' in attrs attributes is not supported\n"
+ f"{name}: {type_str(type_)}"
)
format_and_raise(
node=None, key=None, value=default, cause=e, msg=str(e)
)
value = default.factory() # type: ignore
else:
value = default
if is_union_annotation(type_) and not is_supported_union_annotation(type_):
e = ConfigValueError(
f"Unions of containers are not supported:\n{name}: {type_str(type_)}"
Expand Down
Loading