From 9ce734c3c297f09415b276b660d40dd53d76e76b Mon Sep 17 00:00:00 2001 From: Max Shkutnyk Date: Wed, 15 Jan 2025 15:19:00 +0200 Subject: [PATCH] Fix issue that python auto-formatter fails on pip install cohere --- .github/scripts/check_python_code_snippets.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/.github/scripts/check_python_code_snippets.py b/.github/scripts/check_python_code_snippets.py index ecd519bcd..18c21643e 100755 --- a/.github/scripts/check_python_code_snippets.py +++ b/.github/scripts/check_python_code_snippets.py @@ -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)