yaml-dataclass-config icon indicating copy to clipboard operation
yaml-dataclass-config copied to clipboard

Cannot get List[Union[str, CustomClass]] to work

Open umarcor opened this issue 3 years ago • 1 comments

I'm trying to write a single class which can read the following two files:

#test-logentry.yml

Log:
  - Time: '1 s'
    Severity: 'Info'
    Message: 'this is a message'
  - Time: '1.5 s'
    Severity: 'Warning'
    Message: 'this is a warning'
#test-str.yml

Log:
  - 'this is a message'
  - 'this is a warning'

I'm using the following script:

from typing import List, Union
from pathlib import Path
from dataclasses import dataclass
from yamldataclassconfig.config import YamlDataClassConfig


@dataclass
class Logentry(YamlDataClassConfig):
    """
    """
    Time: str = None
    Severity: str = None
    Message: str = None


@dataclass
class Testcase(YamlDataClassConfig):
    """
    """
    Log: List[Union[str, Logentry]] = None
    #Log: List[Logentry] = None
    #Log: List[str] = None


_cpath = Path('test-.yml')
_osvr = Testcase()
_osvr.load(_cpath)
_osvr.FILE_PATH = _cpath
print(_osvr.FILE_PATH)
print('Log:', _osvr.Log)

Reading test-str.yml works as expected:

# python test.py 
test-str.yml
Log: ['this is a message', 'this is a warning']

Reading test-logentry.yml produces the following warnings:

# python test.py 
C:/msys64/mingw64/lib/python3.8/site-packages/dataclasses_json/mm.py:108: UserWarning: The type "dict" (value: "{'Time': '1 s', 'Severity': 'Info', 'Message': 'this is a message'}") is not in the list of possible types of typing.Union (dataclass: Testcase, field: Log). Value cannot be deserialized properly.
  warnings.warn(
C:/msys64/mingw64/lib/python3.8/site-packages/dataclasses_json/mm.py:108: UserWarning: The type "dict" (value: "{'Time': '1.5 s', 'Severity': 'Warning', 'Message': 'this is a warning'}") is not in the list of possible 
types of typing.Union (dataclass: Testcase, field: Log). Value cannot be deserialized properly.
  warnings.warn(
test-logentry.yml
Log: [{'Time': '1 s', 'Severity': 'Info', 'Message': 'this is a message'}, {'Time': '1.5 s', 'Severity': 'Warning', 'Message': 'this is a warning'}]

However, if Log: List[Logentry] = None is used, instead of the Union, reading test-logentry.yml does not produce any warning:

# python test.py 
test-logentry.yml
Log: [Logentry(FILE_PATH=WindowsPath('T:/vunit/osvb/OSVR/testunion/config.yml'), Time='1 s', Severity='Info', Message='this is a message'), Logentry(FILE_PATH=WindowsPath('T:/vunit/osvb/OSVR/testunion/config.yml'), Time='1.5 s', Severity='Warning', Message='this is a warning')]

I would appreciate any hint for solving this issue.

I'm running the tests on MSYS2 (MINGW64) using Python 3.8.8 and yamldataclassconfig 1.5.0.

umarcor avatar Apr 06 '21 00:04 umarcor