omry / omegaconf

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

Structured Config schema Type validation of "list[list[float]]" does not accept 'int' #1054

Open luobroad opened 1 year ago

luobroad commented 1 year ago

Describe the bug See the following reproducing code.

To Reproduce Here I have a .py file:

from dataclasses import dataclass, field
from typing import Union, Optional
import hydra
from omegaconf import OmegaConf
from hydra.core.config_store import ConfigStore

@dataclass
class Config:
    list_of_list: list[list[float]]

cs = ConfigStore().instance()
cs.store(name="base_config", node=Config)

@hydra.main(version_base=None, config_path='configs', config_name='reprod')
def my_app(cfg : Config) -> None:
    print(OmegaConf.to_yaml(cfg))

if __name__ == "__main__":
    my_app()

and a reprod.yaml file

list_of_list: [[2]]

defaults:
  - base_config
  - _self_

Expected behavior It gives error message as following:

In 'reprod': ValidationError raised while composing config:
Value 2 (int) is incompatible with type hint 'float'
    full_key: list_of_list[0]
    reference_type=List[List[float]]
    object_type=list

Additional context

Jasha10 commented 1 year ago

Thanks for the bug report, @luobroad.

luobroad commented 1 year ago

Add a similar bug: type Optional[Tuple[float, int]] does not work as well, the second element in the tuple will still be float rather than int. Is this an implementation bug of omegaconf.ListConfig?

Jasha10 commented 1 year ago

I'm not sure if this is specific to ListConfig -- it might be possible to find similar behavior with nested DictConfig as well.