refactor: return NamedTuples

This commit is contained in:
Lex Darlog (DRL)
2025-07-19 06:48:01 +05:00
parent 0f6597cf49
commit 646f7bf28d
2 changed files with 26 additions and 4 deletions
+5 -4
View File
@@ -11,6 +11,7 @@ from math import sqrt as _sqrt
from server import PromptServer as _PromptServer
from .enums import *
from .return_tuples import *
_t_number = _t.Union[int, float]
@@ -159,12 +160,12 @@ def simple_result_from_approx_wh(
width_f: float, height_f: _t_number, step: int,
show: bool = True,
unique_id: str = None, target_square_size: _t_number = None
):
) -> ResultSimple:
"""Final part of the main func for simple (non-upscale) nodes - when desired width/height are already calculated."""
step = number_to_int(step)
width, n_steps_x, height, n_steps_y = round_width_and_height_closest_to_the_ratio(width_f, height_f, step)
result = (width, height)
result = ResultSimple(width, height)
if not unique_id:
return result
@@ -199,7 +200,7 @@ def upscale_result_from_approx_wh(
priority: _t.Union[RoundingPriority, str], upscale: float, up_step:int,
show: bool = True,
unique_id: str = None, target_square_size: _t_number = None
):
) -> ResultUpscaled:
"""Primary part of the main func for nodes with upscaling - when desired initial-width/height are already calculated."""
upscale = max(float(upscale), 1.0)
up_width_f: float = upscale * width_f
@@ -230,7 +231,7 @@ def upscale_result_from_approx_wh(
needs_resize, real_upscale_avg, real_upscale_x, real_upscale_y = _need_post_resize(width, height, up_width, up_height)
out_upscale: float = upscale if needs_resize else real_upscale_avg
result = (out_upscale, width, height, up_width, up_height, needs_resize)
result = ResultUpscaled(out_upscale, width, height, up_width, up_height, needs_resize)
if not unique_id:
return result
+21
View File
@@ -0,0 +1,21 @@
# encoding: utf-8
"""
For readability, all nodes in this node pack return NamedTuples instead of regular tuples.
"""
import typing as _t
class ResultSimple(_t.NamedTuple):
"""Returned NamedTuple for simple (non-upscale) nodes."""
width: int
height: int
class ResultUpscaled(_t.NamedTuple):
"""Returned NamedTuple for nodes with upscaling."""
upscale: float
orig_width: int
orig_height: int
up_width: int
up_height: int
needs_resize: bool