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

Attrs default factory is not used by default #299

Closed
bilelomrani1 opened this issue Jun 9, 2023 · 3 comments · Fixed by #303
Closed

Attrs default factory is not used by default #299

bilelomrani1 opened this issue Jun 9, 2023 · 3 comments · Fixed by #303
Labels
bug Something isn't working

Comments

@bilelomrani1
Copy link

🐛 Bug report

When using a factory to build a default value in a attrs dataclass, jsonargparse ignores the factory at configuration time and considers the field as empty.

To reproduce

from typing import List

from attrs import define, field
from jsonargparse import ArgumentParser


@define
class Configuration:
    name: List[str] = field(factory=lambda: ["hello", "world"])


if __name__ == "__main__":
    parser = ArgumentParser()
    parser.add_argument("--data", type=Configuration)
    cfg = parser.parse_args()
    print(parser.instantiate_classes(cfg))
$ python main.py
usage: main.py [-h] [--data CONFIG] [--data.name NAME]
main.py: error: Configuration check failed :: Parser key "data.name":
  Expected a <class 'list'>. Got value: _Nothing.NOTHING

Expected behavior

The default factory should be called and the following should be printed to stdout:

$ python main.py
Namespace(data=Configuration(name=['hello', 'world']))

Environment

  • jsonargparse version: 4.21.2
  • Python version: 3.10.9
  • How jsonargparse was installed: pip install jsonargparse
  • OS: MacOS 13.2.1
@bilelomrani1 bilelomrani1 added the bug Something isn't working label Jun 9, 2023
@mauvilsa
Copy link
Member

Thank you very much for reporting! Good to know that there is interest for the attrs support. The field factory should be an easy fix. Will do it soon.

@mauvilsa
Copy link
Member

@bilelomrani1 I have implemented a fix #303. You can try it out by installing as:

pip install "jsonargparse @ https://github.com/omni-us/jsonargparse/zipball/issue-299-attrs-field-factory"

@bilelomrani1
Copy link
Author

Hi @mauvilsa, it works perfectly thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants