From 101be540d0449ffb9392d9768f1d343d3e5ca62f Mon Sep 17 00:00:00 2001 From: QTom <22166516+DaydreamCoding@users.noreply.github.com> Date: Sun, 28 Sep 2025 13:37:41 +0800 Subject: [PATCH] Update comfyui_to_python.py Fixed "ComfyUI-to-Python-Extension/comfyui_to_python.py", line 414, in assemble_python_code + f"{custom_nodes}with torch.inference_mode():\n\t\t" File "src/black/__init__.py", line 1212, in format_str File "src/black/__init__.py", line 1235, in _format_str_once File "src/black/parsing.py", line 98, in lib2to3_parse black.parsing.InvalidInput: Cannot parse: --- comfyui_to_python.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/comfyui_to_python.py b/comfyui_to_python.py index d654069..7f9a287 100644 --- a/comfyui_to_python.py +++ b/comfyui_to_python.py @@ -6,6 +6,7 @@ import random import sys import re +import keyword from typing import Dict, List, Any, Callable, Tuple, TextIO from argparse import ArgumentParser @@ -183,6 +184,14 @@ def __init__(self, node_class_mappings: Dict, base_node_class_mappings: Dict): self.node_class_mappings = node_class_mappings self.base_node_class_mappings = base_node_class_mappings + @staticmethod + def is_valid_kwarg_name(name: str) -> bool: + """Return True if name is a valid Python identifier and not a keyword. + + This prevents emitting invalid kwargs like "Update inputs". + """ + return isinstance(name, str) and name.isidentifier() and not keyword.iskeyword(name) + def generate_workflow( self, load_order: List, @@ -246,11 +255,12 @@ def generate_workflow( ) no_params = class_def_params is None - # Remove any keyword arguments from **inputs if they are not in class_def_params + # Remove any invalid/unknown kwargs. If function accepts **kwargs (no_params=True) + # we still must ensure Python-valid identifiers only. inputs = { key: value for key, value in inputs.items() - if no_params or key in class_def_params + if self.is_valid_kwarg_name(key) and (no_params or key in class_def_params) } # Deal with hidden variables if ( @@ -314,6 +324,8 @@ def create_function_call_code( Returns: str: The generated Python code. """ + # Safety: filter any invalid kwarg names again at the call site + kwargs = {k: v for k, v in kwargs.items() if self.is_valid_kwarg_name(k)} args = ", ".join(self.format_arg(key, value) for key, value in kwargs.items()) # Generate the Python code