Generic dataclass detection fails for unions
See original GitHub issueHi Brent,
I have a very low-level bug to flag for you – when saving/loading nested dataclasses to yaml (using extras.to_yaml()
, extras.from_yaml()
, if a dataclass has a Union of two custom types, they don’t get detected as custom types for the yaml.Loader
to construct.
I wrote a MWE to replicate the issue:
import dataclasses
import dcargs
from typing import Union
@dataclasses.dataclass
class TypeA:
data: int
@dataclasses.dataclass
class TypeB:
data: int
@dataclasses.dataclass
class Wrapper:
subclass: Union[TypeA, TypeB] = TypeA(1)
if __name__ == "__main__":
wrapper1 = Wrapper() # Create Wrapper object.
wrapper2 = dcargs.extras.from_yaml(Wrapper, dcargs.extras.to_yaml(wrapper1)) # Errors, no constructor for TypeA
No worries if this is too low-level to deal with right now – I think we can work around it by just pickling the configs, but wanted to flag something is going awry in the custom type detection.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Generic Type[T] fails in unions for unions #4298 - GitHub
Generic Type[T] fails in unions for unions #4298 ... Construct a dataclass from command-line inputs. instance_of_dataclass = cli(DataclassType).
Read more >Inherit Union type in Python 3.10 - Stack Overflow
Say that we create a generic Union type called ResultData in Python 3.10 from __future__ import annotations from dataclasses import ...
Read more >Generics - mypy 0.991 documentation
This section explains how you can define your own generic classes that take one or more type parameters, similar to built-in types such...
Read more >How to create a Generic Type where the output is the inner ...
I want to use custom types to annotate functions. Example: @dataclass class CurrencyPosition: owner: str def read_balance(balance: ...
Read more >Denotable union and intersection types : KT-13108 - YouTrack
The problem is that your solution works for concrete types, but as soon as we introduce generic types, it falls apart.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Sounds good!
If you haven’t considered it, I’d recommend also looking into serializing with PyYAML + the standard
yaml.load(your_yaml, Loader=yaml.Loader)
andyaml.dump(your_dataclass_instance)
helpers. It should be basically as flexible as pickle, bigger file sizes but still human-readable + editable.Thanks, appreciate the heads-up! I wanted to file an issue in case the
Union
typing issue went further than just the YAML utils. Before your patch, we were just pickling the dataclass objects with no problem, we might just stick with that.