nebius/SWE-bench-extra huggingface dataset:
Repository: macisamuele/language-formatters-pre-commit-hooks

Issue:
pretty-format-yaml removes empty documents
This issue has been opened as follow-up from https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/3#discussion_r420938179


Comments:

Patch:
diff --git a/language_formatters_pre_commit_hooks/pretty_format_yaml.py b/language_formatters_pre_commit_hooks/pretty_format_yaml.py
index 7897715..59f491c 100644
--- a/language_formatters_pre_commit_hooks/pretty_format_yaml.py
+++ b/language_formatters_pre_commit_hooks/pretty_format_yaml.py
@@ -32,10 +32,9 @@ def _process_single_document(document, yaml):
         pretty_output = StringIO()
         yaml.dump(content, pretty_output)
         return pretty_output.getvalue()
-    elif document:
+    else:
         # do not disturb primitive content (unstructured text)
         return str(document)
-    return None
 
 
 def pretty_format_yaml(argv=None):
@@ -79,6 +78,11 @@ def pretty_format_yaml(argv=None):
         separator_pattern = r'^---\s*\n'
         original_docs = re.split(separator_pattern, string_content, flags=re.MULTILINE)
 
+        # A valid multi-document YAML file might starts with the separator.
+        # In this case the first document of original docs will be empty and should not be consdered
+        if string_content.startswith('---'):
+            original_docs = original_docs[1:]
+
         pretty_docs = []
 
         try:


Test patch:
diff --git a/test-data/pretty_format_yaml/empty-doc-with-separator.yaml b/test-data/pretty_format_yaml/empty-doc-with-separator.yaml
new file mode 100644
index 0000000..ed97d53
--- /dev/null
+++ b/test-data/pretty_format_yaml/empty-doc-with-separator.yaml
@@ -0,0 +1,1 @@
+---
diff --git a/test-data/pretty_format_yaml/empty-doc.yaml b/test-data/pretty_format_yaml/empty-doc.yaml
new file mode 100644
index 0000000..e69de29
diff --git a/test-data/pretty_format_yaml/multi-doc-with-empty-document-inside.yaml b/test-data/pretty_format_yaml/multi-doc-with-empty-document-inside.yaml
new file mode 100644
index 0000000..89266d2
--- /dev/null
+++ b/test-data/pretty_format_yaml/multi-doc-with-empty-document-inside.yaml
@@ -0,0 +1,5 @@
+---
+a: 1
+---
+---
+b: 2
diff --git a/tests/pretty_format_yaml_test.py b/tests/pretty_format_yaml_test.py
index d0d17da..907c726 100644
--- a/tests/pretty_format_yaml_test.py
+++ b/tests/pretty_format_yaml_test.py
@@ -30,6 +30,9 @@ def change_dir():
         ('not-valid-file.yaml', 1),
         ('ansible-vault.yaml', 0),
         ('primitive.yaml', 0),
+        ('empty-doc-with-separator.yaml', 1),
+        ('empty-doc.yaml', 0),
+        ('multi-doc-with-empty-document-inside.yaml', 0),
     ),
 )
 def test_pretty_format_yaml(filename, expected_retval):