Skip to content

Commit 43e0d4e

Browse files
authored
comfy_api: remove usage of "Type","List" and "Dict" types (#11238)
1 parent dbd3304 commit 43e0d4e

File tree

8 files changed

+38
-40
lines changed

8 files changed

+38
-40
lines changed

comfy_api/feature_flags.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
allowing graceful protocol evolution while maintaining backward compatibility.
66
"""
77

8-
from typing import Any, Dict
8+
from typing import Any
99

1010
from comfy.cli_args import args
1111

1212
# Default server capabilities
13-
SERVER_FEATURE_FLAGS: Dict[str, Any] = {
13+
SERVER_FEATURE_FLAGS: dict[str, Any] = {
1414
"supports_preview_metadata": True,
1515
"max_upload_size": args.max_upload_size * 1024 * 1024, # Convert MB to bytes
1616
"extension": {"manager": {"supports_v4": True}},
1717
}
1818

1919

2020
def get_connection_feature(
21-
sockets_metadata: Dict[str, Dict[str, Any]],
21+
sockets_metadata: dict[str, dict[str, Any]],
2222
sid: str,
2323
feature_name: str,
2424
default: Any = False
@@ -42,7 +42,7 @@ def get_connection_feature(
4242

4343

4444
def supports_feature(
45-
sockets_metadata: Dict[str, Dict[str, Any]],
45+
sockets_metadata: dict[str, dict[str, Any]],
4646
sid: str,
4747
feature_name: str
4848
) -> bool:
@@ -60,7 +60,7 @@ def supports_feature(
6060
return get_connection_feature(sockets_metadata, sid, feature_name, False) is True
6161

6262

63-
def get_server_features() -> Dict[str, Any]:
63+
def get_server_features() -> dict[str, Any]:
6464
"""
6565
Get the server's feature flags.
6666

comfy_api/internal/api_registry.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Type, List, NamedTuple
1+
from typing import NamedTuple
22
from comfy_api.internal.singleton import ProxiedSingleton
33
from packaging import version as packaging_version
44

@@ -10,7 +10,7 @@ def __init__(self):
1010

1111
class ComfyAPIWithVersion(NamedTuple):
1212
version: str
13-
api_class: Type[ComfyAPIBase]
13+
api_class: type[ComfyAPIBase]
1414

1515

1616
def parse_version(version_str: str) -> packaging_version.Version:
@@ -23,16 +23,16 @@ def parse_version(version_str: str) -> packaging_version.Version:
2323
return packaging_version.parse(version_str)
2424

2525

26-
registered_versions: List[ComfyAPIWithVersion] = []
26+
registered_versions: list[ComfyAPIWithVersion] = []
2727

2828

29-
def register_versions(versions: List[ComfyAPIWithVersion]):
29+
def register_versions(versions: list[ComfyAPIWithVersion]):
3030
versions.sort(key=lambda x: parse_version(x.version))
3131
global registered_versions
3232
registered_versions = versions
3333

3434

35-
def get_all_versions() -> List[ComfyAPIWithVersion]:
35+
def get_all_versions() -> list[ComfyAPIWithVersion]:
3636
"""
3737
Returns a list of all registered ComfyAPI versions.
3838
"""

comfy_api/internal/async_to_sync.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import textwrap
99
import threading
1010
from enum import Enum
11-
from typing import Optional, Type, get_origin, get_args, get_type_hints
11+
from typing import Optional, get_origin, get_args, get_type_hints
1212

1313

1414
class TypeTracker:
@@ -193,7 +193,7 @@ async def run_with_context():
193193
return result_container["result"]
194194

195195
@classmethod
196-
def create_sync_class(cls, async_class: Type, thread_pool_size=10) -> Type:
196+
def create_sync_class(cls, async_class: type, thread_pool_size=10) -> type:
197197
"""
198198
Creates a new class with synchronous versions of all async methods.
199199
@@ -563,7 +563,7 @@ def _generate_method_signature(
563563

564564
@classmethod
565565
def _generate_imports(
566-
cls, async_class: Type, type_tracker: TypeTracker
566+
cls, async_class: type, type_tracker: TypeTracker
567567
) -> list[str]:
568568
"""Generate import statements for the stub file."""
569569
imports = []
@@ -628,7 +628,7 @@ def _generate_imports(
628628
return imports
629629

630630
@classmethod
631-
def _get_class_attributes(cls, async_class: Type) -> list[tuple[str, Type]]:
631+
def _get_class_attributes(cls, async_class: type) -> list[tuple[str, type]]:
632632
"""Extract class attributes that are classes themselves."""
633633
class_attributes = []
634634

@@ -654,7 +654,7 @@ def _get_class_attributes(cls, async_class: Type) -> list[tuple[str, Type]]:
654654
def _generate_inner_class_stub(
655655
cls,
656656
name: str,
657-
attr: Type,
657+
attr: type,
658658
indent: str = " ",
659659
type_tracker: Optional[TypeTracker] = None,
660660
) -> list[str]:
@@ -782,7 +782,7 @@ def _post_process_stub_content(cls, stub_content: list[str]) -> list[str]:
782782
return processed
783783

784784
@classmethod
785-
def generate_stub_file(cls, async_class: Type, sync_class: Type) -> None:
785+
def generate_stub_file(cls, async_class: type, sync_class: type) -> None:
786786
"""
787787
Generate a .pyi stub file for the sync class to help IDEs with type checking.
788788
"""
@@ -988,7 +988,7 @@ def generate_stub_file(cls, async_class: Type, sync_class: Type) -> None:
988988
logging.error(traceback.format_exc())
989989

990990

991-
def create_sync_class(async_class: Type, thread_pool_size=10) -> Type:
991+
def create_sync_class(async_class: type, thread_pool_size=10) -> type:
992992
"""
993993
Creates a sync version of an async class
994994

comfy_api/internal/singleton.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Type, TypeVar
1+
from typing import TypeVar
22

33
class SingletonMetaclass(type):
44
T = TypeVar("T", bound="SingletonMetaclass")
@@ -11,13 +11,13 @@ def __call__(cls, *args, **kwargs):
1111
)
1212
return cls._instances[cls]
1313

