From dfcd0ca160c190d99f7da6c6b87a0c00b56d3d7e Mon Sep 17 00:00:00 2001 From: Talmaj Marinc Date: Tue, 24 Feb 2026 21:44:25 +0100 Subject: [PATCH] Fix ruff formatting issues. --- comfy/text_encoders/longcat_image.py | 55 ++++++++++++++----- tests-unit/comfy_test/model_detection_test.py | 2 - 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/comfy/text_encoders/longcat_image.py b/comfy/text_encoders/longcat_image.py index 4560d1a2e..882d80901 100644 --- a/comfy/text_encoders/longcat_image.py +++ b/comfy/text_encoders/longcat_image.py @@ -9,7 +9,10 @@ logger = logging.getLogger(__name__) QUOTE_PAIRS = [("'", "'"), ('"', '"'), ("\u2018", "\u2019"), ("\u201c", "\u201d")] QUOTE_PATTERN = "|".join( - [re.escape(q1) + r"[^" + re.escape(q1 + q2) + r"]*?" + re.escape(q2) for q1, q2 in QUOTE_PAIRS] + [ + re.escape(q1) + r"[^" + re.escape(q1 + q2) + r"]*?" + re.escape(q2) + for q1, q2 in QUOTE_PAIRS + ] ) WORD_INTERNAL_QUOTE_RE = re.compile(r"[a-zA-Z]+'[a-zA-Z]+") @@ -52,7 +55,7 @@ class LongCatImageBaseTokenizer(Qwen25_7BVLITokenizer): all_tokens.extend(ids) if len(all_tokens) > self.max_length: - all_tokens = all_tokens[:self.max_length] + all_tokens = all_tokens[: self.max_length] logger.warning(f"Truncated prompt to {self.max_length} tokens") output = [(t, 1.0) for t in all_tokens] @@ -63,27 +66,40 @@ class LongCatImageBaseTokenizer(Qwen25_7BVLITokenizer): class LongCatImageTokenizer(sd1_clip.SD1Tokenizer): def __init__(self, embedding_directory=None, tokenizer_data={}): - super().__init__(embedding_directory=embedding_directory, tokenizer_data=tokenizer_data, name="qwen25_7b", tokenizer=LongCatImageBaseTokenizer) + super().__init__( + embedding_directory=embedding_directory, + tokenizer_data=tokenizer_data, + name="qwen25_7b", + tokenizer=LongCatImageBaseTokenizer, + ) self.longcat_template_prefix = "<|im_start|>system\nAs an image captioning expert, generate a descriptive text prompt based on an image content, suitable for input to a text-to-image model.<|im_end|>\n<|im_start|>user\n" self.longcat_template_suffix = "<|im_end|>\n<|im_start|>assistant\n" def tokenize_with_weights(self, text, return_word_ids=False, **kwargs): skip_template = False - if text.startswith('<|im_start|>'): + if text.startswith("<|im_start|>"): skip_template = True - if text.startswith('<|start_header_id|>'): + if text.startswith("<|start_header_id|>"): skip_template = True - if text == '': - text = ' ' + if text == "": + text = " " base_tok = getattr(self, "qwen25_7b") if skip_template: - tokens = super().tokenize_with_weights(text, return_word_ids=return_word_ids, disable_weights=True, **kwargs) + tokens = super().tokenize_with_weights( + text, return_word_ids=return_word_ids, disable_weights=True, **kwargs + ) else: - prefix_ids = base_tok.tokenizer(self.longcat_template_prefix, add_special_tokens=False)["input_ids"] - suffix_ids = base_tok.tokenizer(self.longcat_template_suffix, add_special_tokens=False)["input_ids"] + prefix_ids = base_tok.tokenizer( + self.longcat_template_prefix, add_special_tokens=False + )["input_ids"] + suffix_ids = base_tok.tokenizer( + self.longcat_template_suffix, add_special_tokens=False + )["input_ids"] - prompt_tokens = base_tok.tokenize_with_weights(text, return_word_ids=return_word_ids, **kwargs) + prompt_tokens = base_tok.tokenize_with_weights( + text, return_word_ids=return_word_ids, **kwargs + ) prompt_pairs = prompt_tokens[0] prefix_pairs = [(t, 1.0) for t in prefix_ids] @@ -97,7 +113,13 @@ class LongCatImageTokenizer(sd1_clip.SD1Tokenizer): class LongCatImageTEModel(sd1_clip.SD1ClipModel): def __init__(self, device="cpu", dtype=None, model_options={}): - super().__init__(device=device, dtype=dtype, name="qwen25_7b", clip_model=Qwen25_7BVLIModel, model_options=model_options) + super().__init__( + device=device, + dtype=dtype, + name="qwen25_7b", + clip_model=Qwen25_7BVLIModel, + model_options=model_options, + ) def encode_token_weights(self, token_weight_pairs, template_end=-1): out, pooled, extra = super().encode_token_weights(token_weight_pairs) @@ -116,9 +138,9 @@ class LongCatImageTEModel(sd1_clip.SD1ClipModel): if tok_pairs[template_end + 1][0] == 872: if tok_pairs[template_end + 2][0] == 198: template_end += 3 - + if template_end == -1: - template_end = 0 + template_end = 0 suffix_start = None for i in range(len(tok_pairs) - 1, -1, -1): @@ -141,7 +163,9 @@ class LongCatImageTEModel(sd1_clip.SD1ClipModel): out = out[:, :-suffix_len] if "attention_mask" in extra: extra["attention_mask"] = extra["attention_mask"][:, :-suffix_len] - if extra["attention_mask"].sum() == torch.numel(extra["attention_mask"]): + if extra["attention_mask"].sum() == torch.numel( + extra["attention_mask"] + ): extra.pop("attention_mask") return out, pooled, extra @@ -156,4 +180,5 @@ def te(dtype_llama=None, llama_quantization_metadata=None): if dtype_llama is not None: dtype = dtype_llama super().__init__(device=device, dtype=dtype, model_options=model_options) + return LongCatImageTEModel_ diff --git a/tests-unit/comfy_test/model_detection_test.py b/tests-unit/comfy_test/model_detection_test.py index a9c4189ef..60f247264 100644 --- a/tests-unit/comfy_test/model_detection_test.py +++ b/tests-unit/comfy_test/model_detection_test.py @@ -1,5 +1,4 @@ import torch -import pytest from unittest.mock import patch from comfy.model_detection import detect_unet_config, model_config_from_unet_config @@ -70,7 +69,6 @@ class TestModelDetectionSpecificity: """Specificity logic must pick LongCatImage even when FluxSchnell appears first.""" sd = _make_longcat_diffusers_sd() unet_config = detect_unet_config(sd, "") - original_models = comfy.supported_models.models longcat_cls = comfy.supported_models.LongCatImage schnell_cls = comfy.supported_models.FluxSchnell