Skip to content

Add support for dataclasses._MISSING_TYPE #1136

@majiayu000

Description

@majiayu000

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 facebookresearch/fairseq#5359 (comment)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions