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

Support None option values. #20736

Merged
merged 1 commit into from
Apr 2, 2024

Conversation

benjyw
Copy link
Contributor

@benjyw benjyw commented Apr 1, 2024

The python options parser allows default=None,
so we must support it in the rust parser.

A None value can currently only be obtained
via a default of None: if a default is not None
then the option will have that value if it doesn't
take a non-None value from some other source.

We don't currently support setting a value to None in
args/env/config in Python, so we don't support
it here either. However the data structures could
support this if we want to in the future.

The python options parser allows default=None,
so we must support it in the rust parser.

A None value can currently only be obtained
via a default of None: if a default is not None
then the option will have that value if it doesn't
take a non-None value from some other source.

We don't currently support setting a value to None
in args/env/config in Python, so we don't support
it here either. However the data structures could
support this if we want to in the future.
@benjyw benjyw added the category:internal CI, fixes for not-yet-released features, etc. label Apr 1, 2024
@benjyw benjyw requested review from huonw and tgolsson April 1, 2024 07:43
pub struct OptionalOptionValue<T> {
pub derivation: Option<Vec<(Source, T)>>,
pub source: Source,
pub value: Option<T>,
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like this is almost identical to OptionValue<Option<T>> (except derivation becomes Option<Vec<(Source, Option<T>)>); what do you think about using that instead?

I think we can still add this convenience method, as I believe this should work:

impl<T> OptionValue<Option<T>> {
    fn unwrap(self) -> OptionValue<T> { ... }
}

Alternatively, add combinator methods like fn map<U>(self, f: FnOnce(T) -> U) -> OptionValue<U> to OptionValue<T>, then the places that need unwrap can write some_option_value.map(|v| v.unwrap()) or some_option_value.map(Option::unwrap).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could. That type for a derivation would be substantial overkill, since only defaults can be None. But I'm not sure having both types is a big problem? They are lightweight containers with no associated logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review. Will merge as-is for now, and consider simplifying this once the Python bindings are done and we can see how it all fits together.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, not a problem at all, just exploring the solution space. Happy as is!

@benjyw benjyw merged commit 5374772 into pantsbuild:main Apr 2, 2024
24 checks passed
@benjyw benjyw deleted the allow_none_option_defaults branch April 2, 2024 01:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category:internal CI, fixes for not-yet-released features, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants