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

Fix Structured Config assignment to Dict annotation raising wrong error. #416

Merged
merged 5 commits into from
Oct 29, 2020

Conversation

pereman2
Copy link
Contributor

Close #410 .
With this fix check_assign_value_node is no longer needed.
Added a test in test_error to check the error message and, added test cases for test_assign_wrong_type_to_list and test_assign_wrong_type_to_dict for completion.

Copy link
Contributor Author

@pereman2 pereman2 left a comment

Choose a reason for hiding this comment

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

I've noticed that it keeps modifying the dict1 value in the test cases {"foo": True} and {"user": User(age=1, name="foo")} (test_assign_wrong_type_to_dict) because when setting dict1 value to one of these it first creates a empty dict and then starts assigning its values if it passes validate_set.

class DictSubclass:
    dict1: Dict[str, int] = ...
cfg.OmegaConf.structured(DictSubclass)
cfg.dict1 = {"foo": True} 
# 1. {"foo": True} valid assignment so it creates {}
# 2. tries to assign True to {} but it's not a valid value_type so, it raises error
# 3. {} stays as new dict1 value

So if validate_set encounters an invalid assignment it should be able to recover somehow with a cache for example.

@@ -597,9 +584,9 @@ def _set_value(self, value: Any, flags: Optional[Dict[str, bool]] = None) -> Non
elif isinstance(value, dict):
for k, v in value.items():
self.__setitem__(k, v)
else:
else: # pragma: no cover
Copy link
Owner

Choose a reason for hiding this comment

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

Why is this not covered?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No reason, I just moved this comment from the sentence below there because it made more sense.

Copy link
Owner

Choose a reason for hiding this comment

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

this comment says that test coverage is not considered this line as needed to be covered.
it's not a comment, it's a directive to the code coverage.
try to remove it and see if we still get 100% coverage.
if not, try to add a test that will make it get to 100%.
if it's impossible, please explain why.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's impossible, if it is a structured config the following code will check it.

if (
     is_assign
     and is_container_annotation(target_type)
     and not is_container_annotation(value_type)
 ):

If it isn't a structured config it will wrap a new node anyways.

Copy link
Owner

Choose a reason for hiding this comment

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

If this code never runs, why do we need it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't. Well to have a default case could be useful sometime but now, no.

Copy link
Owner

Choose a reason for hiding this comment

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

ok. I guess we can keep it.
If there is no reason, keep the pragma unchanged.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay

Copy link
Owner

@omry omry left a comment

Choose a reason for hiding this comment

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

Looking good, I see that you also removed the no cover, meaning this case is now covered by tests.

@omry omry merged commit 4326857 into omry:master Oct 29, 2020
@pereman2 pereman2 deleted the 410 branch October 29, 2020 16:47
pereman2 added a commit to pereman2/omegaconf that referenced this pull request Oct 30, 2020
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.

Assignment of Structured Config to Subscripted Dict raising wrong error.
2 participants