Skip to content

Commit

Permalink
fix: 416 python hanging comma (#566)
Browse files Browse the repository at this point in the history
Co-authored-by: Morgante Pell <morgantep@google.com>
  • Loading branch information
Alex-ley-scrub and morgante authored Oct 26, 2024
1 parent 67e36d7 commit e12de3b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
60 changes: 60 additions & 0 deletions crates/core/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12272,6 +12272,66 @@ fn trailing_comma_import_from_python_with_alias() {
.unwrap();
}

// refer to https://github.com/getgrit/gritql/issues/416
#[test]
fn trailing_comma_after_argument_removal() {
run_test_expected({
TestArgExpected {
pattern: r#"
language python
`TaskMetadata($args)` where {
$args <: contains `n_samples=$_` => .
}
"#.to_owned(),
source: r#"
TaskMetadata(
description="Parallel news titles from the Tbilisi City Hall website (https://tbilisi.gov.ge/).",
main_score="f1", domains=["News"],
text_creation="created",
n_samples={_EVAL_SPLIT: 1820},
reference="https://huggingface.co/datasets/jupyterjazz/tbilisi-city-hall-titles"
)
"#
.to_owned(),
expected: r#"
TaskMetadata(
description="Parallel news titles from the Tbilisi City Hall website (https://tbilisi.gov.ge/).",
main_score="f1", domains=["News"],
text_creation="created",
reference="https://huggingface.co/datasets/jupyterjazz/tbilisi-city-hall-titles"
)
"#
.to_owned(),
}
})
.unwrap();
}

/// Same as above test, but ensures the behavior doesn't depend on line breaks
#[test]
fn trailing_comma_after_argument_removal_one_line() {
run_test_expected({
TestArgExpected {
pattern: r#"
language python
`TaskMetadata($args)` where {
$args <: contains `n_samples=$_` => .
}
"#.to_owned(),
source: r#"
TaskMetadata(description="Parallel news titles from the Tbilisi City Hall website (https://tbilisi.gov.ge/).", main_score="f1", domains=["News"], text_creation="created", n_samples={_EVAL_SPLIT: 1820}, reference="https://huggingface.co/datasets/jupyterjazz/tbilisi-city-hall-titles")
"#
.to_owned(),
expected: r#"
TaskMetadata(description="Parallel news titles from the Tbilisi City Hall website (https://tbilisi.gov.ge/).", main_score="f1", domains=["News"], text_creation="created", reference="https://huggingface.co/datasets/jupyterjazz/tbilisi-city-hall-titles")
"#
.to_owned(),
}
})
.unwrap();
}

#[test]
fn python_orphaned_from_imports() {
run_test_expected({
Expand Down
13 changes: 12 additions & 1 deletion crates/language/src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,22 @@ impl Language for Python {

fn check_replacements(&self, n: NodeWithSource<'_>, replacements: &mut Vec<Replacement>) {
if n.node.is_error() {
if n.text().is_ok_and(|t| t == "->") {
// https://github.com/getgrit/gritql/issues/416 single line example
if n.text().is_ok_and(|t| t == "->") || n.text().is_ok_and(|t| t == ",") {
replacements.push(Replacement::new(n.range(), ""));
}
return;
}
if n.node.kind() == "," {
// https://github.com/getgrit/gritql/issues/416 multi line example
if let Some(prev) = n.node.prev_sibling() {
let empty = prev.range().end_byte() == prev.range().start_byte();
if empty {
replacements.push(Replacement::new(n.range(), ""));
return;
}
}
}
if n.node.kind() == "import_from_statement" {
if let Some(name_field) = n.node.child_by_field_name("name") {
let names_text = name_field
Expand Down

0 comments on commit e12de3b

Please sign in to comment.