Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #598 from mit-ll-responsible-ai/kwargs-fn
Adds `hydra_zen.kwargs_fn`
- Loading branch information
Showing
10 changed files
with
230 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
hydra\_zen.kwargs_of | ||
==================== | ||
|
||
.. currentmodule:: hydra_zen | ||
|
||
.. autofunction:: kwargs_of |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Copyright (c) 2023 Massachusetts Institute of Technology | ||
# SPDX-License-Identifier: MIT | ||
from inspect import signature | ||
|
||
import pytest | ||
|
||
from hydra_zen import BuildsFn, instantiate, kwargs_of | ||
|
||
|
||
def test_basic(): | ||
Conf = kwargs_of(lambda x, y: None) | ||
assert set(signature(Conf).parameters) == {"x", "y"} | ||
out = instantiate(Conf(x=-9, y=10)) | ||
assert isinstance(out, dict) | ||
assert out == dict(x=-9, y=10) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"exclude,params", | ||
[ | ||
([0], {"y"}), | ||
(["x", -1], set()), | ||
], | ||
) | ||
def test_exclude(exclude, params): | ||
Conf = kwargs_of((lambda x, y: None), zen_exclude=exclude) | ||
assert set(signature(Conf).parameters) == params | ||
|
||
|
||
def test_dataclass_options(): | ||
Conf = kwargs_of((lambda x, y: None), zen_dataclass={"cls_name": "foo"}) | ||
assert Conf.__name__ == "foo" | ||
|
||
|
||
def test_dataclass_options_via_cls_defaults(): | ||
class Moo(BuildsFn): | ||
_default_dataclass_options_for_kwargs_of = {"cls_name": "bar"} | ||
|
||
Conf1 = kwargs_of((lambda: None), zen_dataclass={"cls_name": "foo"}) | ||
assert Conf1.__name__ == "foo" | ||
|
||
Conf2 = Moo.kwargs_of((lambda: None)) | ||
assert Conf2.__name__ == "bar" | ||
|
||
|
||
def test_kwarg_override(): | ||
Config = kwargs_of(lambda *, x, y: None, y=22) | ||
assert instantiate(Config(x=1)) == {"x": 1, "y": 22} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters