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

Improve type-widening and add tests #185

Merged
merged 2 commits into from Dec 24, 2021
Merged

Improve type-widening and add tests #185

merged 2 commits into from Dec 24, 2021

Conversation

rsokl
Copy link
Contributor

@rsokl rsokl commented Dec 23, 2021

builds performs type-widening when users specify targeted configs as default values. This prevents common friction with Hydra's validation system; see #84

This PR fixes a case where the type-widening behavior was too aggressive: A targeted config type should not be broadened to Any since this is a valid type that can be leveraged by Hydra's runtime validation. Thus this PR fixes this, so that dataclass-types are retained and thus can be leveraged by Hydra for validation.

Before:

from hydra_zen import builds, instantiate

BuildsInt = builds(int)

def f_with_dataclass_annotation(x: BuildsInt = BuildsInt()):
    return x
>>> bad_value = builds(str)()  # not an instance of `BuildsInt`
>>> instantiate(builds(f_with_dataclass_annotation, x=bad_value))  # should raise validation error
''

After

>>> bad_value = builds(str)()
>>> instantiate(builds(f_with_dataclass_annotation, x=bad_value))
---------------------------------------------------------------------------
ValidationError   
ValidationError: Invalid type assigned: Builds_str is not a subclass of Builds_int. value: Builds_str(_target_='builtins.str')
    full_key: x
    object_type=None

>>> ok_value = builds(str, builds_bases=(BuildsInt,))()  # inherits from `BuildsInt`: type-checking passes
>>> instantiate(builds(f_with_dataclass_annotation, x=ok_value))
''

This PR also ensures that type-widening is performed based on a configuration value after it has been processed by our sanitization functions. Currently, the only example that I could think of to actually exercise this distinction involves the functionality in #172 . The test I included will thus only exercise this aspect of this PR for sufficiently-old versions of omegaconf.

@rsokl rsokl added the enhancement New feature or request label Dec 23, 2021
@rsokl rsokl added this to the hydra-zen 0.5.0 milestone Dec 23, 2021
@rsokl rsokl merged commit 67d0b86 into main Dec 24, 2021
@rsokl rsokl deleted the improve-type-widening branch January 9, 2022 16:37
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