14-
def inject_instance(cls: Type[T], instance: T) -> None:
14+
def inject_instance(cls: type[T], instance: T) -> None:
1515
assert cls not in SingletonMetaclass._instances, (
1616
"Cannot inject instance after first instantiation"
1717
)
1818
SingletonMetaclass._instances[cls] = instance
1919

20-
def get_instance(cls: Type[T], *args, **kwargs) -> T:
20+
def get_instance(cls: type[T], *args, **kwargs) -> T:
2121
"""
2222
Gets the singleton instance of the class, creating it if it doesn't exist.
2323
"""

comfy_api/latest/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from abc import ABC, abstractmethod
4-
from typing import Type, TYPE_CHECKING
4+
from typing import TYPE_CHECKING
55
from comfy_api.internal import ComfyAPIBase
66
from comfy_api.internal.singleton import ProxiedSingleton
77
from comfy_api.internal.async_to_sync import create_sync_class
@@ -113,7 +113,7 @@ class Types:
113113
if TYPE_CHECKING:
114114
import comfy_api.latest.generated.ComfyAPISyncStub # type: ignore
115115

116-
ComfyAPISync: Type[comfy_api.latest.generated.ComfyAPISyncStub.ComfyAPISyncStub]
116+
ComfyAPISync: type[comfy_api.latest.generated.ComfyAPISyncStub.ComfyAPISyncStub]
117117
ComfyAPISync = create_sync_class(ComfyAPI_latest)
118118

119119
# create new aliases for io and ui

comfy_api/latest/_input/basic_types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import torch
2-
from typing import TypedDict, List, Optional
2+
from typing import TypedDict, Optional
33

