Skip to content

Commit

Permalink
Add type hint for FileLocator (#6968)
Browse files Browse the repository at this point in the history
* Add type hint for FileLocator

* nit
  • Loading branch information
huchenlei authored Mar 5, 2025
1 parent c1909f3 commit 5d84607
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 deletions.
3 changes: 2 additions & 1 deletion comfy/comfy_types/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import torch
from typing import Callable, Protocol, TypedDict, Optional, List
from .node_typing import IO, InputTypeDict, ComfyNodeABC, CheckLazyMixin
from .node_typing import IO, InputTypeDict, ComfyNodeABC, CheckLazyMixin, FileLocator


class UnetApplyFunction(Protocol):
Expand Down Expand Up @@ -42,4 +42,5 @@ class UnetParams(TypedDict):
InputTypeDict.__name__,
ComfyNodeABC.__name__,
CheckLazyMixin.__name__,
FileLocator.__name__,
]
11 changes: 11 additions & 0 deletions comfy/comfy_types/node_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,14 @@ def check_lazy_status(self, **kwargs) -> list[str]:

need = [name for name in kwargs if kwargs[name] is None]
return need


class FileLocator(TypedDict):
"""Provides type hinting for the file location"""

filename: str
"""The filename of the file."""
subfolder: str
"""The subfolder of the file."""
type: Literal["input", "output", "temp"]
"""The root folder of the file."""
5 changes: 4 additions & 1 deletion comfy_extras/nodes_audio.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import torchaudio
import torch
import comfy.model_management
Expand All @@ -10,6 +12,7 @@
import hashlib
import node_helpers
from comfy.cli_args import args
from comfy.comfy_types import FileLocator

class EmptyLatentAudio:
def __init__(self):
Expand Down Expand Up @@ -164,7 +167,7 @@ def INPUT_TYPES(s):
def save_audio(self, audio, filename_prefix="ComfyUI", prompt=None, extra_pnginfo=None):
filename_prefix += self.prefix_append
full_output_folder, filename, counter, subfolder, filename_prefix = folder_paths.get_save_image_path(filename_prefix, self.output_dir)
results = list()
results: list[FileLocator] = []

metadata = {}
if not args.disable_metadata:
Expand Down
6 changes: 5 additions & 1 deletion comfy_extras/nodes_images.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import nodes
import folder_paths
from comfy.cli_args import args
Expand All @@ -9,6 +11,8 @@
import json
import os

from comfy.comfy_types import FileLocator

MAX_RESOLUTION = nodes.MAX_RESOLUTION

class ImageCrop:
Expand Down Expand Up @@ -99,7 +103,7 @@ def save_images(self, images, fps, filename_prefix, lossless, quality, method, n
method = self.methods.get(method)
filename_prefix += self.prefix_append
full_output_folder, filename, counter, subfolder, filename_prefix = folder_paths.get_save_image_path(filename_prefix, self.output_dir, images[0].shape[1], images[0].shape[0])
results = list()
results: list[FileLocator] = []
pil_images = []
for image in images:
i = 255. * image.cpu().numpy()
Expand Down
5 changes: 4 additions & 1 deletion comfy_extras/nodes_video.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from __future__ import annotations

import os
import av
import torch
import folder_paths
import json
from fractions import Fraction
from comfy.comfy_types import FileLocator


class SaveWEBM:
Expand Down Expand Up @@ -62,7 +65,7 @@ def save_images(self, images, codec, fps, filename_prefix, crf, prompt=None, ext
container.mux(stream.encode())
container.close()

results = [{
results: list[FileLocator] = [{
"filename": file,
"subfolder": subfolder,
"type": self.type
Expand Down
4 changes: 2 additions & 2 deletions nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import comfy.sd
import comfy.utils
import comfy.controlnet
from comfy.comfy_types import IO, ComfyNodeABC, InputTypeDict
from comfy.comfy_types import IO, ComfyNodeABC, InputTypeDict, FileLocator

import comfy.clip_vision

Expand Down Expand Up @@ -479,7 +479,7 @@ def save(self, samples, filename_prefix="ComfyUI", prompt=None, extra_pnginfo=No

file = f"{filename}_{counter:05}_.latent"

results = list()
results: list[FileLocator] = []
results.append({
"filename": file,
"subfolder": subfolder,
Expand Down

0 comments on commit 5d84607

Please sign in to comment.