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

Support for dataclass fields of type Optional[List[X]] #58

Closed
kylegentle opened this issue Jan 22, 2019 · 2 comments
Closed

Support for dataclass fields of type Optional[List[X]] #58

kylegentle opened this issue Jan 22, 2019 · 2 comments

Comments

@kylegentle
Copy link

Summary

The library currently breaks for dataclasses with fields of type Optional[List[X]], returning the following error:

  File "dcj_bug/ex.py", line 66, in <module>
    Adult.schema().loads(test3, many=True)  # Fail
  File "<home_path>/.cache/pypoetry/virtualenvs/dcj-bug-py3.7/lib/python3.7/site-packages/dataclasses_json/api.py", line 100, in schema
    infer_missing)
  File "<home_path>/.cache/pypoetry/virtualenvs/dcj-bug-py3.7/lib/python3.7/site-packages/dataclasses_json/mm.py", line 86, in _make_default_fields
    cls)
  File "<home_path>/.cache/pypoetry/virtualenvs/dcj-bug-py3.7/lib/python3.7/site-packages/dataclasses_json/mm.py", line 103, in _make_default_field
    cons = _type_to_cons[cons_type]
KeyError: typing.List[__main__.Child]

I've attached some code below that demonstrates the issue.

Please let me know if there's any information or assistance I can provide; I dug into the code a bit in mm.py where it's breaking, but wasn't able to figure out what the correct logic should be in mm._make_default_field.

Example

import json

from dataclasses import dataclass
from typing import Optional, List

from dataclasses_json import DataClassJsonMixin


@dataclass
class Child(DataClassJsonMixin):
    name: str


@dataclass
class Adult(DataClassJsonMixin):
    name: str
    children: Optional[List[Child]] = None


test1 = """
{
    "name": "Foo"

}
""".strip()

test2 = """
{
    "name": "Bar",
    "children": [
        {
            "name": "Baz"
        },
        {
            "name": "Bat"
        }
    ]
}
""".strip()

test3 = """
[
    {
        "name": "Foo"
    },
    {
        "name": "Bar",
        "children": [
            {
                "name": "Baz"
            },
            {
                "name": "Bat"
            }
        ]
    }
]
""".strip()

test4 = json.loads(test3)

Adult.from_json(test1)  # OK
Adult.from_json(test2)  # OK

# Adult.from_json(test3)  # Breaks
Adult.schema().loads(test3, many=True)  # Breaks
# Adult.schema().load(test4, many=True)  # Breaks
@kylegentle
Copy link
Author

Here is a slightly hacky workaround to create lists of dataclass_json instances with fields of type Optional[List[X]]:

-# Adult.schema().load(test4, many=True)  # Breaks
+adults = [Adult.from_json(json.dumps(a)) for a in test4] # OK

This was referenced Feb 19, 2019
@lidatong
Copy link
Owner

Thanks for reporting this. Fixed in #66

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

No branches or pull requests

2 participants