Skip to content

cannot convert to YAML with nested dataclass type-hints for dict values #858

Description

@vttrifonov

I want to be able to convert a given object back into a YAML using hz.just but it fails. Or rather OmegaConf fails because the objects which hz.just creates are not the same as what the type hints expect. So the following snippet

import hydra_zen as hz
from dataclasses import dataclass

@dataclass
class Inner:
    x: int = 0

@dataclass
class Outer:
    outer: dict[str, Inner] 
    #outer: Inner

x = Outer(outer={'a': Inner(x=1)})
#x = Outer(outer=Inner(x=1))
cfg = hz.just(x)
print(hz.to_yaml(cfg))

fails with

ValidationError: Invalid type assigned: Builds_Inner is not a subclass of Inner. value: <class 'types.Builds_Inner'>
    full_key: a
    object_type=None

If I change (swap in commented lines) to Inner instead of dict of Inner values it works. So it is not entirely impossible... But my use-case has dict and it feels like this should work. A workaround is to abandon the type-hint for Inner and use Any. But I would like to keep the stricter hints: it will be too intrusive on the code-base and not very pythonic either.

hydra-zen is full wonderful surprises so maybe i am missing something :-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions