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

fix: 416 python hanging comma #566

Merged
merged 5 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading