-
-
Notifications
You must be signed in to change notification settings - Fork 608
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
add _partial_ to instantiate api #1905
Conversation
tests/instantiate/__init__.py
Outdated
def partial_equal(obj1: Any, obj2: Any) -> bool: | ||
obj1, obj2 = _convert_type(obj1), _convert_type(obj2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Many places in this file there is the pattern A == B or partial_equal(A, B)
.
This could be simplified by moving the A == B
part inside of the partial_equal
function as below:
def partial_equal(obj1: Any, obj2: Any) -> bool: | |
obj1, obj2 = _convert_type(obj1), _convert_type(obj2) | |
def partial_equal(obj1: Any, obj2: Any) -> bool: | |
if obj1 == obj2: | |
return True | |
obj1, obj2 = _convert_type(obj1), _convert_type(obj2) |
This would enable simplifying the call-sites of partial_equal
.
For example, the expression below on line 30
if not (obj1[i] == obj2[i] or partial_equal(obj1[i], obj2[i])):
return False
could then be replaced with this:
if not partial_equal(obj1[i], obj2[i]):
return False
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tested out the change locally to make sure it works. If you agree with the idea, I can push a commit to this PR branch!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!! feel free to push, thanks jasha!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in bf689d3
def _call_target(_target_: Callable, *args, **kwargs) -> Any: # type: ignore | ||
def _call_target(_target_: Callable, partial: Optional[bool], *args, **kwargs) -> Any: # type: ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we get away with bool
instead of Optional[bool]
?
(Same for the signature of the instantiate_node
function below)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the reason it is None
is a bit nuanced. with configs like _recursive_
,_convert_
, a child node will always inherit the parent node config if the config is not specifically set in the child node. But _partial_
is different, i think it only makes sense to set _partial_
per node, which means if _partial_
is not set in a node, it should default to False instead of inherit that from the parent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that _partial_
should be set per-node...
but why not make the default partial=False
instead of partial=None
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are right, Pr updated! :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing straggler parentheses from my previous commit:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few more nits:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR suffers from the same bug as #1669:
>>> instantiate({"_target_": "builtins.dict", "_partial_": True})
functools.partial(<class 'dict'>)
>>> instantiate({"_target_": "builtins.dict", "_partial_": True, "partial": "unrelated"})
Traceback (most recent call last):
...
TypeError: _call_target() got multiple values for argument 'partial'
Closes #1283
add
_partial_
support to the instantiation API