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

Error message improvement for OmegaConf.structured creation #557

Closed

Conversation

Jasha10
Copy link
Collaborator

@Jasha10 Jasha10 commented Feb 22, 2021

Closes #435

@Jasha10 Jasha10 changed the title Closes #435 Error message improvement for OmegaConf.structured creation Feb 22, 2021
Comment on lines +956 to +957
except ValidationError as e:
format_and_raise(node=parent, key=key, value=value, msg=str(e), cause=e)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main change of this PR is to wrap the body of OmegaConf._node_wrap in a try/catch block, so that more information can be given to the format_and_raise function. The goal is to close #435.

I wasn't sure if passing node=parent to format_and_raise above is the right decision. I tried passing node=None, which resulted in a lot more errors.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After further review recalled that, in the case of structured config creation, the parent is a dummy_parent created by the get_dataclass_data function.

Comment on lines +295 to +307
# structured typecheck
pytest.param(
Expected(
create=lambda: None,
op=lambda cfg: OmegaConf.structured(User(age="foo")), # type: ignore
exception_type=ValidationError,
msg="Value 'foo' could not be converted to Integer",
object_type=dict,
key="age",
low_level=True,
),
id="structured:creation-with-incorrect-key-type",
),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the test case motivated by #435.
The object_type=dict comes from passing node=parent to the format_and_raise function, I think.

Comment on lines -480 to +495
object_type_str=None,
ref_type_str=None,
object_type=dict,
key="foo",
low_level=True,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made this change to get the tests to pass. I will review to see if there is a better way to achieve the improved error message, without having to change the above test case.

Comment on lines -490 to +507
object_type=None,
msg="Non optional field cannot be assigned None",
object_type_str="NotOptionalInt",
ref_type_str=None,
object_type=dict,
key="foo",
low_level=True,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto.

@Jasha10 Jasha10 closed this Feb 23, 2021
@Jasha10 Jasha10 deleted the could-not-convert-obj_type-error branch February 23, 2021 00:58
@Jasha10 Jasha10 self-assigned this Feb 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unclear error: incorrect type for field of dataclass instance
1 participant