omry / omegaconf

Flexible Python configuration system. The last one you will ever need.
BSD 3-Clause "New" or "Revised" License
1.88k stars 98 forks source link

Fix interpolation to structured config from within typed list #1139

Closed Jasha10 closed 8 months ago

Jasha10 commented 8 months ago

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'}]}