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

Edge case of field(default=False, init=False) #648

Closed
carmocca opened this issue Dec 19, 2024 · 1 comment · Fixed by #650
Closed

Edge case of field(default=False, init=False) #648

carmocca opened this issue Dec 19, 2024 · 1 comment · Fixed by #650
Labels
bug Something isn't working

Comments

@carmocca
Copy link
Contributor

🐛 Bug report

Hello Mauricio!

Here's an edge case for you

  File "/home/ubuntu/carlos/stuff/kk.py", line 12, in <module>
    print(CLI(Bar))
  File "/home/ubuntu/.pyenv/versions/env/lib/python3.10/site-packages/jsonargparse/_cli.py", line 88, in CLI
    _add_component_to_parser(components, parser, as_positional, fail_untyped, config_help)
  File "/home/ubuntu/.pyenv/versions/env/lib/python3.10/site-packages/jsonargparse/_cli.py", line 169, in _add_component_to_parser
    added_args = parser.add_class_arguments(component, as_group=False, **kwargs)
  File "/home/ubuntu/.pyenv/versions/env/lib/python3.10/site-packages/jsonargparse/_signatures.py", line 107, in add_class_arguments
    added_args = self._add_signature_arguments(
  File "/home/ubuntu/.pyenv/versions/env/lib/python3.10/site-packages/jsonargparse/_signatures.py", line 308, in _add_signature_arguments
    self._add_signature_parameter(
  File "/home/ubuntu/.pyenv/versions/env/lib/python3.10/site-packages/jsonargparse/_signatures.py", line 434, in _add_signature_parameter
    action = container.add_argument(*args, **kwargs)
  File "/home/ubuntu/.pyenv/versions/env/lib/python3.10/site-packages/jsonargparse/_core.py", line 130, in add_argument
    self.add_class_arguments(kwargs.pop("type"), nested_key, **kwargs)
  File "/home/ubuntu/.pyenv/versions/env/lib/python3.10/site-packages/jsonargparse/_signatures.py", line 133, in add_class_arguments
    self.set_defaults(**defaults)  # type: ignore[attr-defined]
  File "/home/ubuntu/.pyenv/versions/env/lib/python3.10/site-packages/jsonargparse/_core.py", line 207, in set_defaults
    self.set_defaults(kwargs)
  File "/home/ubuntu/.pyenv/versions/env/lib/python3.10/site-packages/jsonargparse/_core.py", line 196, in set_defaults
    raise NSKeyError(f'No action for key "{dest}" to set its default.')
jsonargparse._namespace.NSKeyError: No action for key "metrics.x" to set its default.

To reproduce

from dataclasses import dataclass, field
from jsonargparse import CLI

@dataclass
class Foo:
    x: bool = field(default=False, init=False)

@dataclass
class Bar:
    metrics: Foo = field(default_factory=Foo)

print(CLI(Bar))

Motivation

I have a dataclass with dataclasses
I want to add fields that are not part of the init
I cannot use __post_init__ instead because it will be frozen (not included in the snippet above)

Expected behavior

Works as expected

Environment

  • jsonargparse version: 4.35.0
  • Python version: 3.10.12
  • How jsonargparse was installed: pip
  • OS: Linux

As always, thank you for this great tool 🙇

@carmocca carmocca added the bug Something isn't working label Dec 19, 2024
@mauvilsa
Copy link
Member

Thank you for reporting!

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