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 interpolation to structured config from within typed list #1139

Merged
merged 1 commit into from
Nov 15, 2023

Conversation

Jasha10
Copy link
Collaborator

@Jasha10 Jasha10 commented Nov 7, 2023

This PR closes #1005, fixing a bug where a validation error was raised
when an interpolation points to a structured config from within a typed
list.

from dataclasses import dataclass, field
from omegaconf import MISSING, OmegaConf

@dataclass
class User:
    name: str = MISSING

@dataclass
class Config:
    user: User = User("John")
    users: list[User] = field(default_factory=lambda: ["${user}"])

cfg = OmegaConf.structured(Config)
OmegaConf.resolve(cfg)
print(cfg)

BEFORE

$ python repro.py
Traceback (most recent call last):
  ...
omegaconf.errors.ValidationError: Invalid type assigned: str is not a subclass of User. value: ${user}
    full_key: users[0]
    reference_type=List[User]
    object_type=list

AFTER

$ python repro.py
{'user': {'name': 'John'}, 'users': [{'name': 'John'}]}

This PR closes omry#1005, fixing a bug where a validation error was raised
when an interpolation points to a structured config from within a typed
list.

```python
from dataclasses import dataclass, field
from omegaconf import MISSING, OmegaConf

@DataClass
class User:
    name: str = MISSING

@DataClass
class Config:
    user: User = User("John")
    users: list[User] = field(default_factory=lambda: ["${user}"])

cfg = OmegaConf.structured(Config)
OmegaConf.resolve(cfg)
print(cfg)
```
BEFORE
------
```text
$ python repro.py
$ python repro.py
Traceback (most recent call last):
  ...
omegaconf.errors.ValidationError: Invalid type assigned: str is not a subclass of User. value: ${user}
    full_key: users[0]
    reference_type=List[User]
    object_type=list
```
AFTER
-----
```text
$ python repro.py
{'user': {'name': 'John'}, 'users': [{'name': 'John'}]}
```
Copy link
Owner

@omry omry left a comment

Choose a reason for hiding this comment

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

lgtm, thanks.

@Jasha10 Jasha10 merged commit c1333d7 into omry:master Nov 15, 2023
6 checks passed
@Jasha10 Jasha10 deleted the jasha-i1005 branch November 15, 2023 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Resolving nested structured config of Lists with interpolation raises ValidationError
2 participants