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

[FEATURE] Support for descriptor objects #549

Open
USSX-Hares opened this issue Oct 4, 2024 · 1 comment
Open

[FEATURE] Support for descriptor objects #549

USSX-Hares opened this issue Oct 4, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@USSX-Hares
Copy link
Collaborator

Description

  1. Fields with descriptors as default values should be treated differently, as they do for the "vanilla" dataclasses
  2. Fields with descriptors assigned as field(default=CoolDescriptor(), ...) should not use the descriptor value as the default __init__ argument, instead that argument should be missing.

Possible solution

No response

Alternatives

n/a

Context

No response

@USSX-Hares USSX-Hares added the enhancement New feature or request label Oct 4, 2024
@USSX-Hares
Copy link
Collaborator Author

Added a demo-tests for that.
There are two testcase classes, one proving that instantiating normally works, and one doing so by using DataClassJsonMixin.from_json(...). Tests are otherwise exactly the same (with one little exception regarding enums).
Classes used to test the bahaviour are listed below (descriptors do what you expect them to do from their name).

classes.py
import random
from dataclasses import dataclass, field
from enum import Enum, auto
from typing import *

from dataclasses_json import DataClassJsonMixin, config as dcj_config

from issue_549.descriptors import *


@dataclass
class User(DataClassJsonMixin):
    """ DataClass with descriptors assigned as default values """
    
    user_name: str = ToStrDescriptor()
    user_id: str = ToStrDescriptor(default_factory=lambda: random.randint(int(1e+5), int(1e+6)))

@dataclass
class Smartphone(DataClassJsonMixin):
    """ DataClass with descriptors assigned as field(default=) without additional metadata """
    
    manufacturer: str = 'Smartsung'
    battery_charge: float = field(default=PercentDescriptor(default=1.0))
    
    def explode(self):
        self.battery_charge = 0
        del self

class SemaphoreColor(Enum):
    RED     = auto()
    YELLOW  = auto()
    GREEN   = auto()

def enum_name(e: Enum) -> str: return e.name
_scd = EnumDescriptor(SemaphoreColor, default=SemaphoreColor.RED)
@dataclass
class Semaphore(DataClassJsonMixin):
    """ DataClass with descriptors assigned as field(default=) with encoder/decoder metadata """
    
    program: List[str] = field(default_factory=list)
    current_color: SemaphoreColor = field(default=_scd, metadata=dcj_config(encoder=enum_name, decoder=lambda val: _scd.__value_of__(None, val)))


__all__ = \
[
    'User',
    'Smartphone',
    'Semaphore',
    'SemaphoreColor',
]

issue_549.tar.gz

Also attaching the test reports (XML & HTML formats):
image
issue-549-reports.tar.gz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant