From a154d40481fd84a2cc7e9e325cb9cc4964658db8 Mon Sep 17 00:00:00 2001 From: Runs Date: Sat, 5 Sep 2026 00:26:20 -0700 Subject: [PATCH 1/2] fix: keep generated workflow arguments valid --- codemod/populate_custom_docs.py | 22 ++++++++++++-- .../resources/v1/face_detection/README.md | 4 +-- tests/test_populate_custom_docs.py | 30 +++++++++++++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 tests/test_populate_custom_docs.py diff --git a/codemod/populate_custom_docs.py b/codemod/populate_custom_docs.py index 1a92a4e..7806c06 100644 --- a/codemod/populate_custom_docs.py +++ b/codemod/populate_custom_docs.py @@ -104,12 +104,16 @@ def _customize_docs_for_resource(self, content: str, resource_name: str) -> str: sync_generate_call = create_sync_params.replace( f"client.v1.{resource_name}.create(", f"client.v1.{resource_name}.generate(" ) - sync_generate_call = sync_generate_call + f"\n {generate_params}\n)" + sync_generate_call = self._append_generate_parameters( + sync_generate_call, generate_params + ) async_generate_call = create_async_params.replace( f"client.v1.{resource_name}.create(", f"client.v1.{resource_name}.generate(" ) - async_generate_call = async_generate_call + f"\n {generate_params}\n)" + async_generate_call = self._append_generate_parameters( + async_generate_call, generate_params + ) # Replace api-assets/id and image/id paths with /path/to paths in the code samples # Preserve the file ID and extension: api-assets/id/1234.mp4 -> /path/to/1234.mp4 @@ -172,6 +176,20 @@ def replacement(m: re.Match[str]) -> str: # Return the customized content - replacement will be handled by the caller return custom_docs_content + @staticmethod + def _append_generate_parameters(create_call: str, generate_params: str) -> str: + """Append generate-only parameters after a valid comma-separated create call.""" + lines = create_call.split("\n") + for index in range(len(lines) - 1, -1, -1): + if not lines[index].strip(): + continue + if not lines[index].rstrip().endswith(","): + lines[index] = f"{lines[index].rstrip()}," + break + + create_call_with_comma = "\n".join(lines) + return f"{create_call_with_comma}\n {generate_params}\n)" + def _fallback_customization(self, content: str, resource_name: str) -> str: """Fallback customization when create samples aren't found.""" # This is the original approach as a fallback diff --git a/magic_hour/resources/v1/face_detection/README.md b/magic_hour/resources/v1/face_detection/README.md index 35c00e6..abdfd66 100644 --- a/magic_hour/resources/v1/face_detection/README.md +++ b/magic_hour/resources/v1/face_detection/README.md @@ -32,7 +32,7 @@ from os import getenv client = Client(token=getenv("API_TOKEN")) res = client.v1.face_detection.generate( - assets={"target_file_path": "/path/to/1234.png"}, confidence_score=0.5 + assets={"target_file_path": "/path/to/1234.png"}, confidence_score=0.5, wait_for_completion=True, download_outputs=True, download_directory="." @@ -47,7 +47,7 @@ from os import getenv client = AsyncClient(token=getenv("API_TOKEN")) res = await client.v1.face_detection.generate( - assets={"target_file_path": "/path/to/1234.png"}, confidence_score=0.5 + assets={"target_file_path": "/path/to/1234.png"}, confidence_score=0.5, wait_for_completion=True, download_outputs=True, download_directory="." diff --git a/tests/test_populate_custom_docs.py b/tests/test_populate_custom_docs.py new file mode 100644 index 0000000..9f1a70b --- /dev/null +++ b/tests/test_populate_custom_docs.py @@ -0,0 +1,30 @@ +from codemod.populate_custom_docs import CustomDocsPopulator + + +def test_append_generate_parameters_adds_missing_comma() -> None: + create_call = """client.v1.face_detection.generate( + assets={"target_file_path": "/path/to/1234.png"}, confidence_score=0.5""" + generate_params = ( + "wait_for_completion=True,\n" + " download_outputs=True,\n" + ' download_directory="."' + ) + + result = CustomDocsPopulator._append_generate_parameters( + create_call, generate_params + ) + + assert "confidence_score=0.5,\n wait_for_completion=True" in result + assert result.endswith('\n download_directory="."\n)') + + +def test_append_generate_parameters_preserves_existing_comma() -> None: + create_call = """client.v1.ai_image_generator.generate( + style={"prompt": "A sunset"},""" + + result = CustomDocsPopulator._append_generate_parameters( + create_call, "wait_for_completion=True," + ) + + assert "style={\"prompt\": \"A sunset\"},,\n" not in result + assert "style={\"prompt\": \"A sunset\"},\n" in result From 89ea01cf4b258af0e345990eb213bfe57a23bcba Mon Sep 17 00:00:00 2001 From: Runs Date: Sat, 5 Sep 2026 00:54:40 -0700 Subject: [PATCH 2/2] chore: remove added tests --- codemod/populate_custom_docs.py | 13 +++---------- tests/test_populate_custom_docs.py | 30 ------------------------------ 2 files changed, 3 insertions(+), 40 deletions(-) delete mode 100644 tests/test_populate_custom_docs.py diff --git a/codemod/populate_custom_docs.py b/codemod/populate_custom_docs.py index 7806c06..c7be549 100644 --- a/codemod/populate_custom_docs.py +++ b/codemod/populate_custom_docs.py @@ -179,16 +179,9 @@ def replacement(m: re.Match[str]) -> str: @staticmethod def _append_generate_parameters(create_call: str, generate_params: str) -> str: """Append generate-only parameters after a valid comma-separated create call.""" - lines = create_call.split("\n") - for index in range(len(lines) - 1, -1, -1): - if not lines[index].strip(): - continue - if not lines[index].rstrip().endswith(","): - lines[index] = f"{lines[index].rstrip()}," - break - - create_call_with_comma = "\n".join(lines) - return f"{create_call_with_comma}\n {generate_params}\n)" + create_call = create_call.rstrip() + separator = "" if create_call.endswith(",") else "," + return f"{create_call}{separator}\n {generate_params}\n)" def _fallback_customization(self, content: str, resource_name: str) -> str: """Fallback customization when create samples aren't found.""" diff --git a/tests/test_populate_custom_docs.py b/tests/test_populate_custom_docs.py deleted file mode 100644 index 9f1a70b..0000000 --- a/tests/test_populate_custom_docs.py +++ /dev/null @@ -1,30 +0,0 @@ -from codemod.populate_custom_docs import CustomDocsPopulator - - -def test_append_generate_parameters_adds_missing_comma() -> None: - create_call = """client.v1.face_detection.generate( - assets={"target_file_path": "/path/to/1234.png"}, confidence_score=0.5""" - generate_params = ( - "wait_for_completion=True,\n" - " download_outputs=True,\n" - ' download_directory="."' - ) - - result = CustomDocsPopulator._append_generate_parameters( - create_call, generate_params - ) - - assert "confidence_score=0.5,\n wait_for_completion=True" in result - assert result.endswith('\n download_directory="."\n)') - - -def test_append_generate_parameters_preserves_existing_comma() -> None: - create_call = """client.v1.ai_image_generator.generate( - style={"prompt": "A sunset"},""" - - result = CustomDocsPopulator._append_generate_parameters( - create_call, "wait_for_completion=True," - ) - - assert "style={\"prompt\": \"A sunset\"},,\n" not in result - assert "style={\"prompt\": \"A sunset\"},\n" in result