dataclasses-json icon indicating copy to clipboard operation
dataclasses-json copied to clipboard

UserWarning: Unknown type typing.Tuple [bug]

Open AndreyMZ opened this issue 5 years ago • 1 comments

Steps to reproduce

Run the following tests by pytest:

import warnings
from dataclasses import dataclass
from typing import Tuple, List

from dataclasses_json import DataClassJsonMixin

@dataclass
class Foo(DataClassJsonMixin):
    bar: List[Tuple[str, int]]

def test_many_foo_from_dict():
    foos = [
        {
            'bar': [
                ["item1", 1],
                ["item2", 2],
            ]
        }
    ]
    Foo.schema().load(foos, many=True)

Actual result

There is a warning:

C:\Users\user\.virtualenvs\project-cKk0Zhor\lib\site-packages\dataclasses_json\mm.py:270: UserWarning: Unknown type typing.Tuple[str, int] at Foo.bar: typing.List[typing.Tuple[str, int]] It's advised to pass the correct marshmallow type to `mm_field`.

Expected result

No warnings.

AndreyMZ avatar Mar 12 '20 10:03 AndreyMZ

I think this is because there is no mapping of the Python type tuple to fields.Tuple in https://github.com/lidatong/dataclasses-json/blob/3dc59e01ccdfec619ee4e4c3502b9759b67c3fa8/dataclasses_json/mm.py#L116-L134.

Before the lookup, this is called: https://github.com/lidatong/dataclasses-json/blob/3dc59e01ccdfec619ee4e4c3502b9759b67c3fa8/dataclasses_json/mm.py#L251

In case of a typing.Tuple, __origin__ is defined and points to tuple (the Python type).

However, naively plugging it in breaks.

patzm avatar Nov 21 '20 19:11 patzm