44
ImageInput = torch.Tensor
55
"""
@@ -39,4 +39,4 @@ class LatentInput(TypedDict):
3939
Optional noise mask tensor in the same format as samples.
4040
"""
4141

42-
batch_index: Optional[List[int]]
42+
batch_index: Optional[list[int]]

comfy_api/latest/_ui.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import random
66
import uuid
77
from io import BytesIO
8-
from typing import Type
98

109
import av
1110
import numpy as np
@@ -83,7 +82,7 @@ def _convert_tensor_to_pil(image_tensor: torch.Tensor) -> PILImage.Image:
8382
return PILImage.fromarray(np.clip(255.0 * image_tensor.cpu().numpy(), 0, 255).astype(np.uint8))
8483

8584
@staticmethod
86-
def _create_png_metadata(cls: Type[ComfyNode] | None) -> PngInfo | None:
85+
def _create_png_metadata(cls: type[ComfyNode] | None) -> PngInfo | None:
8786
"""Creates a PngInfo object with prompt and extra_pnginfo."""
8887
if args.disable_metadata or cls is None or not cls.hidden:
8988
return None
@@ -96,7 +95,7 @@ def _create_png_metadata(cls: Type[ComfyNode] | None) -> PngInfo | None:
9695
return metadata
9796

9897
@staticmethod
99-
def _create_animated_png_metadata(cls: Type[ComfyNode] | None) -> PngInfo | None:
98+
def _create_animated_png_metadata(cls: type[ComfyNode] | None) -> PngInfo | None:
10099
"""Creates a PngInfo object with prompt and extra_pnginfo for animated PNGs (APNG)."""
101100
if args.disable_metadata or cls is None or not cls.hidden:
102101
return None
@@ -121,7 +120,7 @@ def _create_animated_png_metadata(cls: Type[ComfyNode] | None) -> PngInfo | None
121120
return metadata
122121

