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 List[Optional[Repository]] #73

Closed
mjmonroe opened this issue Mar 14, 2019 · 2 comments
Closed

Support for dataclass fields of List[Optional[Repository]] #73

mjmonroe opened this issue Mar 14, 2019 · 2 comments

Comments

@mjmonroe
Copy link

Similar to the issue, #58, but in this case List[Optional[Repository]] fails with an exception. Here is some sample code. The error occurred with dataclasses-json version 0.2.2

Error

/Users/PC/miniconda3/envs/Flask_async/lib/python3.7/site-packages/dataclasses_json/mm.py:70:
                UserWarning: Unknown type <class 'NoneType'> at Breaks.nodes: typing.List[typing.Union[__main__.Repository, NoneType]]
                It's advised to pass the correct marshmallow type to `mm_field`.
                warnings.warn(f"Unknown type {type_} at {cls.__name__}.{field.name}: {field.type} "
        Traceback (most recent call last):
          File "<input>", line 1, in <module>
          File "/Users/PC/miniconda3/envs/Flask_async/lib/python3.7/site-packages/dataclasses_json/api.py", line 76, in schema
            Schema = build_schema(cls, DataClassJsonMixin, infer_missing, partial)
          File "/Users/PC/miniconda3/envs/Flask_async/lib/python3.7/site-packages/dataclasses_json/mm.py", line 113, in build_schema
            schema_ = schema(cls, mixin, infer_missing)
          File "/Users/PC/miniconda3/envs/Flask_async/lib/python3.7/site-packages/dataclasses_json/mm.py", line 98, in schema
            t = build_type(type_, options, mixin, field, cls)
          File "/Users/PC/miniconda3/envs/Flask_async/lib/python3.7/site-packages/dataclasses_json/mm.py", line 73, in build_type
            return inner(type_, options)
          File "/Users/PC/miniconda3/envs/Flask_async/lib/python3.7/site-packages/dataclasses_json/mm.py", line 66, in inner
            args = [inner(a, {}) for a in getattr(type_, '__args__', [])]
          File "/Users/PC/miniconda3/envs/Flask_async/lib/python3.7/site-packages/dataclasses_json/mm.py", line 66, in <listcomp>
            args = [inner(a, {}) for a in getattr(type_, '__args__', [])]
          File "/Users/PC/miniconda3/envs/Flask_async/lib/python3.7/site-packages/dataclasses_json/mm.py", line 66, in inner
            args = [inner(a, {}) for a in getattr(type_, '__args__', [])]
          File "/Users/PC/miniconda3/envs/Flask_async/lib/python3.7/site-packages/dataclasses_json/mm.py", line 66, in <listcomp>
            args = [inner(a, {}) for a in getattr(type_, '__args__', [])]
          File "/Users/PC/miniconda3/envs/Flask_async/lib/python3.7/site-packages/dataclasses_json/mm.py", line 72, in inner
            return field.Field(**options)

Code example:

from dataclasses import dataclass
from dataclasses_json import dataclass_json
from typing import List, Optional

@dataclass_json
@dataclass
class Repository:
    name: str
    stargazers: str

@dataclass_json
@dataclass
class Breaks:
    errors_with_trace = ''"
    nodes: List[Optional[Repository]]


@dataclass_json
@dataclass
class Works:
    nodes: Optional[List[Repository]]

try:
    Breaks.schema()
except AttributeError as exc:
    print(exc)

Works.schema()
@Peilonrayz
Copy link
Contributor

You're missing the error which is raised -> AttributeError: 'Field' object has no attribute 'Field'.

This is a know issue, and the fix is changing field to fields. Changing the code to include this fix makes your code not error. Could you perform the fix on your side, and check if it works ok afterwords?

@mjmonroe
Copy link
Author

I tried your change and it fixed the error. Thanks!

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