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

dataclass-wizard only use the inner class name in nested classes, and cause problem in nested classes. #123

Open
wanmeihuali opened this issue Jul 23, 2024 · 1 comment
Labels
acknowledged awaiting-user-input Awaiting user input / feedback. bug Something isn't working low-priority Low Priority

Comments

@wanmeihuali
Copy link

  • Dataclass Wizard version: 0.22.3
  • Python version: 3.8
  • Operating System: ubuntu20

Description

I'm trying to have to use data class-wizard for config, and I have some code like:

class A:
    @dataclass
    class Config(JSONWizard):
        name: str = "A"
        x: int = 0
        y: int = 0

    def __init__(self, config: Config):
        self.config = config

class B:
    @dataclass
    class Config(JSONWizard):
        name: str = "A"
        x: int = 0
        z: int = 0

    def __init__(self, config: Config):
        self.config = config


@dataclass
class ConfigC(JSONWizard):
    class Meta(JSONWizard.Meta):
        tag_key = 'type'
        auto_assign_tags = True
    sub_config: A.Config | B.Config = field(default_factory=A.Config)
    sub_name: str = "A"


config = ConfigC(sub_config=A.Config())
new_config = ConfigC.from_json(config.to_json())
assert isinstance(new_config.sub_config, A.Config) # <----------- assert fails
print(config.to_json()) # '{"subConfig": {"name": "A", "x": 0, "y": 0, "type": "Config"}, "subName": "A"}'

And it turns out that dataclass-wizard only use the inner class name in nested classes. Is this issue fixed in later version?

@rnag rnag added bug Something isn't working acknowledged low-priority Low Priority labels Nov 24, 2024
@rnag
Copy link
Owner

rnag commented Nov 24, 2024

@wanmeihuali Thanks for opening the issue. I didn't even think about nested classes and that they could be used in Union types. Can you expand on your use case?

Also, does it work if you manually assign tags, instead of auto_assign_tags Meta field? I tested below example and got the correct/intended result:

from dataclasses import dataclass, field

from dataclass_wizard import JSONWizard


class A:
    @dataclass
    class Config(JSONWizard):
        class _(JSONWizard.Meta):
            tag = __qualname__  # or: 'A.Config'

        name: str = "A"
        x: int = 0
        y: int = 0

    def __init__(self, config: Config):
        self.config = config

class B:
    @dataclass
    class Config(JSONWizard):
        class _(JSONWizard.Meta):
            tag = __qualname__  # or: 'B.Config'

        name: str = "A"
        x: int = 0
        z: int = 0

    def __init__(self, config: Config):
        self.config = config


@dataclass
class ConfigC(JSONWizard):
    class Meta(JSONWizard.Meta):
        tag_key = 'type'
    sub_config: A.Config | B.Config = field(default_factory=A.Config)
    sub_name: str = "A"


config = ConfigC(sub_config=A.Config())
new_config = ConfigC.from_json(config.to_json())

print(repr(new_config.sub_config))
assert isinstance(new_config.sub_config, A.Config) # <----------- assert succeeds
print(config.to_json()) # '{"subConfig": {"name": "A", "x": 0, "y": 0, "type": "A.Config"}, "subName": "A"}'

@rnag rnag added the awaiting-user-input Awaiting user input / feedback. label Nov 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
acknowledged awaiting-user-input Awaiting user input / feedback. bug Something isn't working low-priority Low Priority
Projects
None yet
Development

No branches or pull requests

2 participants