Skip to content
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

Nested Optional dataclass parameters require class_path/init_args #243

Closed
odedbd opened this issue Feb 16, 2023 · 3 comments · Fixed by #267
Closed

Nested Optional dataclass parameters require class_path/init_args #243

odedbd opened this issue Feb 16, 2023 · 3 comments · Fixed by #267
Labels
enhancement New feature or request

Comments

@odedbd
Copy link

odedbd commented Feb 16, 2023

          Another UPDATE:

Seems I was a little too quick to say it works as expected with pure dataclasses. I found one edge cases - when a dataclass parameter is nested inside another dataclass, and it is defined as an Optional field, it requires providing class_path/init_args.

A concrete example -

@dataclass
class Nested:
  f1: str = ''
  f2: int = 1

@dataclass
class Parent:
  fn: Optional[Nested] = None

In the above setup, for the config.yaml for Parent it would have to be

fn:
  class_path: Nested
  init_args:
    f2: 3

If I change Parent making Nested non-Optional, class_path/init_args are no longer needed. If I use the above configuration and do not add class_path/init_args, I get an error like this -

Does not validate against any of the Union subtypes
  Subtypes: (<class 'Nested'>, <class 'NoneType'>)
  Errors:
    - Not a valid Nested
    Subclass types expect one of:
    - a class path (str)
    - a dict with class_path entry
    - a dict without class_path but with init_args entry (class path given previously)
    - Expected a <class 'NoneType'>
  Given value type: <class 'dict'>

Originally posted by @odedbd in #236 (comment)

@mauvilsa mauvilsa added the enhancement New feature or request label Feb 16, 2023
@mauvilsa mauvilsa mentioned this issue Apr 15, 2023
6 tasks
@mauvilsa
Copy link
Member

Support for dataclasses nested in a type, including Optional, was implemented in #267.

@odedbd
Copy link
Author

odedbd commented Apr 19, 2023

@mauvilsa thanks! I'll check it out.

@odedbd
Copy link
Author

odedbd commented Jun 27, 2023

@mauvilsa I finally got around to testing this (project was in code-freeze for a while). Seems to working great now, thanks for implementing the clean support for nested optional datalcass parameters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants