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

Generically compute dynamic defaults for Fields #16206

Merged
merged 6 commits into from Jul 18, 2022

Conversation

stuhood
Copy link
Sponsor Member

@stuhood stuhood commented Jul 18, 2022

As discussed on #16175, we don't currently consume the "dynamic" defaults of field values for the purposes of parametrize. That is at least partially because there is no generic way to do so: a Field has no way to declare a dynamic default currently, because Fields cannot declare a dependency @rule_helper to compute their value (...yet? see #12934 (comment)).

This change adds a mechanism for generically declaring the default value of a Field. This is definitely not the most ergonomic API: over the next few versions, many dynamic Field defaults will hopefully move to __defaults__. And #12934 (comment) will hopefully allow for significantly cleaning up those that remain.

Fixes #16175.

Copy link
Contributor

@Eric-Arellano Eric-Arellano 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 overall. I would expect this to need to be consumed by parametrize.py?

@stuhood
Copy link
Sponsor Member Author

stuhood commented Jul 18, 2022

Looks good overall. I would expect this to need to be consumed by parametrize.py?

Yea, will rebase atop #16197 for that.

@stuhood stuhood force-pushed the stuhood/dynamic-field-defaults branch 2 times, most recently from 497143a to 91a21d8 Compare July 18, 2022 22:27
@stuhood stuhood marked this pull request as ready for review July 18, 2022 22:34
Copy link
Contributor

@Eric-Arellano Eric-Arellano left a comment

Choose a reason for hiding this comment

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

Thanks!

  1. Do you think we will want to hook this up to ./pants help, in a follow up?
  2. We have other candidate fields, like those from [pex-binary-defaults]. Should those be hooked up in a follow up?
  3. Should call sites of the resolve fields be refactored to use FieldDefaults instead, for DRY purposes?

src/python/pants/engine/internals/parametrize.py Outdated Show resolved Hide resolved


@dataclass(frozen=True)
class FieldDefaults:
Copy link
Contributor

Choose a reason for hiding this comment

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

What about calling it DynamicFieldDefaults?

Copy link
Sponsor Member Author

@stuhood stuhood Jul 18, 2022

Choose a reason for hiding this comment

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

I think that all imperative code is implicitly "dynamic"... I wanted to mention dynamic in the docstring to differentiate from static Field defaults, but we wouldn't want to rename that field to static_default, so not using it in the name here seems ok.

Comment on lines +326 to +327
def value_or_default(self, field: Field) -> Any:
return (self.factory(type(field)))(field)
Copy link
Contributor

Choose a reason for hiding this comment

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

Worth a unit test?

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

I think that with both unit tests of parametrize_test.py and integration tests in graph_test.py, we're probably good.

…for Fields dynamically.

[ci skip-rust]
[ci skip-build-wheels]
# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
[ci skip-rust]
[ci skip-build-wheels]
@stuhood stuhood force-pushed the stuhood/dynamic-field-defaults branch from 91a21d8 to de167ce Compare July 18, 2022 22:48
# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
@stuhood
Copy link
Sponsor Member Author

stuhood commented Jul 18, 2022

  1. We have other candidate fields, like those from [pex-binary-defaults]. Should those be hooked up in a follow up?
  2. Should call sites of the resolve fields be refactored to use FieldDefaults instead, for DRY purposes?

Possibly. But as mentioned in the description, my hope is that most of the usecases of this facility are replaced with __defaults__, and that any that aren't are replaced with the facility from #12934 (comment).

  1. Do you think we will want to hook this up to ./pants help, in a follow up?

Yea, probably. Whether that should happen before/after a #12934 (comment) Field.compute_default_value-as-@rule_helper facility is the primary question.

@stuhood stuhood enabled auto-merge (squash) July 18, 2022 23:25
@stuhood stuhood disabled auto-merge July 18, 2022 23:28
@stuhood stuhood enabled auto-merge (squash) July 18, 2022 23:29
@stuhood stuhood merged commit 9df18f3 into pantsbuild:main Jul 18, 2022
@stuhood stuhood deleted the stuhood/dynamic-field-defaults branch July 19, 2022 01:26
@stuhood stuhood added this to the 2.12.x milestone Jul 19, 2022
stuhood added a commit to stuhood/pants that referenced this pull request Jul 19, 2022
As discussed on pantsbuild#16175, we don't currently consume the "dynamic" defaults of field values for the purposes of `parametrize`. That is at least partially because there is no generic way to do so: a `Field` has no way to declare a dynamic default currently, because `Field`s cannot declare a dependency `@rule_helper` to compute their value (...yet? see pantsbuild#12934 (comment)).

This change adds a mechanism for generically declaring the default value of a `Field`. This is definitely not the most ergonomic API: over the next few versions, many dynamic `Field` defaults will hopefully move to `__defaults__`. And pantsbuild#12934 (comment) will hopefully allow for significantly cleaning up those that remain.

Fixes pantsbuild#16175.

[ci skip-rust]
[ci skip-build-wheels]
stuhood added a commit to stuhood/pants that referenced this pull request Jul 19, 2022
As discussed on pantsbuild#16175, we don't currently consume the "dynamic" defaults of field values for the purposes of `parametrize`. That is at least partially because there is no generic way to do so: a `Field` has no way to declare a dynamic default currently, because `Field`s cannot declare a dependency `@rule_helper` to compute their value (...yet? see pantsbuild#12934 (comment)).

This change adds a mechanism for generically declaring the default value of a `Field`. This is definitely not the most ergonomic API: over the next few versions, many dynamic `Field` defaults will hopefully move to `__defaults__`. And pantsbuild#12934 (comment) will hopefully allow for significantly cleaning up those that remain.

Fixes pantsbuild#16175.

[ci skip-rust]
[ci skip-build-wheels]
stuhood added a commit that referenced this pull request Jul 19, 2022
) (#16220)

As discussed on #16175, we don't currently consume the "dynamic" defaults of field values for the purposes of `parametrize`. That is at least partially because there is no generic way to do so: a `Field` has no way to declare a dynamic default currently, because `Field`s cannot declare a dependency `@rule_helper` to compute their value (...yet? see #12934 (comment)).

This change adds a mechanism for generically declaring the default value of a `Field`. This is definitely not the most ergonomic API: over the next few versions, many dynamic `Field` defaults will hopefully move to `__defaults__`. And #12934 (comment) will hopefully allow for significantly cleaning up those that remain. 

Fixes #16175.

[ci skip-rust]
[ci skip-build-wheels]
stuhood added a commit that referenced this pull request Jul 19, 2022
) (#16219)

As discussed on #16175, we don't currently consume the "dynamic" defaults of field values for the purposes of `parametrize`. That is at least partially because there is no generic way to do so: a `Field` has no way to declare a dynamic default currently, because `Field`s cannot declare a dependency `@rule_helper` to compute their value (...yet? see #12934 (comment)).

This change adds a mechanism for generically declaring the default value of a `Field`. This is definitely not the most ergonomic API: over the next few versions, many dynamic `Field` defaults will hopefully move to `__defaults__`. And #12934 (comment) will hopefully allow for significantly cleaning up those that remain. 

Fixes #16175.

[ci skip-rust]
[ci skip-build-wheels]
jyggen pushed a commit to jyggen/pants that referenced this pull request Jul 27, 2022
As discussed on pantsbuild#16175, we don't currently consume the "dynamic" defaults of field values for the purposes of `parametrize`. That is at least partially because there is no generic way to do so: a `Field` has no way to declare a dynamic default currently, because `Field`s cannot declare a dependency `@rule_helper` to compute their value (...yet? see pantsbuild#12934 (comment)).

This change adds a mechanism for generically declaring the default value of a `Field`. This is definitely not the most ergonomic API: over the next few versions, many dynamic `Field` defaults will hopefully move to `__defaults__`. And pantsbuild#12934 (comment) will hopefully allow for significantly cleaning up those that remain. 

Fixes pantsbuild#16175.

[ci skip-rust]
[ci skip-build-wheels]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Default values of fields and un-parametrized consumers not accounted for during parametrization defaulting
2 participants