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

Add zen_exclude option to builds and make_custom_builds_fn #558

Merged
merged 11 commits into from Oct 25, 2023
Merged

Conversation

rsokl
Copy link
Contributor

@rsokl rsokl commented Oct 25, 2023

This PR adds the ability to exclude parameters - either by name or by pattern (i.e. callable[[name], bool]) - from builds when populate_full_signature=True.

from hydra_zen import builds, to_yaml

def pyaml(x): print(to_yaml(x))

def f(x=1, y=2, _z=3): ...
>>> c1 = builds(f, populate_full_signature=True, zen_exclude=['y'])  # exclude `y` from config
>>> pyaml(c1)
_target_: __main__.f
x: 1
_z: 3
>>> c2 = builds(f, populate_full_signature=True, zen_exclude=lambda x: x.startswith("_"))  # exclude any param starting with "_"
>>> pyaml(c2)
_target_: __main__.f
x: 1
'y': 2

or

from hydra_zen import make_custom_builds_fn

# a builds fn that always ignores parameters with "private" names from auto-populated parameters.
cbuilds = make_custom_builds_fn(populate_full_signature=True, zen_exclude=lambda x: x.startswith("_"))
>>> pyaml(cbuilds(foo))
_target_: __main__.foo
x: 1

Note that this does not enable one to exclude inherited parameters.

Implementation details

I have wanted to implement zen_exclude for a while, but doing so would make our typing-overloads for populate_full_signature=True double, which I am absolutely not going to do.

Because specifying zen_exclude has the same effect, as far as typing is concerned, as a user specifying a target-kwarg in builds, I realized that I can just "smuggle" zen_exclude into **target_kwargs. The typing overloads work just fine.

The downside to this is that zen_exclude does not explicitly appear in the signature of builds. Thus you will not see it autocomplete nor will you get type-checking on this parameter. This is unfortunate but ultimately it is better to have zen_exclude with some lack in tooling support rather than not at all.

@rsokl rsokl added this to the v0.12.0 milestone Oct 25, 2023
@rsokl rsokl changed the title Add zen_exclude option to builds Add zen_exclude option to builds and make_custom_builds_fn Oct 25, 2023
@rsokl rsokl marked this pull request as ready for review October 25, 2023 15:24
@rsokl rsokl added the enhancement New feature or request label Oct 25, 2023
@rsokl rsokl merged commit 8b70e51 into main Oct 25, 2023
15 checks passed
@rsokl rsokl deleted the zen-exclude-2 branch October 25, 2023 15:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant