question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generic dataclass detection fails for unions

See original GitHub issue

Hi 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:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
brentyicommented, Sep 1, 2022

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) and yaml.dump(your_dataclass_instance) helpers. It should be basically as flexible as pickle, bigger file sizes but still human-readable + editable.

1reaction
pculbertsoncommented, Sep 1, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found