-
Notifications
You must be signed in to change notification settings - Fork 152
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
[BUG] Dataclass.from_json with Union type fields infers missing fields and fails in the wrong spot #488
Labels
bug
Something isn't working
Comments
I was able to reproduce this bug with a few simplifications to the code snippet:
Which results in:
For @pingretispec I would recommend using the
Which does the encoding and decoding successfully:
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description
I have a Message json dataclass that has a field namely "record" that can be dataclass Record1, Record2, or Record3.
The code fails trying to infer record with dataclass Record1.
Code snippet that reproduces the issue
@dataclass_json
@DataClass
class Message:
type: MessageType
op: Operation
record: Union[Record1, Record2]
@dataclass_json
@DataClass
class Record1:
id: uuid.UUID
date: datetime.date
count: int
@dataclass_json
@DataClass
class Record2:
id: uuid.UUID
date: datetime.date
qty: int
record = Record2(id='001', date=datetime.date(2022,9,22), qty=10)
message = Message(type=MessageType.COUNT, op=Operation.CREATE, record=record)
msg_json = json.dump(message.to_dict(), json_path)
with open(json_path) as json_f:
msg = Message.from_json(json_f.read())
Describe the results you expected
msg = Message(type="COUNT", op="CREATE", record=Record2(id="001", date=datetime.date(2022,9,22), qty=10))
Python version you are using
python --version 3.10.10
Environment description
dataclasses-json==0.6.0
The text was updated successfully, but these errors were encountered: