omegaconf icon indicating copy to clipboard operation
omegaconf copied to clipboard

Add support for dataclasses._MISSING_TYPE

Open majiayu000 opened this issue 1 year ago • 0 comments

Describe the bug

In Python version >= 3.11, dataclasses prohibits setting the default value to mutable objects

so this is often the case : common: Example = field(default_factory=Example)

Here, if no assignment is given, the object becomes a _MISSING_TYPE.

When using this type in OmegaConf.structured, an error will occur.

Just like this https://github.com/facebookresearch/fairseq/pull/5359#issuecomment-1779634288

To Reproduce

Run the code below will get this error

from dataclasses import dataclass, field
from typing import List, Any
from omegaconf import OmegaConf


@dataclass
class Example:
    num: int


@dataclass
class TestConfig(Example):
    common: Example = field(default_factory=Example)


for k, field_info in TestConfig.__dataclass_fields__.items():
    default_value = field_info.default
    default_factory = field_info.default_factory
    print(
        f"Field: {k}, Default Value: {default_value}, Default Factory: {default_factory}"
    )

    mis = OmegaConf.structured(default_factory)

image

Expected behavior

Maybe add a type judgement for better compatibility.

Additional context

  • OmegaConf version: 2.4.0.dev1/2.3.0/2.2.0
  • Python version: 3.11
  • Operating system: windows

majiayu000 avatar Oct 26 '23 14:10 majiayu000