123122
@staticmethod
124-
def _create_webp_metadata(pil_image: PILImage.Image, cls: Type[ComfyNode] | None) -> PILImage.Exif:
123+
def _create_webp_metadata(pil_image: PILImage.Image, cls: type[ComfyNode] | None) -> PILImage.Exif:
125124
"""Creates EXIF metadata bytes for WebP images."""
126125
exif_data = pil_image.getexif()
127126
if args.disable_metadata or cls is None or cls.hidden is None:
@@ -137,7 +136,7 @@ def _create_webp_metadata(pil_image: PILImage.Image, cls: Type[ComfyNode] | None
137136

138137
@staticmethod
139138
def save_images(
140-
images, filename_prefix: str, folder_type: FolderType, cls: Type[ComfyNode] | None, compress_level = 4,
139+
images, filename_prefix: str, folder_type: FolderType, cls: type[ComfyNode] | None, compress_level = 4,
141140
) -> list[SavedResult]:
142141
"""Saves a batch of images as individual PNG files."""
143142
full_output_folder, filename, counter, subfolder, _ = folder_paths.get_save_image_path(
@@ -155,7 +154,7 @@ def save_images(
155154
return results
156155

157156
@staticmethod
158-
def get_save_images_ui(images, filename_prefix: str, cls: Type[ComfyNode] | None, compress_level=4) -> SavedImages:
157+
def get_save_images_ui(images, filename_prefix: str, cls: type[ComfyNode] | None, compress_level=4) -> SavedImages:
159158
"""Saves a batch of images and returns a UI object for the node output."""
160159
return SavedImages(
161160
ImageSaveHelper.save_images(
@@ -169,7 +168,7 @@ def get_save_images_ui(images, filename_prefix: str, cls: Type[ComfyNode] | None
169168

170169
@staticmethod
171170
def save_animated_png(
172-
images, filename_prefix: str, folder_type: FolderType, cls: Type[ComfyNode] | None, fps: float, compress_level: int
171+
images, filename_prefix: str, folder_type: FolderType, cls: type[ComfyNode] | None, fps: float, compress_level: int
173172
) -> SavedResult:
174173
"""Saves a batch of images as a single animated PNG."""
175174
full_output_folder, filename, counter, subfolder, _ = folder_paths.get_save_image_path(
@@ -191,7 +190,7 @@ def save_animated_png(
191190

192191
@staticmethod
193192
def get_save_animated_png_ui(
194-
images, filename_prefix: str, cls: Type[ComfyNode] | None, fps: float, compress_level: int
193+
images, filename_prefix: str, cls: type[ComfyNode] | None, fps: float, compress_level: int
195194
) -> SavedImages:
196195
"""Saves an animated PNG and returns a UI object for the node output."""
197196
result = ImageSaveHelper.save_animated_png(
@@ -209,7 +208,7 @@ def save_animated_webp(
209208
images,
210209
filename_prefix: str,
211210
folder_type: FolderType,
212-
cls: Type[ComfyNode] | None,
211+
cls: type[ComfyNode] | None,
213212
fps: float,
214213
lossless: bool,
215214
quality: int,
@@ -238,7 +237,7 @@ def save_animated_webp(
238237
def get_save_animated_webp_ui(
239238
images,
240239
filename_prefix: str,
241-
cls: Type[ComfyNode] | None,
240+
cls: type[ComfyNode] | None,
242241
fps: float,
243242
lossless: bool,
244243
quality: int,
@@ -267,7 +266,7 @@ def save_audio(
267266
audio: dict,
268267
filename_prefix: str,
269268
folder_type: FolderType,
270-
cls: Type[ComfyNode] | None,
269+
cls: type[ComfyNode] | None,
271270
format: str = "flac",
272271
quality: str = "128k",
273272
) -> list[SavedResult]:
@@ -372,7 +371,7 @@ def save_audio(
372371

373372
@staticmethod
374373
def get_save_audio_ui(
375-
audio, filename_prefix: str, cls: Type[ComfyNode] | None, format: str = "flac", quality: str = "128k",
374+
audio, filename_prefix: str, cls: type[ComfyNode] | None, format: str = "flac", quality: str = "128k",
376375
) -> SavedAudios:
377376
"""Save and instantly wrap for UI."""
378377
return SavedAudios(
@@ -388,7 +387,7 @@ def get_save_audio_ui(
388387

389388

390389
class PreviewImage(_UIOutput):
391-
def __init__(self, image: Image.Type, animated: bool = False, cls: Type[ComfyNode] = None, **kwargs):
390+
def __init__(self, image: Image.Type, animated: bool = False, cls: type[ComfyNode] = None, **kwargs):
392391
self.values = ImageSaveHelper.save_images(
393392
image,
394393
filename_prefix="ComfyUI_temp_" + ''.join(random.choice("abcdefghijklmnopqrstupvxyz") for _ in range(5)),
@@ -412,7 +411,7 @@ def __init__(self, mask: PreviewMask.Type, animated: bool=False, cls: ComfyNode=
412411

413412

414413
class PreviewAudio(_UIOutput):
415-
def __init__(self, audio: dict, cls: Type[ComfyNode] = None, **kwargs):
414+
def __init__(self, audio: dict, cls: type[ComfyNode] = None, **kwargs):
416415
self.values = AudioSaveHelper.save_audio(
417416
audio,
418417
filename_prefix="ComfyUI_temp_" + "".join(random.choice("abcdefghijklmnopqrstuvwxyz") for _ in range(5)),

comfy_api/version_list.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
from comfy_api.v0_0_2 import ComfyAPIAdapter_v0_0_2
33
from comfy_api.v0_0_1 import ComfyAPIAdapter_v0_0_1
44
from comfy_api.internal import ComfyAPIBase
5-
from typing import List, Type
65

7-
supported_versions: List[Type[ComfyAPIBase]] = [
6+
supported_versions: list[type[ComfyAPIBase]] = [
87
ComfyAPI_latest,
98
ComfyAPIAdapter_v0_0_2,
109
ComfyAPIAdapter_v0_0_1,

0 commit comments

Comments
 (0)