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

Add support for dataclasses._MISSING_TYPE #1136

Open
majiayu000 opened this issue Oct 26, 2023 · 0 comments · May be fixed by #1137
Open

Add support for dataclasses._MISSING_TYPE #1136

majiayu000 opened this issue Oct 26, 2023 · 0 comments · May be fixed by #1137
Labels
bug Something isn't working

Comments

@majiayu000
Copy link

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant