-
-
Notifications
You must be signed in to change notification settings - Fork 52
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
Support dataclass inheritance #287
Comments
@gautiervarjo thank you for the proposal! I do have in my list of things to improve, a way for the developer to specify which classes to consider or not as subclasses or as dataclass-like. Though, I haven't decided yet how this will be. A couple of minor notes until this kind of support is implemented.
|
Thank you for the tips! These seem very low-effort to implement so they're definitely good enough for my use-case 🙂 |
This feature request also applies to pydantic classes as mentioned in #503. |
@mauvilsa Are there going to be any updates in the near future? I'm particularly interested in supporting |
@rusmux thanks for letting me know about your interest. I do consider interest to decide on which new features to work on. Though I can't really say if soon I would work on this one since it is not simple. |
A similar use-case, which used to work as of version 4.20.0, was hierarchical Optional dataclasses: With the following setup: from dataclasses import dataclass
@dataclass
class B:
param_1: int = 10
param_2: str = "b"
@dataclass
class Bb(B):
pass and import jsonargparse
from hierarchy_test import B
from typing import Optional
def test(
b: Optional[B],
):
print("success")
if __name__ == "__main__":
jsonargparse.CLI(test) with the following YAML: b:
class_path: hierarchy_test.Bb
init_args:
param_1: 1
param_2: "dd" This runs with no error on 4.20.0, and fails on 4.33.2 This issue has been preventing me from upgrading to a newer Pytorch Lightning. I've got a quasi-solution by explicitly defining a Union of types as suggested by @mauvilsa . In my production env this involves dynamically creating a Union type based on all subclasses found through a search of a section of the codebase, not the most elegant thing. |
An update. I have been thinking about this for some time and I want to resolve it hopefully soon. My current idea is to remove the distinction between subclasses and dataclass-like. But do so without any breaking changes, otherwise this would need a major release which I don't see happening soon. No breaking changes would mean that accessing values in a parsed Clearly this behavior is not ideal, but would be the default in order to avoid breaking changes. And there would be a setting to change this default behavior when serializing. Not sure what it should be. Probably no difference between dataclasses and subclasses. Could be that if the It is not easy to describe exactly what I have in mind and all the implications it could have. Nonetheless, if you observe any potential issues with this initial idea, please speak out. |
I have been working on this and it seems it will not be possible without breaking changes. |
🚀 Feature request
The documentation describes how dataclass-like classes are "not expected to have subclasses", and therefore do not accept specifying a
class_path
. Howeverdataclasses.dataclass
does support inheritance: python docsWould it be feasible to add support for this? I can't say whether this makes sense for all dataclass-like objects, or just the plain python ones.
My use-case for dataclass+inheritance is pretty basic, my main reason for using dataclasses is reducing boilerplate of writing
__init__
/__str__
/etc functions.To reproduce
A working example with plain classes
A failing example with dataclasses
A failing example with CLI + config
Expected behavior
The failing dataclass snippet should behave exactly like the working plain-class one.
Environment
jsonargparse[signatures]==4.21.1
.The text was updated successfully, but these errors were encountered: