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

Containers of extended types do not work [bug] #191

Open
AndreyMZ opened this issue Mar 12, 2020 · 1 comment · May be fixed by #357
Open

Containers of extended types do not work [bug] #191

AndreyMZ opened this issue Mar 12, 2020 · 1 comment · May be fixed by #357
Labels
bug Something isn't working

Comments

@AndreyMZ
Copy link

AndreyMZ commented Mar 12, 2020

Steps to reproduce

Run the following tests by pytest:

from dataclasses import dataclass
from datetime import datetime, timezone
from decimal import Decimal
from typing import List
from uuid import UUID, uuid4

from dataclasses_json import DataClassJsonMixin


@dataclass
class Foo(DataClassJsonMixin):
    f1: datetime
    f2: Decimal
    f3: UUID

@dataclass
class Bar(DataClassJsonMixin):
    f1: List[datetime]
    f2: List[Decimal]
    f3: List[UUID]


def test_foo():
    foo_obj = Foo(
        f1=datetime.now(tz=timezone.utc),
        f2=Decimal(314),
        f3=uuid4(),
    )
    assert Foo.from_json(foo_obj.to_json()) == foo_obj

def test_bar():
    bar_obj = Bar(
        f1=[datetime.now(tz=timezone.utc)],
        f2=[Decimal(314)],
        f3=[uuid4()],
    )
    assert Bar.from_json(bar_obj.to_json()) == bar_obj

Actual result

test_foo passed.
test_bar failed:

Expected :Bar(f1=[datetime.datetime(2020, 3, 13, 6, 12, 1, 961388, tzinfo=datetime.timezone.utc)], f2=[Decimal('314')], f3=[UUID('8a82b5b9-d284-48c6-b4cf-a2bba2bbc8fb')])
Actual   :Bar(f1=[1584079921.961388], f2=['314'], f3=['8a82b5b9-d284-48c6-b4cf-a2bba2bbc8fb'])

Expected result

Both tests passed.

@lidatong lidatong added the bug Something isn't working label Mar 20, 2020
@Fraggle
Copy link

Fraggle commented Mar 26, 2020

@AndreyMZ FYI, in the meantime you can fix it by doing something like that for extended types:

from dataclasses import dataclass, field
from datetime import date
from dataclasses_json import dataclass_json, config
from marshmallow import fields

@dataclass_json
@dataclass
class myDataclass:
  date_field:date = field(
        metadata=config(
            encoder=lambda d: date.isoformat(d) if d else None,
            decoder=lambda d: date.fromisoformat(d) if d else None,
            mm_field=fields.Date(format="iso"),
        )
  )

@lidatong Thanks for looking into it, we have this kind of workaround scattered in our codebase and it's quite brittle as if you forget to add it, you will end up with date vs str or uuid vs str etc.. it breaks comparisons.

dax added a commit to dax/dataclasses-json that referenced this issue Apr 27, 2022
@dax dax linked a pull request Apr 27, 2022 that will close this issue
dax added a commit to dax/dataclasses-json that referenced this issue Sep 22, 2022
dax added a commit to dax/dataclasses-json that referenced this issue Jun 21, 2023
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.

3 participants