Skip to content

Commit

Permalink
Fix issue that python auto-formatter fails on pip install cohere (#358)
Browse files Browse the repository at this point in the history
Co-authored-by: Max Shkutnyk <[email protected]>
  • Loading branch information
invader89 and Max Shkutnyk authored Jan 15, 2025
1 parent 4a8be84 commit 6e4c51f
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions .github/scripts/check_python_code_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,21 @@ def format_python_snippets_in_mdx(file_path, line_length=DEFAULT_LINE_LENGTH):

def format_with_black(match):
code = match.group(1)
formatted_code = black.format_str(code, mode=black_mode)
return f"```python\n{formatted_code.strip()}\n```"

# Comment out lines starting with '!'
processed_code = re.sub(r"^\s*!(.*)", r"# TEMP_COMMENT !\1", code, flags=re.MULTILINE)

try:
# Format the code with Black
formatted_code = black.format_str(processed_code, mode=black_mode)
except black.NothingChanged:
# If Black doesn't change anything, use original
formatted_code = processed_code

# Revert the commented lines starting with '!'
reverted_code = re.sub(r"^\s*# TEMP_COMMENT !(.*)", r"!\1", formatted_code, flags=re.MULTILINE)

return f"```python\n{reverted_code.strip()}\n```"

new_content = code_block_pattern.sub(format_with_black, original_content)

Expand Down

0 comments on commit 6e4c51f

Please sign in to comment.