Fix ComfyUI custom node package imports

This commit is contained in:
CyberShadowX
2026-05-28 11:11:31 +08:00
parent 23f81f91da
commit 185749cf63
9 changed files with 63 additions and 14 deletions
+6 -2
View File
@@ -5,8 +5,12 @@ import os
from pathlib import Path
from typing import Any
from smart_metadata_reader.image_io import load_image_and_mask
from smart_metadata_reader.metadata_reader import parse_metadata_bundle, read_metadata
try:
from .smart_metadata_reader.image_io import load_image_and_mask
from .smart_metadata_reader.metadata_reader import parse_metadata_bundle, read_metadata
except ImportError:
from smart_metadata_reader.image_io import load_image_and_mask
from smart_metadata_reader.metadata_reader import parse_metadata_bundle, read_metadata
try:
import folder_paths # type: ignore
+3 -3
View File
@@ -2,9 +2,9 @@ from __future__ import annotations
from typing import Protocol
from smart_metadata_reader.graph import GraphIndex
from smart_metadata_reader.models import NodeRecord, PromptSegment
from smart_metadata_reader.trace import TraceCollector
from ..graph import GraphIndex
from ..models import NodeRecord, PromptSegment
from ..trace import TraceCollector
class NodeAdapter(Protocol):
+2 -2
View File
@@ -1,7 +1,7 @@
from __future__ import annotations
from smart_metadata_reader.adapters.base import TextResolutionContext
from smart_metadata_reader.models import NodeRecord, PromptSegment
from .base import TextResolutionContext
from ..models import NodeRecord, PromptSegment
class CLIPTextEncodeAdapter:
@@ -1,6 +1,6 @@
from __future__ import annotations
from smart_metadata_reader.models import NodeRecord
from ..models import NodeRecord
ROLE_FIELD_ORDER = {
+1 -1
View File
@@ -2,7 +2,7 @@ from __future__ import annotations
from typing import Any
from smart_metadata_reader.models import NodeRecord
from ..models import NodeRecord
CHECKPOINT_NAME_FIELDS = ("ckpt_name", "checkpoint", "model_name", "name")
+2 -2
View File
@@ -1,7 +1,7 @@
from __future__ import annotations
from smart_metadata_reader.adapters.base import TextResolutionContext
from smart_metadata_reader.models import NodeRecord, PromptSegment
from .base import TextResolutionContext
from ..models import NodeRecord, PromptSegment
class StringFunctionAdapter:
+1 -1
View File
@@ -2,7 +2,7 @@ from __future__ import annotations
from typing import Any
from smart_metadata_reader.models import NodeRecord
from ..models import NodeRecord
SAMPLER_CLASS_TYPES = {
@@ -1,7 +1,7 @@
from __future__ import annotations
from smart_metadata_reader.adapters.base import TextResolutionContext
from smart_metadata_reader.models import NodeRecord, PromptSegment
from .base import TextResolutionContext
from ..models import NodeRecord, PromptSegment
COMMON_TEXT_FIELDS = (
+45
View File
@@ -1,6 +1,8 @@
import json
import importlib.util
import subprocess
import sys
import textwrap
from types import SimpleNamespace
import pytest
@@ -184,6 +186,49 @@ def test_plugin_root_init_exports_comfyui_mappings():
assert module.NODE_DISPLAY_NAME_MAPPINGS["SmartMetadataReader"] == "Smart Metadata Reader"
def test_plugin_package_import_works_without_repo_root_on_sys_path(tmp_path):
root_dir = pytest.importorskip("pathlib").Path(__file__).resolve().parents[1]
script = textwrap.dedent(
f"""
import importlib.util
import pathlib
import sys
root = pathlib.Path({str(root_dir)!r})
sys.path = [
path for path in sys.path
if pathlib.Path(path or ".").resolve() != root
]
for name in list(sys.modules):
if name == "smart_metadata_reader" or name.startswith("smart_metadata_reader."):
sys.modules.pop(name, None)
spec = importlib.util.spec_from_file_location(
"comfyui_custom_node_under_test",
root / "__init__.py",
submodule_search_locations=[str(root)],
)
module = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = module
spec.loader.exec_module(module)
assert "SmartMetadataReader" in module.NODE_CLASS_MAPPINGS
assert module.NODE_DISPLAY_NAME_MAPPINGS["SmartMetadataReader"] == "Smart Metadata Reader"
print("package import ok")
"""
)
completed = subprocess.run(
[sys.executable, "-c", script],
cwd=tmp_path,
text=True,
capture_output=True,
check=False,
)
assert completed.returncode == 0, completed.stderr
assert "package import ok" in completed.stdout
def test_input_types_prefers_comfyui_filename_list(monkeypatch):
import nodes