konradhalas / dacite

Simple creation of data classes from dictionaries.
MIT License
1.72k stars 106 forks source link

MissingValueError using union operator with None (`| None`) but `Optional` works #201

Closed dylanmorroll closed 1 year ago

dylanmorroll commented 1 year ago

This code works fine:

from dataclasses import dataclass
from typing import Optional

import dacite

@dataclass
class Data:
    present: str
    optional: Optional[str]

data = {"present": "one"}

dacite.from_dict(Data, data)

However, when using | None, which is allowed by PEP 604, it no longer works.

from dataclasses import dataclass

import dacite

@dataclass
class Data:
    present: str
    optional: str | None

data = {"present": "one"}

dacite.from_dict(Data, data)

This gives the following error:

dacite.exceptions.MissingValueError: missing value for field "optional"

NB: I installed this fix and am still encountering the error, so it seems to be another issue.

idanmiara commented 1 year ago

This is duplicate of https://github.com/konradhalas/dacite/issues/180 You are welcome to use https://pypi.org/project/dacite2/ https://github.com/idanmiara/dacite/pull/1

konradhalas commented 1 year ago

@dylanmorroll should be fixed now