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

Implement dict and list-of-scalar parsing for options sources. #20428

Merged
merged 2 commits into from
Jan 18, 2024

Conversation

benjyw
Copy link
Contributor

@benjyw benjyw commented Jan 17, 2024

No description provided.

@benjyw benjyw added the category:internal CI, fixes for not-yet-released features, etc. label Jan 17, 2024
@benjyw benjyw requested a review from huonw January 17, 2024 06:26
@@ -77,6 +79,31 @@ impl Args {
}
Ok(None)
}

fn get_list<T>(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A generic variant of the old get_string_list, which is now reimplemented using this (as are the other scalar list types). The parse_list argument is the specific parser function we should apply to the raw string to yield the needed list type.

@@ -77,7 +77,7 @@ fn test_bool() {
#[test]
fn test_float() {
let args = args([
"-j=4",
"-j=4.0",
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 never documented support for integer literals as floats, and we expect (and now only support) Python float literals.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, maybe we should support this because people may be relying on it in practice?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I'd be very unsurprised if people are doing things like setting options like pantsd_timeout_when_multiple_invocations to 120. It'd be annoying to be required to specify the .0. Is it hard to support integer literals too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's tricksy to do it in the parser, because we don't want to "swallow" actual ints as floats in a dict. But we can post-process safely I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, we can coerce an int in get_float safely. Done.

assert_float(3.14, option_id!("foo"));
assert_float(1.137, option_id!("baz", "spam"));

assert!(args.get_float(&option_id!("dne")).unwrap().is_none());

assert_eq!(
"Problem parsing --bad value swallow as a float value: invalid float literal".to_owned(),
"Problem parsing --bad float value:\n1:swallow\n ^\n\
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 switch to using the parse.rs methods for everything, including scalars, for uniformity (and to support non-rust literals that python supports, such as underscores in ints and floats)

@@ -305,6 +335,57 @@ impl Config {
.and_then(|table| table.get(Self::option_name(id)))
}

fn get_list<T: FromValue>(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Again, a genericised get_string_list

self.get_list(id, parse_string_list)
}

fn get_dict(&self, id: &OptionId) -> Result<Option<DictEdit>, String> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The diff here is confusing - this is a brand new function

@@ -66,6 +69,20 @@ impl Env {
}
names
}

fn get_list<T>(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Again, a generic version of get_string_list

Copy link
Contributor

@huonw huonw 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 other than some minor questions, but I'm short on time (going on leave tomorrow), so it was only a cursory review.

@@ -77,7 +77,7 @@ fn test_bool() {
#[test]
fn test_float() {
let args = args([
"-j=4",
"-j=4.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I'd be very unsurprised if people are doing things like setting options like pantsd_timeout_when_multiple_invocations to 120. It'd be annoying to be required to specify the .0. Is it hard to support integer literals too?

remove,
)?,
})
if sub_table.len() == 1 && add.is_table() {
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if these aren't true? It looks like the value is silently dropped?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What happens if these aren't true? It looks like the value is silently dropped?

Ah yes, good catch. We should probably never have supported a subtable named "add" the way we do for lists, because unlike in the list case this is also a valid dict value. But we do in Python so we must here as well.

Anyway, fixed.

@benjyw benjyw merged commit e7b4b25 into pantsbuild:main Jan 18, 2024
@benjyw benjyw deleted the parse_list_options branch January 18, 2024 04:51
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