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

ValidationError for forward references inside a container #1174

Open
4 tasks done
D-Sokol opened this issue Apr 23, 2024 · 0 comments
Open
4 tasks done

ValidationError for forward references inside a container #1174

D-Sokol opened this issue Apr 23, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@D-Sokol
Copy link

D-Sokol commented Apr 23, 2024

Describe the bug
I have a dataclass config and want to convert it into a YAML representation via OmegaConf, but it raises a ValidationError since I use forward reference in the list.
Here is the simplified version of my code:

from dataclasses import dataclass
from omegaconf import OmegaConf
from typing import Optional


@dataclass
class Tree:
    value: int = -1
    children: Optional[list["Tree"]] = None


mytree = Tree(value=1, children=[Tree(value=2)])

config = OmegaConf.structured(mytree)
print(config)

Expected behavior
Output {'value': 1, 'children': [{'value': 2, 'children': None}]}, no error

Actual behavior

omegaconf.errors.ValidationError: Unsupported value type: 'ForwardRef('Tree')'
    full_key: children
    object_type=None

I believe this is a bug since the following very similar code produces an expected result:

@dataclass
class Tree:
    value: int = -1
    child: Optional["Tree"] = None


mytree = Tree(value=1, child=Tree(value=2))

config = OmegaConf.structured(mytree)
print(config)
# {'value': 1, 'child': {'value': 2, 'child': None}}

Additional context

  • OmegaConf version: 2.3.0
  • Python version: Python 3.9.2
  • Operating system: 5.10.0-28-amd64 #1 SMP Debian 5.10.209-2 (2024-01-31) x86_64 GNU/Linux
  • Please provide a minimal repro
@D-Sokol D-Sokol added the bug Something isn't working label Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant