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

added throw_on_missing flag to _get_node() #513

Merged
merged 4 commits into from
Feb 3, 2021
Merged

Conversation

omry
Copy link
Owner

@omry omry commented Feb 3, 2021

I also noticed that _get_node() can operate on slices as well, in which case it does not return a Node but a list.
I changed it's return type to Any to address this.

@omry
Copy link
Owner Author

omry commented Feb 3, 2021

@Jasha10, can you review?

@Jasha10
Copy link
Collaborator

Jasha10 commented Feb 3, 2021

Will take a look now.

@omry
Copy link
Owner Author

omry commented Feb 3, 2021

Will take a look now.

Thanks.
I just updated the typing.
By the way, can you join the Hydra chat?

@Jasha10
Copy link
Collaborator

Jasha10 commented Feb 3, 2021

Yes, I've joined the Zulip chat.

Copy link
Collaborator

@Jasha10 Jasha10 left a comment

Choose a reason for hiding this comment

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

Looks good!

Comment on lines 222 to 223
assert node is not None
assert isinstance(node, Node)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
assert node is not None
assert isinstance(node, Node)
assert isinstance(node, Node)

No need for the first assertion, since the second one implies node is not None.

Copy link
Owner Author

Choose a reason for hiding this comment

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

roger.

Comment on lines 244 to 245
assert node is not None
assert isinstance(node, Node)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
assert node is not None
assert isinstance(node, Node)
assert isinstance(node, Node)

Copy link
Owner Author

Choose a reason for hiding this comment

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

roger roger

Comment on lines 384 to 388
assert isinstance(self.__dict__["_content"], list)
if validate_access:
self._validate_get(key)
return self.__dict__["_content"][key] # type: ignore
assert self.__dict__["_content"] is not None
assert not isinstance(self.__dict__["_content"], str)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am confused by these three assertions:

  • isinstance(_content, list)
  • _content is not None
  • not isinstance(_content, str)

Could these maybe be replaced with a single assertion?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Good spot, the first one is sufficient.

Comment on lines 387 to 388
assert self.__dict__["_content"] is not None
assert not isinstance(self.__dict__["_content"], str)
Copy link
Collaborator

@Jasha10 Jasha10 Feb 3, 2021

Choose a reason for hiding this comment

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

Suggested change
assert self.__dict__["_content"] is not None
assert not isinstance(self.__dict__["_content"], str)

On my computer, the tests + mypy still pass if the later two assertions are deleted.

Comment on lines 906 to 908
assert val is None or isinstance(val, Node)
if val is not None:
assert isinstance(val, Node)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
assert val is None or isinstance(val, Node)
if val is not None:
assert isinstance(val, Node)
assert val is None or isinstance(val, Node)
if val is not None:

Technically the second assertion here is not necessary: mypy can infer that val has type Node based on the previous two lines.

Copy link
Collaborator

Choose a reason for hiding this comment

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

(If you type reveal_type(val) inside of the if block, mypy prints Revealed type is 'omegaconf.base.Node')

Copy link
Owner Author

@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.

Thanks, fixed.

Many of those are because the first iteration was returning Any and later I decided that returning a Union is a better idea.

Comment on lines 222 to 223
assert node is not None
assert isinstance(node, Node)
Copy link
Owner Author

Choose a reason for hiding this comment

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

roger.

Comment on lines 244 to 245
assert node is not None
assert isinstance(node, Node)
Copy link
Owner Author

Choose a reason for hiding this comment

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

roger roger

Comment on lines 384 to 388
assert isinstance(self.__dict__["_content"], list)
if validate_access:
self._validate_get(key)
return self.__dict__["_content"][key] # type: ignore
assert self.__dict__["_content"] is not None
assert not isinstance(self.__dict__["_content"], str)
Copy link
Owner Author

Choose a reason for hiding this comment

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

Good spot, the first one is sufficient.

],
)
def test_get_node_throw_on_missing(cfg: Any, key: Any) -> None:
with pytest.raises(MissingMandatoryValue):
Copy link
Collaborator

@Jasha10 Jasha10 Feb 3, 2021

Choose a reason for hiding this comment

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

Suggested change
with pytest.raises(MissingMandatoryValue):
with pytest.raises(
MissingMandatoryValue,
match="Missing (mandatory value|node) " + re.escape(f"'{key}'"),
):

If you want to test the error strings :P

Copy link
Owner Author

Choose a reason for hiding this comment

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

Meh, I just normalized the exception message :)
Why use a toopick when you can use a BFG7000?

@omry omry merged commit 96645ef into master Feb 3, 2021
@omry omry deleted the _get_node_throw_on_missing branch February 3, 2021 23:44
odelalleau pushed a commit to odelalleau/omegaconf that referenced this pull request Feb 5, 2021
* added throw_on_missing flag to _get_node()

* tighter typing

* removed some extraneous assertions

* standardized error message
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.

2 participants