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

OmegaConf.structured does not parse dataclass attributes which are not typed #489

Closed
carmocca opened this issue Jan 22, 2021 · 1 comment
Labels
bug Something isn't working

Comments

@carmocca
Copy link

Describe the bug

OmegaConf.structured does not parse dataclass attributes which are not typed

To Reproduce

from dataclasses import dataclass
from omegaconf import OmegaConf

@dataclass
class Foo:
    z = "test"  # No type

x = Foo()
y = OmegaConf.structured(x)
print(y) # {}

@dataclass
class Foo:
    z: str = "test"  # Typed

x = Foo()
y = OmegaConf.structured(x)
print(y) # {'z': 'test'}

Expected behavior
Both print the same thing

Alternatives
It works if I the type is set to Any. However, for my use case, I want to allow users to pass their own dataclasses, so I don't want to force them to have their dataclasses completely typed.

Additional context

  • OmegaConf version: 2.1.0.dev14
  • Python version: 3.8.6
  • Operating system : Linux Pop-os
@carmocca carmocca added the bug Something isn't working label Jan 22, 2021
@omry
Copy link
Owner

omry commented Jan 22, 2021

Thanks for reporting.
This seems to be a problem in dataclasses and attr classes as well:

from dataclasses import dataclass, fields

@dataclass
class Foo:
    untyped = "foo"
    typed: str = "bar"

def print_fields(obj):
    obj_fields = fields(obj)
    for idx, field in enumerate(obj_fields):
        print(f"#{idx}: {field.name}")

print_fields(Foo)

Per BDFL, this is by design.
without a type it's not a field.

If you can find a clean way to access these fields (that works for both dataclasses and attr classes and instances) I am open to a PR.

Closing, feel free to continue the discussion here though.

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

No branches or pull requests

2 participants