gemini-code-assist[bot] commented on code in PR #38547:
URL: https://github.com/apache/beam/pull/38547#discussion_r3289508021


##########
sdks/python/apache_beam/yaml/yaml_transform.py:
##########
@@ -1391,19 +1391,63 @@ def validate_transform_references(spec):
   return spec
 
 
+def strip_leading_comments(source: str) -> str:
+  lines = source.splitlines(keepends=True)
+  stripped_lines = []
+  in_leading_comments = True
+  for line in lines:
+    stripped_line = line.lstrip()
+    if in_leading_comments:
+      if stripped_line.startswith('#') or not stripped_line:
+        continue
+      else:
+        in_leading_comments = False
+    stripped_lines.append(line)
+  return "".join(stripped_lines)

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   The `strip_leading_comments` function removes leading comment lines and 
empty lines from the source. While this is necessary to allow Jinja directives 
like `{% extends %}` to appear at the start of a file (even with license 
headers), the current implementation shifts line numbers by completely removing 
these lines. This will cause error messages from Jinja or the YAML loader to 
point to incorrect lines in the source file, making debugging difficult.
   
   To fix this, you should replace the stripped content with the original line 
endings, effectively turning them into blank lines and preserving the line 
count.
   
   ```suggestion
   def strip_leading_comments(source: str) -> str:
     lines = source.splitlines(keepends=True)
     stripped_lines = []
     in_leading_comments = True
     for line in lines:
       if in_leading_comments:
         stripped_line = line.lstrip()
         if stripped_line.startswith('#') or not stripped_line:
           stripped_lines.append(line[len(line.rstrip('\r\n')):])
           continue
         in_leading_comments = False
       stripped_lines.append(line)
     return "".join(stripped_lines)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to