From 783f20b5d773d3e7b3b719706cf7fb328e0ee9c0 Mon Sep 17 00:00:00 2001 From: awsr <43862868+awsr@users.noreply.github.com> Date: Fri, 20 Mar 2026 18:42:00 -0700 Subject: [PATCH 01/25] Remove RUF013 from ignored rules --- pyproject.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index cd517e67a..57db5b62f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -97,7 +97,6 @@ ignore = [ "RUF008", # Do not use mutable default values for dataclass "RUF010", # Use explicit conversion flag "RUF012", # Mutable class attributes - "RUF013", # PEP 484 prohibits implicit `Optional` "RUF015", # Prefer `next(...)` over single element slice "RUF022", # All is not sorted "RUF046", # Value being cast to `int` is already an integer From 8e10ec3fec31bbb516893ebe1a8e594645823378 Mon Sep 17 00:00:00 2001 From: awsr <43862868+awsr@users.noreply.github.com> Date: Fri, 20 Mar 2026 18:42:44 -0700 Subject: [PATCH 02/25] RUF013 compatibility --- cli/api-checkpoint.py | 2 +- cli/api-control.py | 2 +- cli/api-detect.py | 2 +- cli/api-enhance.py | 2 +- cli/api-faceid.py | 2 +- cli/api-grid.py | 2 +- cli/api-img2img.py | 2 +- cli/api-info.py | 4 ++-- cli/api-json.py | 2 +- cli/api-mask.py | 4 ++-- cli/api-preprocess.py | 4 ++-- cli/api-txt2img.py | 2 +- cli/api-upscale.py | 4 ++-- cli/api-vqa.py | 4 ++-- cli/api-xyz.py | 2 +- cli/api-xyzenum.py | 2 +- cli/civitai-search.py | 6 +++--- cli/gen-styles.py | 2 +- cli/image-search.py | 6 +++--- cli/process.py | 4 ++-- cli/sdapi.py | 8 ++++---- 21 files changed, 34 insertions(+), 34 deletions(-) diff --git a/cli/api-checkpoint.py b/cli/api-checkpoint.py index ff939f64e..61c799d83 100755 --- a/cli/api-checkpoint.py +++ b/cli/api-checkpoint.py @@ -24,7 +24,7 @@ def auth(): return None -def get(endpoint: str, dct: dict = None): +def get(endpoint: str, dct: dict | None = None): req = requests.get(f'{sd_url}{endpoint}', json = dct, timeout=300, verify=False, auth=auth()) if req.status_code != 200: return { 'error': req.status_code, 'reason': req.reason, 'url': req.url } diff --git a/cli/api-control.py b/cli/api-control.py index 0e73e94be..865e5b54d 100755 --- a/cli/api-control.py +++ b/cli/api-control.py @@ -30,7 +30,7 @@ def auth(): return None -def post(endpoint: str, dct: dict = None): +def post(endpoint: str, dct: dict | None = None): req = requests.post(f'{sd_url}{endpoint}', json = dct, timeout=300, verify=False, auth=auth()) if req.status_code != 200: return { 'error': req.status_code, 'reason': req.reason, 'url': req.url } diff --git a/cli/api-detect.py b/cli/api-detect.py index ca121b220..8d1ab2b7a 100755 --- a/cli/api-detect.py +++ b/cli/api-detect.py @@ -23,7 +23,7 @@ def auth(): return None -def post(endpoint: str, dct: dict = None): +def post(endpoint: str, dct: dict | None = None): req = requests.post(f'{sd_url}{endpoint}', json = dct, timeout=300, verify=False, auth=auth()) if req.status_code != 200: return { 'error': req.status_code, 'reason': req.reason, 'url': req.url } diff --git a/cli/api-enhance.py b/cli/api-enhance.py index 520625edd..0acb7d1ab 100755 --- a/cli/api-enhance.py +++ b/cli/api-enhance.py @@ -24,7 +24,7 @@ def auth(): return None -def post(endpoint: str, dct: dict = None): +def post(endpoint: str, dct: dict | None = None): req = requests.post(f'{sd_url}{endpoint}', json = dct, timeout=300, verify=False, auth=auth()) if req.status_code != 200: return { 'error': req.status_code, 'reason': req.reason, 'url': req.url } diff --git a/cli/api-faceid.py b/cli/api-faceid.py index 18f1d3503..bf9c8a1f0 100755 --- a/cli/api-faceid.py +++ b/cli/api-faceid.py @@ -29,7 +29,7 @@ def auth(): return None -def post(endpoint: str, dct: dict = None): +def post(endpoint: str, dct: dict | None = None): req = requests.post(f'{sd_url}{endpoint}', json = dct, timeout=300, verify=False, auth=auth()) if req.status_code != 200: return { 'error': req.status_code, 'reason': req.reason, 'url': req.url } diff --git a/cli/api-grid.py b/cli/api-grid.py index e41276bb9..769fbab35 100755 --- a/cli/api-grid.py +++ b/cli/api-grid.py @@ -79,7 +79,7 @@ def generate(x: int, y: int): # pylint: disable=redefined-outer-name return images -def merge(images: list[Image.Image], horizontal: bool, labels: list[str] = None): +def merge(images: list[Image.Image], horizontal: bool, labels: list[str] | None = None): rows = 1 if horizontal else len(images) cols = math.ceil(len(images) / rows) w = max([i.size[0] for i in images]) diff --git a/cli/api-img2img.py b/cli/api-img2img.py index 99ab5ac34..49b23b5d7 100755 --- a/cli/api-img2img.py +++ b/cli/api-img2img.py @@ -29,7 +29,7 @@ def auth(): return None -def post(endpoint: str, dct: dict = None): +def post(endpoint: str, dct: dict | None = None): req = requests.post(f'{sd_url}{endpoint}', json = dct, timeout=300, verify=False, auth=auth()) if req.status_code != 200: return { 'error': req.status_code, 'reason': req.reason, 'url': req.url } diff --git a/cli/api-info.py b/cli/api-info.py index 1056ddf2a..8032a628c 100755 --- a/cli/api-info.py +++ b/cli/api-info.py @@ -24,7 +24,7 @@ def auth(): return None -def get(endpoint: str, dct: dict = None): +def get(endpoint: str, dct: dict | None = None): req = requests.get(f'{sd_url}{endpoint}', json=dct, timeout=300, verify=False, auth=auth()) if req.status_code != 200: return { 'error': req.status_code, 'reason': req.reason, 'url': req.url } @@ -32,7 +32,7 @@ def get(endpoint: str, dct: dict = None): return req.json() -def post(endpoint: str, dct: dict = None): +def post(endpoint: str, dct: dict | None = None): req = requests.post(f'{sd_url}{endpoint}', json = dct, timeout=300, verify=False, auth=auth()) if req.status_code != 200: return { 'error': req.status_code, 'reason': req.reason, 'url': req.url } diff --git a/cli/api-json.py b/cli/api-json.py index 61e5ec3ce..d0e657af6 100755 --- a/cli/api-json.py +++ b/cli/api-json.py @@ -28,7 +28,7 @@ def auth(): return None -def post(endpoint: str, payload: dict = None): +def post(endpoint: str, payload: dict | None = None): if 'sdapi' not in endpoint: endpoint = f'sdapi/v1/{endpoint}' if 'http' not in endpoint: diff --git a/cli/api-mask.py b/cli/api-mask.py index 38aae3018..f8ee56a10 100755 --- a/cli/api-mask.py +++ b/cli/api-mask.py @@ -26,7 +26,7 @@ def auth(): return None -def get(endpoint: str, dct: dict = None): +def get(endpoint: str, dct: dict | None = None): req = requests.get(f'{sd_url}{endpoint}', json=dct, timeout=300, verify=False, auth=auth()) if req.status_code != 200: return { 'error': req.status_code, 'reason': req.reason, 'url': req.url } @@ -34,7 +34,7 @@ def get(endpoint: str, dct: dict = None): return req.json() -def post(endpoint: str, dct: dict = None): +def post(endpoint: str, dct: dict | None = None): req = requests.post(f'{sd_url}{endpoint}', json = dct, timeout=300, verify=False, auth=auth()) if req.status_code != 200: return { 'error': req.status_code, 'reason': req.reason, 'url': req.url } diff --git a/cli/api-preprocess.py b/cli/api-preprocess.py index abb6a9d2f..b51bfbed9 100755 --- a/cli/api-preprocess.py +++ b/cli/api-preprocess.py @@ -26,7 +26,7 @@ def auth(): return None -def get(endpoint: str, dct: dict = None): +def get(endpoint: str, dct: dict | None = None): req = requests.get(f'{sd_url}{endpoint}', json=dct, timeout=300, verify=False, auth=auth()) if req.status_code != 200: return { 'error': req.status_code, 'reason': req.reason, 'url': req.url } @@ -34,7 +34,7 @@ def get(endpoint: str, dct: dict = None): return req.json() -def post(endpoint: str, dct: dict = None): +def post(endpoint: str, dct: dict | None = None): req = requests.post(f'{sd_url}{endpoint}', json = dct, timeout=300, verify=False, auth=auth()) if req.status_code != 200: return { 'error': req.status_code, 'reason': req.reason, 'url': req.url } diff --git a/cli/api-txt2img.py b/cli/api-txt2img.py index 19ee6d474..648b72576 100755 --- a/cli/api-txt2img.py +++ b/cli/api-txt2img.py @@ -28,7 +28,7 @@ def auth(): return None -def post(endpoint: str, dct: dict = None): +def post(endpoint: str, dct: dict | None = None): req = requests.post(f'{sd_url}{endpoint}', json = dct, timeout=300, verify=False, auth=auth()) if req.status_code != 200: return { 'error': req.status_code, 'reason': req.reason, 'url': req.url } diff --git a/cli/api-upscale.py b/cli/api-upscale.py index 488f2db45..2498da964 100755 --- a/cli/api-upscale.py +++ b/cli/api-upscale.py @@ -24,7 +24,7 @@ def auth(): return None -def get(endpoint: str, dct: dict = None): +def get(endpoint: str, dct: dict | None = None): req = requests.get(f'{sd_url}{endpoint}', json=dct, timeout=300, verify=False, auth=auth()) if req.status_code != 200: return { 'error': req.status_code, 'reason': req.reason, 'url': req.url } @@ -32,7 +32,7 @@ def get(endpoint: str, dct: dict = None): return req.json() -def post(endpoint: str, dct: dict = None): +def post(endpoint: str, dct: dict | None = None): req = requests.post(f'{sd_url}{endpoint}', json = dct, timeout=300, verify=False, auth=auth()) if req.status_code != 200: return { 'error': req.status_code, 'reason': req.reason, 'url': req.url } diff --git a/cli/api-vqa.py b/cli/api-vqa.py index 87a3ef2b6..b93d8a15f 100755 --- a/cli/api-vqa.py +++ b/cli/api-vqa.py @@ -24,7 +24,7 @@ def auth(): return None -def get(endpoint: str, dct: dict = None): +def get(endpoint: str, dct: dict | None = None): req = requests.get(f'{sd_url}{endpoint}', json=dct, timeout=300, verify=False, auth=auth()) if req.status_code != 200: return { 'error': req.status_code, 'reason': req.reason, 'url': req.url } @@ -32,7 +32,7 @@ def get(endpoint: str, dct: dict = None): return req.json() -def post(endpoint: str, dct: dict = None): +def post(endpoint: str, dct: dict | None = None): req = requests.post(f'{sd_url}{endpoint}', json = dct, timeout=300, verify=False, auth=auth()) if req.status_code != 200: return { 'error': req.status_code, 'reason': req.reason, 'url': req.url } diff --git a/cli/api-xyz.py b/cli/api-xyz.py index e82fa23a8..50a0f3ecf 100755 --- a/cli/api-xyz.py +++ b/cli/api-xyz.py @@ -30,7 +30,7 @@ def auth(): return None -def post(endpoint: str, dct: dict = None): +def post(endpoint: str, dct: dict | None = None): req = requests.post(f'{sd_url}{endpoint}', json = dct, timeout=300, verify=False, auth=auth()) if req.status_code != 200: return { 'error': req.status_code, 'reason': req.reason, 'url': req.url } diff --git a/cli/api-xyzenum.py b/cli/api-xyzenum.py index e5eb12f83..0decd02f3 100755 --- a/cli/api-xyzenum.py +++ b/cli/api-xyzenum.py @@ -24,7 +24,7 @@ def auth(): return None -def get(endpoint: str, dct: dict = None): +def get(endpoint: str, dct: dict | None = None): req = requests.get(f'{sd_url}{endpoint}', json = dct, timeout=300, verify=False, auth=auth()) if req.status_code != 200: return { 'error': req.status_code, 'reason': req.reason, 'url': req.url } diff --git a/cli/civitai-search.py b/cli/civitai-search.py index cdb29c7ab..b8fbc68b3 100755 --- a/cli/civitai-search.py +++ b/cli/civitai-search.py @@ -99,10 +99,10 @@ def search_civitai( types:str = '', # (Checkpoint, TextualInversion, Hypernetwork, AestheticGradient, LORA, Controlnet, Poses) sort:str = '', # (Highest Rated, Most Downloaded, Newest) period:str = '', # (AllTime, Year, Month, Week, Day) - nsfw:bool = None, # optional:bool + nsfw:bool | None = None, # optional:bool limit:int = 0, base:list[str] = [], # list - token:str = None, + token:str | None = None, exact:bool = True, ): import requests @@ -169,7 +169,7 @@ def search_civitai( return exact_models if len(exact_models) > 0 else models -def models_to_dct(all_models:list, model_id:int=None): +def models_to_dct(all_models:list, model_id:int | None=None): dct = [] for model in all_models: if model_id is not None and model.id != model_id: diff --git a/cli/gen-styles.py b/cli/gen-styles.py index ec1f1089f..1171265a5 100755 --- a/cli/gen-styles.py +++ b/cli/gen-styles.py @@ -33,7 +33,7 @@ def pil_to_b64(img: Image, size: int, quality: int): return f'data:image/jpeg;base64,{b64encoded}' -def post(endpoint: str, dct: dict = None): +def post(endpoint: str, dct: dict | None = None): req = requests.post(endpoint, json = dct, timeout=300, verify=False) if req.status_code != 200: return { 'error': req.status_code, 'reason': req.reason, 'url': req.url } diff --git a/cli/image-search.py b/cli/image-search.py index 8b740a82e..5be923e6e 100755 --- a/cli/image-search.py +++ b/cli/image-search.py @@ -19,7 +19,7 @@ class ImageDB: def __init__(self, name:str='db', fmt:str='json', - cache_dir:str=None, + cache_dir:str | None=None, dtype:torch.dtype=torch.float16, device:torch.device=torch.device('cpu'), model:str='openai/clip-vit-large-patch14', # 'facebook/dinov2-small' @@ -123,8 +123,8 @@ class ImageDB: self.df = rec self.index.add(embed) - def search(self, filename: str = None, metadata: str = None, embed: np.ndarray = None, k=10, d=1.0): # search by filename/metadata/prompt-embed/image-embed - def dct(record: pd.DataFrame, mode: str, distance: float = None): + def search(self, filename: str | None = None, metadata: str | None = None, embed: np.ndarray = None, k=10, d=1.0): # search by filename/metadata/prompt-embed/image-embed + def dct(record: pd.DataFrame, mode: str, distance: float | None = None): if distance is not None: return {'type': mode, 'filename': record[1]['filename'], 'metadata': record[1]['metadata'], 'distance': round(distance, 2)} else: diff --git a/cli/process.py b/cli/process.py index 3461e8f04..dcb4278d9 100644 --- a/cli/process.py +++ b/cli/process.py @@ -21,7 +21,7 @@ all_images_by_type = {} class Result(): - def __init__(self, typ: str, fn: str, tag: str = None, requested: list = []): + def __init__(self, typ: str, fn: str, tag: str | None = None, requested: list = []): self.type = typ self.input = fn self.output = '' @@ -144,7 +144,7 @@ def upscale_restore_image(res: Result, upscale: bool = False): return res -def caption_image(res: Result, tag: str = None): +def caption_image(res: Result, tag: str | None = None): caption = '' tags = [] for model in options.process.caption_model: diff --git a/cli/sdapi.py b/cli/sdapi.py index a91910b1b..6c01d7de3 100755 --- a/cli/sdapi.py +++ b/cli/sdapi.py @@ -93,7 +93,7 @@ def resultsync(req: requests.Response): return res -async def get(endpoint: str, json: dict = None): +async def get(endpoint: str, json: dict | None = None): global sess # pylint: disable=global-statement sess = sess if sess is not None else await session() try: @@ -105,7 +105,7 @@ async def get(endpoint: str, json: dict = None): return {} -def getsync(endpoint: str, json: dict = None): +def getsync(endpoint: str, json: dict | None = None): try: req = requests.get(f'{sd_url}{endpoint}', json=json, verify=False, auth=authsync()) # pylint: disable=missing-timeout res = resultsync(req) @@ -115,7 +115,7 @@ def getsync(endpoint: str, json: dict = None): return {} -async def post(endpoint: str, json: dict = None): +async def post(endpoint: str, json: dict | None = None): global sess # pylint: disable=global-statement # sess = sess if sess is not None else await session() if sess and not sess.closed: @@ -130,7 +130,7 @@ async def post(endpoint: str, json: dict = None): return {} -def postsync(endpoint: str, json: dict = None): +def postsync(endpoint: str, json: dict | None = None): req = requests.post(f'{sd_url}{endpoint}', json=json, verify=False, auth=authsync()) # pylint: disable=missing-timeout res = resultsync(req) return res From d474f28cb372e883d3da38e769add4c20aed5695 Mon Sep 17 00:00:00 2001 From: awsr <43862868+awsr@users.noreply.github.com> Date: Mon, 23 Mar 2026 22:53:34 -0700 Subject: [PATCH 03/25] RUF013 update --- modules/api/docs.py | 8 ++++---- modules/api/endpoints.py | 2 +- modules/api/models.py | 12 ++++++------ 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/modules/api/docs.py b/modules/api/docs.py index 1eac8226e..2d5ff6e4a 100644 --- a/modules/api/docs.py +++ b/modules/api/docs.py @@ -10,11 +10,11 @@ def get_swagger_ui_html(*, title: str, swagger_js_url: str = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui-bundle.js", swagger_css_url: str = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui.css", - swagger_extra_css_url: str = None, + swagger_extra_css_url: str | None = None, swagger_favicon_url: str = "https://fastapi.tiangolo.com/img/favicon.png", - oauth2_redirect_url: str = None, - init_oauth: dict = None, - swagger_ui_parameters: dict = None, + oauth2_redirect_url: str | None = None, + init_oauth: dict | None = None, + swagger_ui_parameters: dict | None = None, ) -> HTMLResponse: current_swagger_ui_parameters = swagger_ui_default_parameters.copy() if swagger_ui_parameters: diff --git a/modules/api/endpoints.py b/modules/api/endpoints.py index 775fa1bc9..f950db601 100644 --- a/modules/api/endpoints.py +++ b/modules/api/endpoints.py @@ -242,7 +242,7 @@ def get_checkpoint(): checkpoint['hash'] = shared.sd_model.sd_checkpoint_info.shorthash return checkpoint -def set_checkpoint(sd_model_checkpoint: str, dtype:str=None, force:bool=False): +def set_checkpoint(sd_model_checkpoint: str, dtype: str | None = None, force: bool = False): """Load a checkpoint by name. Optionally set dtype and force a clean reload.""" from modules import sd_models, devices if force: diff --git a/modules/api/models.py b/modules/api/models.py index f51bd3ad9..f32152027 100644 --- a/modules/api/models.py +++ b/modules/api/models.py @@ -57,10 +57,10 @@ def underscore(name: str) -> str: # Convert CamelCase or PascalCase string to un class PydanticModelGenerator: def __init__( self, - model_name: str = None, - class_instance = None, - additional_fields = None, - exclude_fields: list = None, + model_name: str, + class_instance, + additional_fields: list, + exclude_fields: list | None = None, ): if exclude_fields is None: exclude_fields = [] @@ -100,7 +100,7 @@ class PydanticModelGenerator: self._model_def = [x for x in self._model_def if x.field != fld] def generate_model(self): - model_fields = { d.field: (d.field_type, Field(default=d.field_value, alias=d.field_alias, exclude=d.field_exclude)) for d in self._model_def } + model_fields: dict[str, Any] = { d.field: (d.field_type, Field(default=d.field_value, alias=d.field_alias, exclude=d.field_exclude)) for d in self._model_def } if PYDANTIC_V2: config = ConfigDict(arbitrary_types_allowed=True, from_attributes=True, populate_by_name=True) else: @@ -507,7 +507,7 @@ class ItemLoadedModel(BaseModel): # helper function -def create_model_from_signature(func: Callable, model_name: str, base_model: type[BaseModel] = BaseModel, additional_fields: list = None, exclude_fields: list[str] = None) -> type[BaseModel]: +def create_model_from_signature(func: Callable, model_name: str, base_model: type[BaseModel] = BaseModel, additional_fields: list | None = None, exclude_fields: list[str] | None = None) -> type[BaseModel]: from PIL import Image if exclude_fields is None: From de86927c1b5b734d3aa5abd00039780c1c440de5 Mon Sep 17 00:00:00 2001 From: awsr <43862868+awsr@users.noreply.github.com> Date: Mon, 23 Mar 2026 22:54:40 -0700 Subject: [PATCH 04/25] Remove unneeded type and default --- modules/attention.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/attention.py b/modules/attention.py index 8224f59df..24c30582d 100644 --- a/modules/attention.py +++ b/modules/attention.py @@ -186,11 +186,11 @@ def set_sage_attention(backend: str, device: torch.device): log.error(f'Torch attention: type="Sage attention" {err}') -def set_diffusers_attention(pipe, quiet:bool=False): +def set_diffusers_attention(pipe, quiet = False): from modules import shared import diffusers.models.attention_processor as p - def set_attn(pipe, attention, name:str=None): + def set_attn(pipe, attention, name: str): if attention is None: return # other models uses their own attention processor From 3e228afa7827acebd9e6251a4f7ae06028d9c8af Mon Sep 17 00:00:00 2001 From: awsr <43862868+awsr@users.noreply.github.com> Date: Mon, 23 Mar 2026 22:57:35 -0700 Subject: [PATCH 05/25] RUF013 update --- modules/caption/deepbooru.py | 16 +++++----- modules/caption/joycaption.py | 8 ++--- modules/caption/moondream3.py | 10 +++---- modules/caption/tagger.py | 2 +- modules/caption/vqa.py | 49 +++++++++++++++++++------------ modules/caption/vqa_detection.py | 6 ++-- modules/caption/waifudiffusion.py | 24 +++++++-------- 7 files changed, 63 insertions(+), 52 deletions(-) diff --git a/modules/caption/deepbooru.py b/modules/caption/deepbooru.py index 6ccb848b1..878b3852a 100644 --- a/modules/caption/deepbooru.py +++ b/modules/caption/deepbooru.py @@ -53,13 +53,13 @@ class DeepDanbooru: def tag_multi( self, pil_image, - general_threshold: float = None, - include_rating: bool = None, - exclude_tags: str = None, - max_tags: int = None, - sort_alpha: bool = None, - use_spaces: bool = None, - escape_brackets: bool = None, + general_threshold: float | None = None, + include_rating: bool | None = None, + exclude_tags: str | None = None, + max_tags: int | None = None, + sort_alpha: bool | None = None, + use_spaces: bool | None = None, + escape_brackets: bool | None = None, ): """Run inference and return formatted tag string. @@ -134,7 +134,7 @@ def get_models() -> list: return ["DeepBooru"] -def load_model(model_name: str = None) -> bool: # pylint: disable=unused-argument +def load_model(model_name: str = "") -> bool: # pylint: disable=unused-argument """Load the DeepBooru model.""" try: model.load() diff --git a/modules/caption/joycaption.py b/modules/caption/joycaption.py index dbee83eba..a18b46800 100644 --- a/modules/caption/joycaption.py +++ b/modules/caption/joycaption.py @@ -53,12 +53,12 @@ class JoyOptions: return f'repo="{self.repo}" temp={self.temp} top_k={self.top_k} top_p={self.top_p} sample={self.sample} tokens={self.max_new_tokens}' -processor: AutoProcessor = None -llava_model: LlavaForConditionalGeneration = None +processor: AutoProcessor | None = None +llava_model: LlavaForConditionalGeneration | None = None opts = JoyOptions() -def load(repo: str = None): +def load(repo: str | None = None): """Load JoyCaption model.""" global llava_model, processor # pylint: disable=global-statement repo = repo or opts.repo @@ -93,7 +93,7 @@ def unload(): log.debug('JoyCaption unload: no model loaded') -def predict(question: str, image, vqa_model: str = None) -> str: +def predict(question: str, image, vqa_model: str | None = None) -> str: opts.max_new_tokens = shared.opts.caption_vlm_max_length load(vqa_model) diff --git a/modules/caption/moondream3.py b/modules/caption/moondream3.py index 20418134c..7859f0f06 100644 --- a/modules/caption/moondream3.py +++ b/modules/caption/moondream3.py @@ -87,7 +87,7 @@ def _image_hash(image: Image.Image) -> str: return h.hexdigest() -def encode_image(image: Image.Image, cache_key: str = None): +def encode_image(image: Image.Image, cache_key: str | None = None): """ Encode image for reuse across multiple queries. @@ -119,7 +119,7 @@ def encode_image(image: Image.Image, cache_key: str = None): def query(image: Image.Image, question: str, repo: str, stream: bool = False, - temperature: float = None, top_p: float = None, max_tokens: int = None, + temperature: float | None = None, top_p: float | None = None, max_tokens: int | None = None, use_cache: bool = False, reasoning: bool = True): """ Visual question answering with optional streaming. @@ -180,7 +180,7 @@ def query(image: Image.Image, question: str, repo: str, stream: bool = False, def caption(image: Image.Image, repo: str, length: str = 'normal', stream: bool = False, - temperature: float = None, top_p: float = None, max_tokens: int = None): + temperature: float | None = None, top_p: float | None = None, max_tokens: int | None = None): """ Generate image captions at different lengths. @@ -290,8 +290,8 @@ def detect(image: Image.Image, object_name: str, repo: str, max_objects: int = 1 return detections -def predict(question: str, image: Image.Image, repo: str, model_name: str = None, thinking_mode: bool = False, - mode: str = None, stream: bool = False, use_cache: bool = False, **kwargs): +def predict(question: str, image: Image.Image, repo: str, model_name: str | None = None, thinking_mode: bool = False, + mode: str | None = None, stream: bool = False, use_cache: bool = False, **kwargs): """ Main entry point for Moondream 3 VQA - auto-detects mode from question. diff --git a/modules/caption/tagger.py b/modules/caption/tagger.py index e73e7df4d..6f2436b38 100644 --- a/modules/caption/tagger.py +++ b/modules/caption/tagger.py @@ -65,7 +65,7 @@ def unload_model(): waifudiffusion.unload_model() -def tag(image, model_name: str = None, **kwargs) -> str: +def tag(image, model_name: str | None = None, **kwargs) -> str: """Unified tagging - dispatch to correct backend. Args: diff --git a/modules/caption/vqa.py b/modules/caption/vqa.py index a10818624..807479144 100644 --- a/modules/caption/vqa.py +++ b/modules/caption/vqa.py @@ -50,7 +50,7 @@ def get_prompts_for_model(model_name: str) -> list: return vlm_prompts_common -def get_internal_prompt(friendly_name: str, user_prompt: str = None) -> str: +def get_internal_prompt(friendly_name: str, user_prompt: str | None = None) -> str: """Convert friendly prompt name to internal token/command.""" internal = vlm_prompt_mapping.get(friendly_name, friendly_name) @@ -350,7 +350,7 @@ class VQA: self.processor = None devices.torch_gc(force=True, reason='vqa model switch') - def load(self, model_name: str = None): + def load(self, model_name: str | None = None): """Load VLM model into memory for the specified model name.""" model_name = model_name or shared.opts.caption_vlm_model if not model_name: @@ -444,7 +444,7 @@ class VQA: self.loaded = repo devices.torch_gc() - def _fastvlm(self, question: str, image: Image.Image, repo: str, model_name: str = None): + def _fastvlm(self, question: str, image: Image.Image, repo: str, model_name: str | None = None): debug(f'VQA caption: handler=fastvlm model_name="{model_name}" repo="{repo}" question="{question}" image_size={image.size if image else None}') self._load_fastvlm(repo) move_aux_to_gpu('vqa') @@ -521,7 +521,7 @@ class VQA: self.loaded = repo devices.torch_gc() - def _qwen(self, question: str, image: Image.Image, repo: str, system_prompt: str = None, model_name: str = None, prefill: str = None, thinking_mode: bool = False): + def _qwen(self, question: str, image: Image.Image, repo: str, system_prompt: str | None = None, model_name: str | None = None, prefill: str | None = None, thinking_mode: bool = False): self._load_qwen(repo) move_aux_to_gpu('vqa') # Get model class name for logging @@ -644,7 +644,7 @@ class VQA: self.loaded = repo devices.torch_gc() - def _gemma(self, question: str, image: Image.Image, repo: str, system_prompt: str = None, model_name: str = None, prefill: str = None, thinking_mode: bool = False): + def _gemma(self, question: str, image: Image.Image, repo: str, system_prompt: str | None = None, model_name: str | None = None, prefill: str | None = None, thinking_mode: bool = False): self._load_gemma(repo) move_aux_to_gpu('vqa') # Get model class name for logging @@ -757,7 +757,7 @@ class VQA: self.loaded = repo devices.torch_gc() - def _mistral(self, question: str, image: Image.Image, repo: str, system_prompt: str = None, model_name: str = None, prefill: str = None, thinking_mode: bool = False): + def _mistral(self, question: str, image: Image.Image, repo: str, system_prompt: str | None = None, model_name: str | None = None, prefill: str | None = None, thinking_mode: bool = False): self._load_mistral(repo) move_aux_to_gpu('vqa') cls_name = self.model.__class__.__name__ @@ -828,7 +828,7 @@ class VQA: self.loaded = repo devices.torch_gc() - def _paligemma(self, question: str, image: Image.Image, repo: str, model_name: str = None): # pylint: disable=unused-argument + def _paligemma(self, question: str, image: Image.Image, repo: str, model_name: str | None = None): # pylint: disable=unused-argument self._load_paligemma(repo) move_aux_to_gpu('vqa') question = question.replace('<', '').replace('>', '').replace('_', ' ') @@ -870,7 +870,7 @@ class VQA: self.loaded = repo devices.torch_gc() - def _ovis(self, question: str, image: Image.Image, repo: str, model_name: str = None): # pylint: disable=unused-argument + def _ovis(self, question: str, image: Image.Image, repo: str, model_name: str | None = None): # pylint: disable=unused-argument try: pass # pylint: disable=unused-import except Exception: @@ -925,7 +925,7 @@ class VQA: self.loaded = repo devices.torch_gc() - def _smol(self, question: str, image: Image.Image, repo: str, system_prompt: str = None, model_name: str = None, prefill: str = None, thinking_mode: bool = False): + def _smol(self, question: str, image: Image.Image, repo: str, system_prompt: str | None = None, model_name: str | None = None, prefill: str | None = None, thinking_mode: bool = False): self._load_smol(repo) move_aux_to_gpu('vqa') # Get model class name for logging @@ -1019,7 +1019,7 @@ class VQA: self.loaded = repo devices.torch_gc() - def _git(self, question: str, image: Image.Image, repo: str, model_name: str = None): # pylint: disable=unused-argument + def _git(self, question: str, image: Image.Image, repo: str, model_name: str | None = None): # pylint: disable=unused-argument self._load_git(repo) move_aux_to_gpu('vqa') pixel_values = self.processor(images=image, return_tensors="pt").pixel_values @@ -1053,7 +1053,7 @@ class VQA: self.loaded = repo devices.torch_gc() - def _blip(self, question: str, image: Image.Image, repo: str, model_name: str = None): # pylint: disable=unused-argument + def _blip(self, question: str, image: Image.Image, repo: str, model_name: str | None = None): # pylint: disable=unused-argument self._load_blip(repo) move_aux_to_gpu('vqa') inputs = self.processor(image, question, return_tensors="pt") @@ -1081,7 +1081,7 @@ class VQA: self.loaded = repo devices.torch_gc() - def _vilt(self, question: str, image: Image.Image, repo: str, model_name: str = None): # pylint: disable=unused-argument + def _vilt(self, question: str, image: Image.Image, repo: str, model_name: str | None = None): # pylint: disable=unused-argument self._load_vilt(repo) move_aux_to_gpu('vqa') inputs = self.processor(image, question, return_tensors="pt") @@ -1111,7 +1111,7 @@ class VQA: self.loaded = repo devices.torch_gc() - def _pix(self, question: str, image: Image.Image, repo: str, model_name: str = None): # pylint: disable=unused-argument + def _pix(self, question: str, image: Image.Image, repo: str, model_name: str | None = None): # pylint: disable=unused-argument self._load_pix(repo) move_aux_to_gpu('vqa') if len(question) > 0: @@ -1144,7 +1144,7 @@ class VQA: register_aux('vqa', self.model) devices.torch_gc() - def _moondream(self, question: str, image: Image.Image, repo: str, model_name: str = None, thinking_mode: bool = False): + def _moondream(self, question: str, image: Image.Image, repo: str, model_name: str | None = None, thinking_mode: bool = False): debug(f'VQA caption: handler=moondream model_name="{model_name}" repo="{repo}" question="{question}" thinking_mode={thinking_mode}') self._load_moondream(repo) move_aux_to_gpu('vqa') @@ -1207,7 +1207,7 @@ class VQA: # When keep_thinking is False, just use the answer (reasoning is discarded) return response - def _load_florence(self, repo: str, revision: str = None): + def _load_florence(self, repo: str, revision: str | None = None): """Load Florence-2 model and processor.""" _get_imports = transformers.dynamic_module_utils.get_imports @@ -1247,7 +1247,7 @@ class VQA: self.loaded = cache_key devices.torch_gc() - def _florence(self, question: str, image: Image.Image, repo: str, revision: str = None, model_name: str = None): # pylint: disable=unused-argument + def _florence(self, question: str, image: Image.Image, repo: str, revision: str | None = None, model_name: str | None = None): # pylint: disable=unused-argument self._load_florence(repo, revision) move_aux_to_gpu('vqa') if question.startswith('<'): @@ -1306,7 +1306,7 @@ class VQA: self.loaded = repo devices.torch_gc() - def _sa2(self, question: str, image: Image.Image, repo: str, model_name: str = None): # pylint: disable=unused-argument + def _sa2(self, question: str, image: Image.Image, repo: str, model_name: str | None = None): # pylint: disable=unused-argument self._load_sa2(repo) move_aux_to_gpu('vqa') if question.startswith('<'): @@ -1325,7 +1325,18 @@ class VQA: response = return_dict["prediction"] # the text format answer return response - def caption(self, question: str = '', system_prompt: str = None, prompt: str = None, image: Image.Image = None, model_name: str = None, prefill: str = None, thinking_mode: bool = None, quiet: bool = False, generation_kwargs: dict = None) -> str: + def caption( + self, + question: str = "", + system_prompt: str | None = None, + prompt: str | None = None, + image: list[Image.Image] | Image.Image | dict | None = None, + model_name: str | None = None, + prefill: str | None = None, + thinking_mode: bool | None = None, + quiet: bool = False, + generation_kwargs: dict | None = None, + ) -> str: """ Main entry point for VQA captioning. Returns string answer. Detection data stored in self.last_detection_data for annotated image creation. @@ -1596,7 +1607,7 @@ def unload_model(): return get_instance().unload() -def load_model(model_name: str = None): +def load_model(model_name: str | None = None): return get_instance().load(model_name) diff --git a/modules/caption/vqa_detection.py b/modules/caption/vqa_detection.py index d0aadf970..683067250 100644 --- a/modules/caption/vqa_detection.py +++ b/modules/caption/vqa_detection.py @@ -38,7 +38,7 @@ def parse_points(result) -> list: return points -def parse_detections(result, label: str, max_objects: int = None) -> list: +def parse_detections(result, label: str, max_objects: int | None = None) -> list: """Parse and validate detection bboxes from model result. Args: @@ -74,7 +74,7 @@ def parse_detections(result, label: str, max_objects: int = None) -> list: return detections -def parse_florence_detections(response, image_size: tuple = None) -> list: +def parse_florence_detections(response, image_size: tuple | None = None) -> list: """Parse Florence-style detection response into standard detection format. Florence returns detection data in two possible formats: @@ -286,7 +286,7 @@ def calculate_eye_position(face_bbox: dict) -> tuple: return (eye_x, eye_y) -def draw_bounding_boxes(image: Image.Image, detections: list, points: list = None) -> Image.Image: +def draw_bounding_boxes(image: Image.Image, detections: list, points: list | None = None) -> Image.Image: """ Draw bounding boxes and/or points on an image. diff --git a/modules/caption/waifudiffusion.py b/modules/caption/waifudiffusion.py index 3e284db75..4fe455553 100644 --- a/modules/caption/waifudiffusion.py +++ b/modules/caption/waifudiffusion.py @@ -50,7 +50,7 @@ class WaifuDiffusionTagger: self.model_path = None self.image_size = 448 # Standard for WD models - def load(self, model_name: str = None): + def load(self, model_name: str | None = None): """Load the ONNX model and tags from HuggingFace.""" import huggingface_hub @@ -195,15 +195,15 @@ class WaifuDiffusionTagger: def predict( self, - image: Image.Image, - general_threshold: float = None, - character_threshold: float = None, - include_rating: bool = None, - exclude_tags: str = None, - max_tags: int = None, - sort_alpha: bool = None, - use_spaces: bool = None, - escape_brackets: bool = None, + image: Image.Image | list[Image.Image] | dict | None, + general_threshold: float | None = None, + character_threshold: float | None = None, + include_rating: bool | None = None, + exclude_tags: str | None = None, + max_tags: int | None = None, + sort_alpha: bool | None = None, + use_spaces: bool | None = None, + escape_brackets: bool | None = None, ) -> str: """Run inference and return formatted tag string. @@ -352,7 +352,7 @@ def refresh_models() -> list: return get_models() -def load_model(model_name: str = None) -> bool: +def load_model(model_name: str | None = None) -> bool: """Load the specified WaifuDiffusion model.""" return tagger.load(model_name) @@ -362,7 +362,7 @@ def unload_model(): tagger.unload() -def tag(image: Image.Image, model_name: str = None, **kwargs) -> str: +def tag(image: Image.Image, model_name: str | None = None, **kwargs) -> str: """Tag an image using WaifuDiffusion tagger. Args: From 53598d40ab52c1e31aa043aa5abf0007337d02e1 Mon Sep 17 00:00:00 2001 From: awsr <43862868+awsr@users.noreply.github.com> Date: Mon, 23 Mar 2026 22:59:32 -0700 Subject: [PATCH 06/25] RUF013 update --- modules/civitai/api_civitai.py | 28 ++++++++++++++-------------- modules/civitai/download_civitai.py | 2 +- modules/civitai/metadata_civitai.py | 2 +- modules/civitai/search_civitai.py | 4 ++-- modules/civitai/userdata_civitai.py | 2 +- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/modules/civitai/api_civitai.py b/modules/civitai/api_civitai.py index de07186fd..37ba28bf0 100644 --- a/modules/civitai/api_civitai.py +++ b/modules/civitai/api_civitai.py @@ -87,12 +87,12 @@ def get_search( sort: str = '', period: str = '', base_models: str = '', - nsfw: bool = None, + nsfw: bool | None = None, limit: int = 20, - cursor: str = None, + cursor: str | None = None, username: str = '', favorites: bool = False, - token: str = None, + token: str | None = None, ): """Search CivitAI models with pagination.""" from modules.civitai.client_civitai import client @@ -110,7 +110,7 @@ def get_search( return response.dict(by_alias=True) -def get_model(model_id: int, token: str = None): +def get_model(model_id: int, token: str | None = None): """Get a single model by ID (fresh fetch).""" from modules.civitai.client_civitai import client model = client.get_model(model_id, token=token) @@ -119,7 +119,7 @@ def get_model(model_id: int, token: str = None): return model_to_dict(model) -def get_version(version_id: int, token: str = None): +def get_version(version_id: int, token: str | None = None): """Get a single version by ID.""" from modules.civitai.client_civitai import client version = client.get_version(version_id, token=token) @@ -128,7 +128,7 @@ def get_version(version_id: int, token: str = None): return version_to_dict(version) -def get_version_by_hash(hash_str: str, token: str = None): +def get_version_by_hash(hash_str: str, token: str | None = None): """Look up a version by file hash.""" from modules.civitai.client_civitai import client version = client.get_version_by_hash(hash_str, token=token) @@ -155,13 +155,13 @@ def get_creators(query: str = '', limit: int = 20, page: int = 1): return client.get_creators(query=query, limit=limit, page=page).dict(by_alias=True) -def get_images(model_id: int = None, model_version_id: int = None, limit: int = 20): +def get_images(model_id: int | None = None, model_version_id: int | None = None, limit: int = 20): """Get images with generation metadata from CivitAI.""" from modules.civitai.client_civitai import client return {"items": client.get_images_raw(model_id=model_id, model_version_id=model_version_id, limit=limit)} -def get_me(token: str = None): +def get_me(token: str | None = None): """Get authenticated CivitAI user profile.""" from modules.civitai.client_civitai import client profile = client.get_me(token=token) @@ -300,7 +300,7 @@ def get_resolve_path( # Metadata # --------------------------------------------------------------------------- -def post_metadata_scan(request: dict = None): +def post_metadata_scan(request: dict | None = None): """Scan local models for CivitAI metadata. Optional ``page`` filters by network type (e.g. 'lora', 'model').""" from modules.civitai import metadata_civitai page = (request or {}).get('page', None) @@ -388,7 +388,7 @@ def delete_banned(name: str): # User Data — Search History # --------------------------------------------------------------------------- -def get_history(search_type: str = None): +def get_history(search_type: str | None = None): from modules.civitai.userdata_civitai import search_history return {"history": search_history.list(search_type)} @@ -529,16 +529,16 @@ def post_check_local(request: dict): # --------------------------------------------------------------------------- def legacy_get_civitai( - model_id: int = None, + model_id: int | None = None, query: str = '', tag: str = '', types: str = '', sort: str = '', period: str = '', - nsfw: bool = None, + nsfw: bool | None = None, limit: int = 0, base: str = '', - token: str = None, + token: str | None = None, exact: bool = True, ): """Legacy GET /sdapi/v1/civitai — delegates to search or model lookup.""" @@ -559,7 +559,7 @@ def legacy_get_civitai( return JSONResponse(content=[], status_code=200) -def legacy_post_civitai(page: str = None): +def legacy_post_civitai(page: str | None = None): """Legacy POST /sdapi/v1/civitai — scan metadata.""" from modules.civitai import metadata_civitai result = [] diff --git a/modules/civitai/download_civitai.py b/modules/civitai/download_civitai.py index da5378b41..402f2ac28 100644 --- a/modules/civitai/download_civitai.py +++ b/modules/civitai/download_civitai.py @@ -406,7 +406,7 @@ def download_civit_preview(model_path: str, preview_url: str): return 200, str(total_size), '' -def download_civit_model(model_url: str, model_name: str = '', model_path: str = '', model_type: str = '', token: str = None, +def download_civit_model(model_url: str, model_name: str = '', model_path: str = '', model_type: str = '', token: str | None = None, base_model: str = '', model_id: int = 0, version_id: int = 0): """Legacy function — delegates to DownloadManager for non-blocking downloads.""" if not model_url: diff --git a/modules/civitai/metadata_civitai.py b/modules/civitai/metadata_civitai.py index 57c4e082e..c5c7b89e6 100644 --- a/modules/civitai/metadata_civitai.py +++ b/modules/civitai/metadata_civitai.py @@ -169,7 +169,7 @@ def atomic_civit_search_metadata(item, results): results.append(dict(result)) -def civit_search_metadata(title: str = None, raw: bool = False): +def civit_search_metadata(title: str | None = None, raw: bool = False): def create_search_metadata_table(rows): html = """ diff --git a/modules/civitai/search_civitai.py b/modules/civitai/search_civitai.py index e3b116dc2..947bcbc51 100644 --- a/modules/civitai/search_civitai.py +++ b/modules/civitai/search_civitai.py @@ -15,10 +15,10 @@ def search_civitai( types: str = '', sort: str = '', period: str = '', - nsfw: bool = None, + nsfw: bool | None = None, limit: int = 0, base: str = '', - token: str = None, + token: str | None = None, exact: bool = True, ) -> list[CivitModel]: if not query and not tag and not sort: diff --git a/modules/civitai/userdata_civitai.py b/modules/civitai/userdata_civitai.py index d990699a2..64c7efae5 100644 --- a/modules/civitai/userdata_civitai.py +++ b/modules/civitai/userdata_civitai.py @@ -111,7 +111,7 @@ class SearchHistory: self._entries = self._entries[:self._max_entries] self._save() - def list(self, search_type: str = None) -> list[dict]: + def list(self, search_type: str | None = None) -> list[dict]: with self._lock: if search_type: return [e for e in self._entries if e.get('type') == search_type] From b75a1f971fa0716b40fe0adc371ee98fd7e79be4 Mon Sep 17 00:00:00 2001 From: awsr <43862868+awsr@users.noreply.github.com> Date: Tue, 24 Mar 2026 03:39:02 -0700 Subject: [PATCH 07/25] RUF013 update + minor typing fixes --- modules/control/processor.py | 26 +++++----- modules/control/processors.py | 13 ++--- modules/control/run.py | 92 +++++++++++++++++------------------ modules/control/unit.py | 31 +++++++----- 4 files changed, 86 insertions(+), 76 deletions(-) diff --git a/modules/control/processor.py b/modules/control/processor.py index ee9189b97..56fa66f66 100644 --- a/modules/control/processor.py +++ b/modules/control/processor.py @@ -61,19 +61,19 @@ processors = [ def preprocess_image( - p:StableDiffusionProcessingControl, - pipe, - input_image:Image.Image = None, - init_image:Image.Image = None, - input_mask:Image.Image = None, - input_type:str = 0, - unit_type:str = 'controlnet', - active_process:list = None, - active_model:list = None, - selected_models:list = None, - has_models:bool = False, - active_units:list = None, - ): + p: StableDiffusionProcessingControl, + pipe, + input_image: Image.Image | None= None, + init_image: Image.Image | None = None, + input_mask: Image.Image | None = None, + input_type = 0, + unit_type = "controlnet", + active_process: list | None = None, + active_model: list | None = None, + selected_models: list | None = None, + has_models = False, + active_units: list | None = None, +): if selected_models is None: selected_models = [] if active_model is None: diff --git a/modules/control/processors.py b/modules/control/processors.py index 6be1f570b..accdc1f67 100644 --- a/modules/control/processors.py +++ b/modules/control/processors.py @@ -195,20 +195,21 @@ def update_settings(*settings): class Processor: - def __init__(self, processor_id: str = None, resize = True): + def __init__(self, processor_id: str | None = None, resize = True): self.model = None - self.processor_id = None - self.override = None + self.processor_id: str | None = None + self.override: Image.Image | None = None self.resize = resize self.reset() self.config(processor_id) + self.load_config: dict = {} if processor_id is not None: self.load() def __str__(self): return f' Processor(id={self.processor_id} model={self.model.__class__.__name__})' if self.processor_id and self.model else '' - def reset(self, processor_id: str = None): + def reset(self, processor_id: str | None = None): if self.model is not None: debug(f'Control Processor unloaded: id="{self.processor_id}"') self.model = None @@ -237,7 +238,7 @@ class Processor: for k, v in from_config.items(): self.load_config[k] = v - def load(self, processor_id: str = None, force: bool = True) -> str: + def load(self, processor_id: str | None = None, force: bool = True) -> str: from modules.shared import state try: t0 = time.time() @@ -304,7 +305,7 @@ class Processor: display(e, 'Control Processor load') return f'Processor load filed: {processor_id}' - def __call__(self, image_input: Image, mode: str = 'RGB', width: int = 0, height: int = 0, resize_mode: int = 0, resize_name: str = 'None', scale_tab: int = 1, scale_by: float = 1.0, local_config: dict = None): + def __call__(self, image_input: Image, mode: str = 'RGB', width: int = 0, height: int = 0, resize_mode: int = 0, resize_name: str = 'None', scale_tab: int = 1, scale_by: float = 1.0, local_config: dict | None = None): """Run the preprocessor on an input image and return the processed control map. Args: diff --git a/modules/control/run.py b/modules/control/run.py index 007833374..94bcc73b8 100644 --- a/modules/control/run.py +++ b/modules/control/run.py @@ -140,7 +140,7 @@ def set_pipe(p, has_models, unit_type, selected_models, active_model, active_str return pipe -def check_active(p, unit_type, units): +def check_active(p, unit_type: str, units: list[unit.Unit]): active_process: list[processors.Processor] = [] # all active preprocessors active_model: list[controlnet.ControlNet | xs.ControlNetXS | t2iadapter.Adapter] = [] # all active models active_strength: list[float] = [] # strength factors for all active models @@ -216,9 +216,9 @@ def check_active(p, unit_type, units): return active_process, active_model, active_strength, active_start, active_end, active_units -def check_enabled(p, unit_type, units, active_model, active_strength, active_start, active_end): +def check_enabled(p, unit_type: str, units: list[unit.Unit], active_model: list[controlnet.ControlNet | xs.ControlNetXS | t2iadapter.Adapter], active_strength: list[float], active_start: list[float], active_end: list[float]): has_models = False - selected_models: list[controlnet.ControlNetModel | xs.ControlNetXSModel | t2iadapter.AdapterModel] = None + selected_models: list[controlnet.ControlNetModel | xs.ControlNetXSModel | t2iadapter.AdapterModel] | None = None control_conditioning = None control_guidance_start = None control_guidance_end = None @@ -271,17 +271,17 @@ def init_units(units: list[unit.Unit]): def control_run(state: str = '', # pylint: disable=keyword-arg-before-vararg - units: list[unit.Unit] = None, inputs: list[Image.Image] = None, inits: list[Image.Image] = None, mask: Image.Image = None, unit_type: str = None, is_generator: bool = True, + units: list[unit.Unit] | None = None, inputs: list[Image.Image] | None = None, inits: list[Image.Image] | None = None, mask: Image.Image = None, unit_type: str | None = None, is_generator: bool = True, input_type: int = 0, - prompt: str = '', negative_prompt: str = '', styles: list[str] = None, - steps: int = 20, sampler_index: int = None, + prompt: str = '', negative_prompt: str = '', styles: list[str] | None = None, + steps: int = 20, sampler_index: int | None = None, seed: int = -1, subseed: int = -1, subseed_strength: float = 0, seed_resize_from_h: int = -1, seed_resize_from_w: int = -1, guidance_name: str = 'Default', guidance_scale: float = 6.0, guidance_rescale: float = 0.0, guidance_start: float = 0.0, guidance_stop: float = 1.0, cfg_scale: float = 6.0, clip_skip: float = 1.0, image_cfg_scale: float = 6.0, diffusers_guidance_rescale: float = 0.7, pag_scale: float = 0.0, pag_adaptive: float = 0.5, cfg_end: float = 1.0, vae_type: str = 'Full', tiling: bool = False, hidiffusion: bool = False, detailer_enabled: bool = False, detailer_prompt: str = '', detailer_negative: str = '', detailer_steps: int = 10, detailer_strength: float = 0.3, detailer_resolution: int = 1024, hdr_mode: int = 0, hdr_brightness: float = 0, hdr_color: float = 0, hdr_sharpen: float = 0, hdr_clamp: bool = False, hdr_boundary: float = 4.0, hdr_threshold: float = 0.95, - hdr_maximize: bool = False, hdr_max_center: float = 0.6, hdr_max_boundary: float = 1.0, hdr_color_picker: str = None, hdr_tint_ratio: float = 0, hdr_apply_hires: bool = True, + hdr_maximize: bool = False, hdr_max_center: float = 0.6, hdr_max_boundary: float = 1.0, hdr_color_picker: str | None = None, hdr_tint_ratio: float = 0, hdr_apply_hires: bool = True, grading_brightness: float = 0.0, grading_contrast: float = 0.0, grading_saturation: float = 0.0, grading_hue: float = 0.0, grading_gamma: float = 1.0, grading_sharpness: float = 0.0, grading_color_temp: float = 6500, grading_shadows: float = 0.0, grading_midtones: float = 0.0, grading_highlights: float = 0.0, @@ -293,54 +293,54 @@ def control_run(state: str = '', # pylint: disable=keyword-arg-before-vararg resize_mode_after: int = 0, resize_name_after: str = 'None', resize_context_after: str = 'None', width_after: int = 0, height_after: int = 0, scale_by_after: float = 1.0, selected_scale_tab_after: int = 0, resize_mode_mask: int = 0, resize_name_mask: str = 'None', resize_context_mask: str = 'None', width_mask: int = 0, height_mask: int = 0, scale_by_mask: float = 1.0, selected_scale_tab_mask: int = 0, denoising_strength: float = 0.3, batch_count: int = 1, batch_size: int = 1, - enable_hr: bool = False, hr_sampler_index: int = None, hr_denoising_strength: float = 0.0, hr_resize_mode: int = 0, hr_resize_context: str = 'None', hr_upscaler: str = None, hr_force: bool = False, hr_second_pass_steps: int = 20, + enable_hr: bool = False, hr_sampler_index: int | None = None, hr_denoising_strength: float = 0.0, hr_resize_mode: int = 0, hr_resize_context: str = 'None', hr_upscaler: str | None = None, hr_force: bool = False, hr_second_pass_steps: int = 20, hr_scale: float = 1.0, hr_resize_x: int = 0, hr_resize_y: int = 0, refiner_steps: int = 5, refiner_start: float = 0.0, refiner_prompt: str = '', refiner_negative: str = '', video_skip_frames: int = 0, video_type: str = 'None', video_duration: float = 2.0, video_loop: bool = False, video_pad: int = 0, video_interpolate: int = 0, - override_script_name: str = None, override_script_args = None, extra: dict = None, + override_script_name: str | None = None, override_script_args = None, extra: dict | None = None, *input_script_args, # API-only params (keyword-only, not wired to Gradio) - detailer_segmentation: bool = None, detailer_include_detections: bool = None, detailer_merge: bool = None, detailer_sort: bool = None, detailer_classes: str = None, - detailer_conf: float = None, detailer_iou: float = None, detailer_max: int = None, - detailer_min_size: float = None, detailer_max_size: float = None, - detailer_blur: int = None, detailer_padding: int = None, - detailer_sigma_adjust: float = None, detailer_sigma_adjust_max: float = None, - detailer_models: list = None, detailer_augment: bool = None, - img2img_color_correction: bool = None, color_correction_method: str = None, img2img_background_color: str = None, - img2img_fix_steps: bool = None, mask_apply_overlay: bool = None, - include_mask: bool = None, inpainting_mask_weight: float = None, + detailer_segmentation: bool | None = None, detailer_include_detections: bool | None = None, detailer_merge: bool | None = None, detailer_sort: bool | None = None, detailer_classes: str | None = None, + detailer_conf: float | None = None, detailer_iou: float | None = None, detailer_max: int | None = None, + detailer_min_size: float | None = None, detailer_max_size: float | None = None, + detailer_blur: int | None = None, detailer_padding: int | None = None, + detailer_sigma_adjust: float | None = None, detailer_sigma_adjust_max: float | None = None, + detailer_models: list | None = None, detailer_augment: bool | None = None, + img2img_color_correction: bool | None = None, color_correction_method: str | None = None, img2img_background_color: str | None = None, + img2img_fix_steps: bool | None = None, mask_apply_overlay: bool | None = None, + include_mask: bool | None = None, inpainting_mask_weight: float | None = None, # output and saving - samples_save: bool = None, samples_format: str = None, - save_images_before_highres_fix: bool = None, save_images_before_refiner: bool = None, - save_images_before_detailer: bool = None, save_images_before_color_correction: bool = None, - grid_save: bool = None, grid_format: str = None, return_grid: bool = None, - save_mask: bool = None, save_mask_composite: bool = None, - return_mask: bool = None, return_mask_composite: bool = None, - keep_incomplete: bool = None, image_metadata: bool = None, jpeg_quality: int = None, + samples_save: bool | None = None, samples_format: str | None = None, + save_images_before_highres_fix: bool | None = None, save_images_before_refiner: bool | None = None, + save_images_before_detailer: bool | None = None, save_images_before_color_correction: bool | None = None, + grid_save: bool | None = None, grid_format: str | None = None, return_grid: bool | None = None, + save_mask: bool | None = None, save_mask_composite: bool | None = None, + return_mask: bool | None = None, return_mask_composite: bool | None = None, + keep_incomplete: bool | None = None, image_metadata: bool | None = None, jpeg_quality: int | None = None, # scheduler/noise overrides - schedulers_prediction_type: str = None, schedulers_beta_schedule: str = None, schedulers_timesteps: str = None, - schedulers_sigma: str = None, schedulers_use_thresholding: bool = None, schedulers_use_loworder: bool = None, - schedulers_solver_order: int = None, uni_pc_variant: str = None, schedulers_beta_start: float = None, - schedulers_beta_end: float = None, schedulers_shift: float = None, schedulers_dynamic_shift: bool = None, - schedulers_base_shift: float = None, schedulers_max_shift: float = None, schedulers_rescale_betas: bool = None, - schedulers_timestep_spacing: str = None, schedulers_timesteps_range: int = None, - schedulers_sigma_adjust: float = None, schedulers_sigma_adjust_min: float = None, schedulers_sigma_adjust_max: float = None, - scheduler_eta: float = None, eta_noise_seed_delta: int = None, enable_batch_seeds: bool = None, - diffusers_generator_device: str = None, nan_skip: bool = None, - sequential_seed: bool = None, + schedulers_prediction_type: str | None = None, schedulers_beta_schedule: str | None = None, schedulers_timesteps: str | None = None, + schedulers_sigma: str | None = None, schedulers_use_thresholding: bool | None = None, schedulers_use_loworder: bool | None = None, + schedulers_solver_order: int | None = None, uni_pc_variant: str | None = None, schedulers_beta_start: float | None = None, + schedulers_beta_end: float | None = None, schedulers_shift: float | None = None, schedulers_dynamic_shift: bool | None = None, + schedulers_base_shift: float | None = None, schedulers_max_shift: float | None = None, schedulers_rescale_betas: bool | None = None, + schedulers_timestep_spacing: str | None = None, schedulers_timesteps_range: int | None = None, + schedulers_sigma_adjust: float | None = None, schedulers_sigma_adjust_min: float | None = None, schedulers_sigma_adjust_max: float | None = None, + scheduler_eta: float | None = None, eta_noise_seed_delta: int | None = None, enable_batch_seeds: bool | None = None, + diffusers_generator_device: str | None = None, nan_skip: bool | None = None, + sequential_seed: bool | None = None, # prompt/attention overrides - prompt_attention: str = None, prompt_mean_norm: bool = None, diffusers_zeros_prompt_pad: bool = None, - te_pooled_embeds: bool = None, lora_apply_te: bool = None, te_complex_human_instruction: str = None, te_use_mask: bool = None, + prompt_attention: str | None = None, prompt_mean_norm: bool | None = None, diffusers_zeros_prompt_pad: bool | None = None, + te_pooled_embeds: bool | None = None, lora_apply_te: bool | None = None, te_complex_human_instruction: str | None = None, te_use_mask: bool | None = None, # generation modifier overrides (hijack) - freeu_enabled: bool = None, freeu_b1: float = None, freeu_b2: float = None, freeu_s1: float = None, freeu_s2: float = None, - hypertile_unet_enabled: bool = None, hypertile_hires_only: bool = None, hypertile_unet_tile: int = None, hypertile_unet_min_tile: int = None, - hypertile_unet_swap_size: int = None, hypertile_unet_depth: int = None, - hypertile_vae_enabled: bool = None, hypertile_vae_tile: int = None, hypertile_vae_swap_size: int = None, - teacache_enabled: bool = None, teacache_thresh: float = None, - token_merging_method: str = None, tome_ratio: float = None, todo_ratio: float = None, + freeu_enabled: bool | None = None, freeu_b1: float | None = None, freeu_b2: float | None = None, freeu_s1: float | None = None, freeu_s2: float | None = None, + hypertile_unet_enabled: bool | None = None, hypertile_hires_only: bool | None = None, hypertile_unet_tile: int | None = None, hypertile_unet_min_tile: int | None = None, + hypertile_unet_swap_size: int | None = None, hypertile_unet_depth: int | None = None, + hypertile_vae_enabled: bool | None = None, hypertile_vae_tile: int | None = None, hypertile_vae_swap_size: int | None = None, + teacache_enabled: bool | None = None, teacache_thresh: float | None = None, + token_merging_method: str | None = None, tome_ratio: float | None = None, todo_ratio: float | None = None, # lora behavior - lora_fuse_native: bool = None, lora_fuse_diffusers: bool = None, - lora_force_reload: bool = None, extra_networks_default_multiplier: float = None, - lora_apply_tags: int = None, + lora_fuse_native: bool | None = None, lora_fuse_diffusers: bool | None = None, + lora_force_reload: bool | None = None, extra_networks_default_multiplier: float | None = None, + lora_apply_tags: int | None = None, ): if override_script_args is None: override_script_args = [] diff --git a/modules/control/unit.py b/modules/control/unit.py index fc33eb349..e66aed02b 100644 --- a/modules/control/unit.py +++ b/modules/control/unit.py @@ -1,3 +1,4 @@ +from typing import TYPE_CHECKING from PIL import Image import gradio as gr from modules.logger import log @@ -33,10 +34,10 @@ class Unit: # mashup of gradio controls and mapping to actual implementation cla def __init__(self, # values - index: int = None, - enabled: bool = None, - strength: float = None, - unit_type: str = None, + index: int | None = None, + enabled: bool | None = None, + strength: float | None = None, + unit_type: str | None = None, start: float = 0, end: float = 1, # ui bindings @@ -55,7 +56,7 @@ class Unit: # mashup of gradio controls and mapping to actual implementation cla control_mode = None, control_tile = None, result_txt = None, - extra_controls: list = None, + extra_controls: list | None = None, ): if extra_controls is None: extra_controls = [] @@ -71,15 +72,15 @@ class Unit: # mashup of gradio controls and mapping to actual implementation cla self.end = end or 1 self.start = min(self.start, self.end) self.end = max(self.start, self.end) - self.mode = None + self.mode: int | None = None # processor always exists, adapter and controlnet are optional - self.model_name = None - self.process_name = None + self.model_name: str | None = None + self.process_name: str | None = None self.process: processors.Processor = processors.Processor() - self.adapter: t2iadapter.Adapter = None - self.controlnet: controlnet.ControlNet | xs.ControlNetXS = None + self.adapter: t2iadapter.Adapter | None = None + self.controlnet: controlnet.ControlNet | xs.ControlNetXS | lite.ControlLLLite | None = None # map to input image - self.override: Image = None + self.override: Image.Image | None = None # global settings but passed per-unit self.factor = 1.0 self.guess = False @@ -177,6 +178,8 @@ class Unit: # mashup of gradio controls and mapping to actual implementation cla # bind ui controls to properties if present if self.type == 't2i adapter': + if TYPE_CHECKING: + assert isinstance(self.adapter, t2iadapter.Adapter) if model_id is not None: if isinstance(model_id, str): self.adapter.load(model_id) @@ -186,6 +189,8 @@ class Unit: # mashup of gradio controls and mapping to actual implementation cla if extra_controls is not None and len(extra_controls) > 0: extra_controls[0].change(fn=adapter_extra, inputs=extra_controls) elif self.type == 'controlnet': + if TYPE_CHECKING: + assert isinstance(self.controlnet, controlnet.ControlNet) if model_id is not None: if isinstance(model_id, str): self.controlnet.load(model_id) @@ -196,6 +201,8 @@ class Unit: # mashup of gradio controls and mapping to actual implementation cla if extra_controls is not None and len(extra_controls) > 0: extra_controls[0].change(fn=controlnet_extra, inputs=extra_controls) elif self.type == 'xs': + if TYPE_CHECKING: + assert isinstance(self.controlnet, xs.ControlNetXS) if model_id is not None: if isinstance(model_id, str): self.controlnet.load(model_id) @@ -205,6 +212,8 @@ class Unit: # mashup of gradio controls and mapping to actual implementation cla if extra_controls is not None and len(extra_controls) > 0: extra_controls[0].change(fn=controlnetxs_extra, inputs=extra_controls) elif self.type == 'lite': + if TYPE_CHECKING: + assert isinstance(self.controlnet, lite.ControlLLLite) if model_id is not None: if isinstance(model_id, str): self.controlnet.load(model_id) From 481d974b91983e58026052be36d3d634457dc262 Mon Sep 17 00:00:00 2001 From: awsr <43862868+awsr@users.noreply.github.com> Date: Tue, 24 Mar 2026 03:40:15 -0700 Subject: [PATCH 08/25] RUF013 update --- modules/devices.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/devices.py b/modules/devices.py index 4e0d63722..fbcf0146a 100644 --- a/modules/devices.py +++ b/modules/devices.py @@ -197,7 +197,7 @@ def get_optimal_device(): return torch.device(get_optimal_device_name()) -def torch_gc(force:bool=False, fast:bool=False, reason:str=None): +def torch_gc(force: bool = False, fast: bool = False, reason: str | None = None): def get_stats(): mem_dict = memstats.memory_stats() gpu_dict = mem_dict.get('gpu', {}) From d3f925e8e5df4dba5bacf334efc8d81fb364aa88 Mon Sep 17 00:00:00 2001 From: awsr <43862868+awsr@users.noreply.github.com> Date: Tue, 24 Mar 2026 03:46:26 -0700 Subject: [PATCH 09/25] RUF013 update --- modules/face/instantid_model.py | 10 +++++----- modules/face/photomaker_pipeline.py | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/modules/face/instantid_model.py b/modules/face/instantid_model.py index 2511ec27a..be17b709a 100644 --- a/modules/face/instantid_model.py +++ b/modules/face/instantid_model.py @@ -545,9 +545,9 @@ class StableDiffusionXLInstantIDPipeline(StableDiffusionXLControlNetPipeline): @replace_example_docstring(EXAMPLE_DOC_STRING) def __call__( self, - prompt: str | list[str] = None, + prompt: str | list[str] | None = None, prompt_2: str | list[str] | None = None, - image: PipelineImageInput = None, + image: PipelineImageInput | None = None, height: int | None = None, width: int | None = None, num_inference_steps: int = 50, @@ -570,15 +570,15 @@ class StableDiffusionXLInstantIDPipeline(StableDiffusionXLControlNetPipeline): guess_mode: bool = False, control_guidance_start: float | list[float] = 0.0, control_guidance_end: float | list[float] = 1.0, - original_size: tuple[int, int] = None, + original_size: tuple[int, int] | None = None, crops_coords_top_left: tuple[int, int] = (0, 0), - target_size: tuple[int, int] = None, + target_size: tuple[int, int] | None = None, negative_original_size: tuple[int, int] | None = None, negative_crops_coords_top_left: tuple[int, int] = (0, 0), negative_target_size: tuple[int, int] | None = None, clip_skip: int | None = None, callback_on_step_end: Callable[[int, int, dict], None] | None = None, - callback_on_step_end_tensor_inputs: list[str] = None, + callback_on_step_end_tensor_inputs: list[str] | None = None, **kwargs, ): r""" diff --git a/modules/face/photomaker_pipeline.py b/modules/face/photomaker_pipeline.py index 191c4f352..82daefbd7 100644 --- a/modules/face/photomaker_pipeline.py +++ b/modules/face/photomaker_pipeline.py @@ -445,13 +445,13 @@ class PhotoMakerStableDiffusionXLPipeline(StableDiffusionXLPipeline): @torch.no_grad() def __call__( self, - prompt: str | list[str] = None, + prompt: str | list[str] | None = None, prompt_2: str | list[str] | None = None, height: int | None = None, width: int | None = None, num_inference_steps: int = 50, - timesteps: list[int] = None, - sigmas: list[float] = None, + timesteps: list[int] | None = None, + sigmas: list[float] | None = None, denoising_end: float | None = None, guidance_scale: float = 5.0, negative_prompt: str | list[str] | None = None, @@ -478,7 +478,7 @@ class PhotoMakerStableDiffusionXLPipeline(StableDiffusionXLPipeline): negative_target_size: tuple[int, int] | None = None, clip_skip: int | None = None, callback_on_step_end: Callable[[int, int, dict], None] | PipelineCallback | MultiPipelineCallbacks | None = None, - callback_on_step_end_tensor_inputs: list[str] = None, + callback_on_step_end_tensor_inputs: list[str] | None = None, # Added parameters (for PhotoMaker) input_id_images: PipelineImageInput = None, start_merge_step: int = 10, From abe25e7b079e79e75ae6653e364d9285e45976b0 Mon Sep 17 00:00:00 2001 From: awsr <43862868+awsr@users.noreply.github.com> Date: Tue, 24 Mar 2026 03:46:44 -0700 Subject: [PATCH 10/25] RUF013 update --- modules/framepack/create-video.py | 4 ++-- modules/framepack/framepack_hijack.py | 2 +- modules/framepack/framepack_load.py | 4 ++-- modules/framepack/framepack_worker.py | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/modules/framepack/create-video.py b/modules/framepack/create-video.py index f1b334d3a..1863a2e72 100755 --- a/modules/framepack/create-video.py +++ b/modules/framepack/create-video.py @@ -26,7 +26,7 @@ def auth(): return None -def get(endpoint: str, dct: dict = None): +def get(endpoint: str, dct: dict | None = None): req = requests.get(f'{sd_url}{endpoint}', json=dct, timeout=300, verify=False, auth=auth()) if req.status_code != 200: return { 'error': req.status_code, 'reason': req.reason, 'url': req.url } @@ -34,7 +34,7 @@ def get(endpoint: str, dct: dict = None): return req.json() -def post(endpoint: str, dct: dict = None): +def post(endpoint: str, dct: dict | None = None): req = requests.post(f'{sd_url}{endpoint}', json = dct, timeout=None, verify=False, auth=auth()) if req.status_code != 200: return { 'error': req.status_code, 'reason': req.reason, 'url': req.url } diff --git a/modules/framepack/framepack_hijack.py b/modules/framepack/framepack_hijack.py index 4ce623827..63f4b24f0 100644 --- a/modules/framepack/framepack_hijack.py +++ b/modules/framepack/framepack_hijack.py @@ -30,7 +30,7 @@ def set_progress_bar_config(): uni_pc_fm.sample_unipc = sample_unipc -def set_prompt_template(prompt, system_prompt:str=None, optimized_prompt:bool=True, unmodified_prompt:bool=False): +def set_prompt_template(prompt, system_prompt: str | None = None, optimized_prompt: bool = True, unmodified_prompt: bool = False): from modules import shared from modules.framepack.pipeline import hunyuan mode = 'unknown' diff --git a/modules/framepack/framepack_load.py b/modules/framepack/framepack_load.py index 21f0819ce..74e853a52 100644 --- a/modules/framepack/framepack_load.py +++ b/modules/framepack/framepack_load.py @@ -35,7 +35,7 @@ def split_url(url): return { 'repo': f'{url[0]}/{url[1]}', 'subfolder': url[2] } -def set_model(receipe: str=None): +def set_model(receipe: str | None = None): if receipe is None or receipe == '': return lines = [line.strip() for line in receipe.split('\n') if line.strip() != '' and ':' in line] @@ -62,7 +62,7 @@ def reset_model(): return '' -def load_model(variant:str=None, pipeline:str=None, text_encoder:str=None, text_encoder_2:str=None, feature_extractor:str=None, image_encoder:str=None, transformer:str=None): +def load_model(variant: str | None = None, pipeline: str | None = None, text_encoder: str | None = None, text_encoder_2: str | None = None, feature_extractor: str | None = None, image_encoder: str | None = None, transformer: str | None = None): shared.state.begin('Load FramePack') if variant is not None: if variant not in models.keys(): diff --git a/modules/framepack/framepack_worker.py b/modules/framepack/framepack_worker.py index 85f87a624..fdd9637c9 100644 --- a/modules/framepack/framepack_worker.py +++ b/modules/framepack/framepack_worker.py @@ -44,7 +44,7 @@ def worker( mp4_fps, mp4_codec, mp4_sf, mp4_video, mp4_frames, mp4_opt, mp4_ext, mp4_interpolate, vae_type, variant, - metadata:dict=None, + metadata: dict | None = None, ): if metadata is None: metadata = {} @@ -85,7 +85,7 @@ def worker( if not is_f1: prompts = list(reversed(prompts)) - def text_encode(prompt, i:int=None): + def text_encode(prompt, i: int | None = None): jobid = shared.state.begin('TE Encode') pbar.update(task, description=f'text encode section={i}') t0 = time.time() From 2d81bcdc697d04b48c13e3932b97d976c3cac123 Mon Sep 17 00:00:00 2001 From: awsr <43862868+awsr@users.noreply.github.com> Date: Tue, 24 Mar 2026 04:12:14 -0700 Subject: [PATCH 11/25] RUF013 updates + type fixes --- modules/image/grid.py | 17 +++++++++++++---- modules/image/resize.py | 4 ++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/modules/image/grid.py b/modules/image/grid.py index 0a411b500..9ba8801fe 100644 --- a/modules/image/grid.py +++ b/modules/image/grid.py @@ -1,5 +1,6 @@ import math from collections import namedtuple +from typing import TYPE_CHECKING import numpy as np from PIL import Image, ImageFont, ImageDraw from modules import shared, script_callbacks @@ -26,7 +27,7 @@ def check_grid_size(imgs): return ok -def get_grid_size(imgs, batch_size=1, rows=None, cols=None): +def get_grid_size(imgs, batch_size=1, rows: int | None = None, cols: int | None = None): if rows and rows > len(imgs): rows = len(imgs) if cols and cols > len(imgs): @@ -34,12 +35,16 @@ def get_grid_size(imgs, batch_size=1, rows=None, cols=None): if rows is None and cols is None: if shared.opts.n_rows > 0: rows = shared.opts.n_rows + if TYPE_CHECKING: + assert isinstance(rows, int) cols = math.ceil(len(imgs) / rows) elif shared.opts.n_rows == 0: rows = batch_size cols = math.ceil(len(imgs) / rows) elif shared.opts.n_cols > 0: cols = shared.opts.n_cols + if TYPE_CHECKING: + assert isinstance(cols, int) rows = math.ceil(len(imgs) / cols) elif shared.opts.n_cols == 0: cols = batch_size @@ -49,16 +54,20 @@ def get_grid_size(imgs, batch_size=1, rows=None, cols=None): while len(imgs) % rows != 0: rows -= 1 cols = math.ceil(len(imgs) / rows) - elif cols is None: + return rows, cols + elif rows is not None and cols is None: cols = math.ceil(len(imgs) / rows) - elif rows is None: + elif rows is None and cols is not None: rows = math.ceil(len(imgs) / cols) else: + if TYPE_CHECKING: + assert isinstance(rows, int) + assert isinstance(cols, int) pass return rows, cols -def image_grid(imgs, batch_size:int=1, rows:int=None, cols:int=None): +def image_grid(imgs, batch_size=1, rows=1, cols=1): rows, cols = get_grid_size(imgs, batch_size, rows=rows, cols=cols) params = script_callbacks.ImageGridLoopParams(imgs, cols, rows) script_callbacks.image_grid_callback(params) diff --git a/modules/image/resize.py b/modules/image/resize.py index 1ff04b610..22841e6e5 100644 --- a/modules/image/resize.py +++ b/modules/image/resize.py @@ -8,7 +8,7 @@ from modules.logger import log from modules.image import sharpfin -def resize_image(resize_mode: int, im: Image.Image | torch.Tensor, width: int, height: int, upscaler_name: str=None, output_type: str='image', context: str=None): +def resize_image(resize_mode: int, im: Image.Image | torch.Tensor, width: int, height: int, upscaler_name: str | None = None, output_type: str = 'image', context: str | None = None): upscaler_name = upscaler_name or shared.opts.upscaler_for_img2img def verify_image(image): @@ -95,7 +95,7 @@ def resize_image(resize_mode: int, im: Image.Image | torch.Tensor, width: int, h res.paste(im, box=((width - im.width)//2, (height - im.height)//2)) return res - def context_aware(im: Image.Image, width, height, context): + def context_aware(im: Image.Image, width: int, height: int, context: str): from installer import install install('seam-carving') width, height = int(width), int(height) From 6ab9b7bc6250b053eb1589b6016bf9827ca329e6 Mon Sep 17 00:00:00 2001 From: awsr <43862868+awsr@users.noreply.github.com> Date: Tue, 24 Mar 2026 04:19:53 -0700 Subject: [PATCH 12/25] RUF013 updates --- modules/intel/openvino/__init__.py | 6 +++--- modules/logger.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/intel/openvino/__init__.py b/modules/intel/openvino/__init__.py index 661b1f393..31405f1f7 100644 --- a/modules/intel/openvino/__init__.py +++ b/modules/intel/openvino/__init__.py @@ -83,7 +83,7 @@ def warn_once(msg): warned = True class OpenVINOGraphModule(torch.nn.Module): - def __init__(self, gm, partition_id, use_python_fusion_cache, model_hash_str: str = None, file_name="", int_inputs=None): + def __init__(self, gm, partition_id, use_python_fusion_cache, model_hash_str: str | None = None, file_name="", int_inputs: list | None = None): if int_inputs is None: int_inputs = [] super().__init__() @@ -211,7 +211,7 @@ def execute_cached(compiled_model, *args): result = [torch.from_numpy(res[out]) for out in compiled_model.outputs] return result -def openvino_compile(gm: GraphModule, *example_inputs, model_hash_str: str = None, file_name=""): +def openvino_compile(gm: GraphModule, *example_inputs, model_hash_str: str | None = None, file_name=""): core = Core() device = get_device() @@ -427,7 +427,7 @@ def openvino_execute_partitioned(gm: GraphModule, *args, executor_parameters=Non return shared.compiled_model_state.partitioned_modules[signature][0](*ov_inputs) -def partition_graph(gm: GraphModule, use_python_fusion_cache: bool, model_hash_str: str = None, file_name="", int_inputs=None): +def partition_graph(gm: GraphModule, use_python_fusion_cache: bool, model_hash_str: str | None = None, file_name="", int_inputs=None): if int_inputs is None: int_inputs = [] for node in gm.graph.nodes: diff --git a/modules/logger.py b/modules/logger.py index d451df0c5..89f5a2277 100644 --- a/modules/logger.py +++ b/modules/logger.py @@ -40,7 +40,7 @@ def get_log(): return log -def install_traceback(suppress: list = None): +def install_traceback(suppress: list | None = None): if suppress is None: suppress = [] width = os.environ.get("SD_TRACEWIDTH", console.width if console else None) @@ -143,7 +143,7 @@ def setup_logging(debug=None, trace=None, filename=None): logging.Logger.trace = partialmethod(logging.Logger.log, logging.TRACE) logging.trace = partial(logging.log, logging.TRACE) - def exception_hook(e: Exception, suppress=None): + def exception_hook(e: Exception, suppress: list | None = None): from rich.traceback import Traceback if suppress is None: suppress = [] From 92960de8d6a2778954cfb7d2d6482cd765bf6130 Mon Sep 17 00:00:00 2001 From: awsr <43862868+awsr@users.noreply.github.com> Date: Tue, 24 Mar 2026 04:27:30 -0700 Subject: [PATCH 13/25] RUF013 updates --- modules/lora/extra_networks_lora.py | 2 +- modules/masking.py | 2 +- modules/merging/convert_sdxl.py | 2 +- modules/merging/modules_sdxl.py | 14 +++++++------- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/modules/lora/extra_networks_lora.py b/modules/lora/extra_networks_lora.py index 9ca2b3b9f..4e82e79f7 100644 --- a/modules/lora/extra_networks_lora.py +++ b/modules/lora/extra_networks_lora.py @@ -173,7 +173,7 @@ class ExtraNetworkLora(extra_networks.ExtraNetwork): def signature(self, names: list[str], te_multipliers: list, unet_multipliers: list): return [f'{name}:{te}:{unet}' for name, te, unet in zip(names, te_multipliers, unet_multipliers, strict=False)] - def changed(self, requested: list[str], include: list[str] = None, exclude: list[str] = None) -> bool: + def changed(self, requested: list[str], include: list[str] | None = None, exclude: list[str] | None = None) -> bool: if shared.opts.lora_force_reload: debug_log(f'Network check: type=LoRA requested={requested} status=forced') return True diff --git a/modules/masking.py b/modules/masking.py index 82c24a3e2..600ec87f2 100644 --- a/modules/masking.py +++ b/modules/masking.py @@ -378,7 +378,7 @@ def outpaint(input_image: Image.Image, outpaint_type: str = 'Edge'): return image, mask -def run_mask(input_image: Image.Image, input_mask: Image.Image = None, return_type: str = None, mask_blur: int = None, mask_padding: int = None, invert=None): +def run_mask(input_image: Image.Image, input_mask: Image.Image | None = None, return_type: str | None = None, mask_blur: int | None = None, mask_padding: int | None = None, invert=None): if isinstance(input_image, list) and len(input_image) > 0: input_image = input_image[0] elif isinstance(input_image, dict): diff --git a/modules/merging/convert_sdxl.py b/modules/merging/convert_sdxl.py index 3238cfd35..969816ae1 100644 --- a/modules/merging/convert_sdxl.py +++ b/modules/merging/convert_sdxl.py @@ -260,7 +260,7 @@ def calculate_model_hash(state_dict): return func.hexdigest() -def convert(model_path:str, checkpoint_path:str, metadata:dict=None): +def convert(model_path: str, checkpoint_path: str, metadata: dict | None = None): if metadata is None: metadata = {} unet_path = os.path.join(model_path, "unet", "diffusion_pytorch_model.safetensors") diff --git a/modules/merging/modules_sdxl.py b/modules/merging/modules_sdxl.py index d4fb799c5..b803e90d3 100644 --- a/modules/merging/modules_sdxl.py +++ b/modules/merging/modules_sdxl.py @@ -65,7 +65,7 @@ def msg(text, err:bool=False): return status -def load_base(override:str=None): +def load_base(override: str | None = None): global pipeline # pylint: disable=global-statement fn = override or recipe.base yield msg(f'base={fn}') @@ -79,7 +79,7 @@ def load_base(override:str=None): pipeline.vae.register_to_config(force_upcast = False) -def load_unet(pipe: diffusers.StableDiffusionXLPipeline, override:str=None): +def load_unet(pipe: diffusers.StableDiffusionXLPipeline, override: str | None = None): if (recipe.unet is None or len(recipe.unet) == 0) and override is None: return fn = override or recipe.unet @@ -99,7 +99,7 @@ def load_unet(pipe: diffusers.StableDiffusionXLPipeline, override:str=None): yield msg(f'unet: {e}') -def load_scheduler(pipe: diffusers.StableDiffusionXLPipeline, override:str=None): +def load_scheduler(pipe: diffusers.StableDiffusionXLPipeline, override: str | None = None): if recipe.scheduler is None and override is None: return config = pipe.scheduler.config.__dict__ @@ -114,7 +114,7 @@ def load_scheduler(pipe: diffusers.StableDiffusionXLPipeline, override:str=None) -def load_vae(pipe: diffusers.StableDiffusionXLPipeline, override:str=None): +def load_vae(pipe: diffusers.StableDiffusionXLPipeline, override: str | None = None): if (recipe.vae is None or len(recipe.vae) == 0)and override is None: return fn = override or recipe.vae @@ -135,7 +135,7 @@ def load_vae(pipe: diffusers.StableDiffusionXLPipeline, override:str=None): yield msg(f'vae: {e}') -def load_te1(pipe: diffusers.StableDiffusionXLPipeline, override:str=None): +def load_te1(pipe: diffusers.StableDiffusionXLPipeline, override: str | None = None): if (recipe.te1 is None or len(recipe.te1) == 0) and override is None: return config = pipe.text_encoder.config.__dict__ @@ -156,7 +156,7 @@ def load_te1(pipe: diffusers.StableDiffusionXLPipeline, override:str=None): yield msg(f'te1: {e}') -def load_te2(pipe: diffusers.StableDiffusionXLPipeline, override:str=None): +def load_te2(pipe: diffusers.StableDiffusionXLPipeline, override: str | None = None): if (recipe.te2 is None or len(recipe.te2) == 0) and override is None: return config = pipe.text_encoder_2.config.__dict__ @@ -177,7 +177,7 @@ def load_te2(pipe: diffusers.StableDiffusionXLPipeline, override:str=None): yield msg(f'te2: {e}') -def load_lora(pipe: diffusers.StableDiffusionXLPipeline, override: dict=None, fuse: float=None): +def load_lora(pipe: diffusers.StableDiffusionXLPipeline, override: dict | None = None, fuse: float | None = None): if recipe.lora is None and override is None: return names = [] From 641321d7d22d1d6b6001861f37658b47f2b6857b Mon Sep 17 00:00:00 2001 From: awsr <43862868+awsr@users.noreply.github.com> Date: Tue, 24 Mar 2026 04:34:38 -0700 Subject: [PATCH 14/25] RUF013 updates --- modules/model_quant.py | 16 ++++++++-------- modules/modelloader.py | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/modules/model_quant.py b/modules/model_quant.py index b57c29582..a963dc3cc 100644 --- a/modules/model_quant.py +++ b/modules/model_quant.py @@ -51,7 +51,7 @@ def dont_quant(): return False -def create_bnb_config(kwargs = None, allow: bool = True, module: str = 'Model', modules_to_not_convert: list = None): +def create_bnb_config(kwargs = None, allow: bool = True, module: str = 'Model', modules_to_not_convert: list | None = None): from modules import shared, devices if allow and (module == 'any' or module in shared.opts.bnb_quantization): load_bnb() @@ -74,7 +74,7 @@ def create_bnb_config(kwargs = None, allow: bool = True, module: str = 'Model', return kwargs -def create_ao_config(kwargs = None, allow: bool = True, module: str = 'Model', modules_to_not_convert: list = None): +def create_ao_config(kwargs = None, allow: bool = True, module: str = 'Model', modules_to_not_convert: list | None = None): from modules import shared if allow and (shared.opts.torchao_quantization_mode in {'pre', 'auto'}) and (module == 'any' or module in shared.opts.torchao_quantization): torchao = load_torchao() @@ -93,7 +93,7 @@ def create_ao_config(kwargs = None, allow: bool = True, module: str = 'Model', m return kwargs -def create_quanto_config(kwargs = None, allow: bool = True, module: str = 'Model', modules_to_not_convert: list = None): +def create_quanto_config(kwargs = None, allow: bool = True, module: str = 'Model', modules_to_not_convert: list | None = None): from modules import shared if allow and (module == 'any' or module in shared.opts.quanto_quantization): load_quanto(silent=True) @@ -115,7 +115,7 @@ def create_quanto_config(kwargs = None, allow: bool = True, module: str = 'Model return kwargs -def create_trt_config(kwargs = None, allow: bool = True, module: str = 'Model', modules_to_not_convert: list = None): +def create_trt_config(kwargs = None, allow: bool = True, module: str = 'Model', modules_to_not_convert: list | None = None): from modules import shared if allow and (module == 'any' or module in shared.opts.trt_quantization): load_trt() @@ -163,7 +163,7 @@ def get_sdnq_devices(mode="pre"): return quantization_device, return_device -def create_sdnq_config(kwargs = None, allow: bool = True, module: str = 'Model', weights_dtype: str = None, quantized_matmul_dtype: str = None, modules_to_not_convert: list = None, modules_dtype_dict: dict = None): +def create_sdnq_config(kwargs = None, allow: bool = True, module: str = 'Model', weights_dtype: str | None = None, quantized_matmul_dtype: str | None = None, modules_to_not_convert: list | None = None, modules_dtype_dict: dict | None = None): from modules import shared if allow and (shared.opts.sdnq_quantize_mode in {'pre', 'auto'}) and (module == 'any' or module in shared.opts.sdnq_quantize_weights): from modules.sdnq import SDNQConfig @@ -276,7 +276,7 @@ def check_nunchaku(module: str = ''): return False -def create_config(kwargs = None, allow: bool = True, module: str = 'Model', modules_to_not_convert: list = None, modules_dtype_dict: dict = None): +def create_config(kwargs = None, allow: bool = True, module: str = 'Model', modules_to_not_convert: list | None = None, modules_dtype_dict: dict | None = None): if kwargs is None: kwargs = {} if module == 'Model' and dont_quant(): @@ -508,7 +508,7 @@ def apply_layerwise(sd_model, quiet:bool=False): log.error(f'Quantization: type=layerwise {e}') -def sdnq_quantize_model(model, op=None, sd_model=None, do_gc: bool = True, weights_dtype: str = None, quantized_matmul_dtype: str = None, modules_to_not_convert: list = None, modules_dtype_dict: dict = None): +def sdnq_quantize_model(model, op=None, sd_model=None, do_gc: bool = True, weights_dtype: str | None = None, quantized_matmul_dtype: str | None = None, modules_to_not_convert: list | None = None, modules_dtype_dict: dict | None = None): global quant_last_model_name, quant_last_model_device # pylint: disable=global-statement from modules import devices, shared, timer from modules.sdnq import sdnq_post_load_quant @@ -774,7 +774,7 @@ def torchao_quantization(sd_model): return sd_model -def get_dit_args(load_config:dict=None, module:str=None, device_map:bool=False, allow_quant:bool=True, modules_to_not_convert: list = None, modules_dtype_dict: dict = None): +def get_dit_args(load_config: dict | None = None, module: str | None = None, device_map: bool = False, allow_quant: bool = True, modules_to_not_convert: list | None = None, modules_dtype_dict: dict | None = None): from modules import shared, devices config = {} if load_config is None else load_config.copy() if 'torch_dtype' not in config: diff --git a/modules/modelloader.py b/modules/modelloader.py index 6d6a776f7..974a4b1a5 100644 --- a/modules/modelloader.py +++ b/modules/modelloader.py @@ -56,7 +56,7 @@ def hf_login(token=None): return True -def download_diffusers_model(hub_id: str, cache_dir: str = None, download_config: dict[str, str] = None, token = None, variant = None, revision = None, mirror = None, custom_pipeline = None): +def download_diffusers_model(hub_id: str, cache_dir: str | None = None, download_config: dict[str, str | bool] | None = None, token = None, variant = None, revision = None, mirror = None, custom_pipeline = None): if hub_id is None or len(hub_id) == 0: return None from diffusers import DiffusionPipeline @@ -219,7 +219,7 @@ def get_reference_opts(name: str, quiet=False): return model_opts -def load_reference(name: str, variant: str = None, revision: str = None, mirror: str = None, custom_pipeline: str = None): +def load_reference(name: str, variant: str | None = None, revision: str | None = None, mirror: str | None = None, custom_pipeline: str | None = None): if '+' in name: name = name.split('+')[0] found = [r for r in diffuser_repos if name == r['name'] or name == r['friendly'] or name == r['path']] @@ -337,7 +337,7 @@ def load_file_from_url(url: str, *, model_dir: str, progress: bool = True, file_ return None -def load_models(model_path: str, model_url: str = None, command_path: str = None, ext_filter=None, download_name=None, ext_blacklist=None) -> list: +def load_models(model_path: str, model_url: str | None = None, command_path: str | None = None, ext_filter=None, download_name=None, ext_blacklist=None) -> list: """ A one-and done loader to try finding the desired models in specified directories. @param download_name: Specify to download from model_url immediately. @@ -404,7 +404,7 @@ def cleanup_models(): move_files(src_path, dest_path) -def move_files(src_path: str, dest_path: str, ext_filter: str = None): +def move_files(src_path: str, dest_path: str, ext_filter: str | None = None): try: if not os.path.exists(dest_path): os.makedirs(dest_path) From fb853af4b0d5b20ce679fcd8444a77301121e0e7 Mon Sep 17 00:00:00 2001 From: awsr <43862868+awsr@users.noreply.github.com> Date: Tue, 24 Mar 2026 04:58:19 -0700 Subject: [PATCH 15/25] RUF013 updates --- .../onnx_stable_diffusion_pipeline.py | 2 +- modules/options_handler.py | 2 +- modules/postprocess/pixelart.py | 4 +- modules/postprocess/yolo.py | 6 +- modules/processing_callbacks.py | 2 +- modules/processing_class.py | 216 +++++++++--------- modules/processing_helpers.py | 2 +- 7 files changed, 117 insertions(+), 117 deletions(-) diff --git a/modules/onnx_impl/pipelines/onnx_stable_diffusion_pipeline.py b/modules/onnx_impl/pipelines/onnx_stable_diffusion_pipeline.py index 2b583e8f5..dc2aa9e75 100644 --- a/modules/onnx_impl/pipelines/onnx_stable_diffusion_pipeline.py +++ b/modules/onnx_impl/pipelines/onnx_stable_diffusion_pipeline.py @@ -30,7 +30,7 @@ class OnnxStableDiffusionPipeline(diffusers.OnnxStableDiffusionPipeline, Callabl def __call__( self, - prompt: str | list[str] = None, + prompt: str | list[str] | None = None, height: int | None = 512, width: int | None = 512, num_inference_steps: int | None = 50, diff --git a/modules/options_handler.py b/modules/options_handler.py index 5041eb86c..c14fadfb4 100644 --- a/modules/options_handler.py +++ b/modules/options_handler.py @@ -28,7 +28,7 @@ class Options: debug = os.environ.get('SD_CONFIG_DEBUG', None) is not None secrets_debug = os.environ.get("SD_SECRETS_DEBUG", None) is not None - def __init__(self, options_templates: dict[str, OptionInfo | LegacyOption] = None, restricted: set[str] | None = None, *, filename = '', secrets = ''): + def __init__(self, options_templates: dict[str, OptionInfo | LegacyOption] | None = None, restricted: set[str] | None = None, *, filename = '', secrets = ''): if options_templates is None: options_templates = {} if restricted is None: diff --git a/modules/postprocess/pixelart.py b/modules/postprocess/pixelart.py index 00b9b9636..57768b1b4 100644 --- a/modules/postprocess/pixelart.py +++ b/modules/postprocess/pixelart.py @@ -224,8 +224,8 @@ class JPEGEncoder(ImageProcessingMixin, ConfigMixin): block_size: int = 16, cbcr_downscale: int = 2, norm: str = "ortho", - latents_std: list[float] = None, - latents_mean: list[float] = None, + latents_std: list[float] | None = None, + latents_mean: list[float] | None = None, ): self.block_size = block_size self.cbcr_downscale = cbcr_downscale diff --git a/modules/postprocess/yolo.py b/modules/postprocess/yolo.py index 89802050d..e70df763f 100644 --- a/modules/postprocess/yolo.py +++ b/modules/postprocess/yolo.py @@ -96,11 +96,11 @@ class YoloRestorer(Detailer): imgsz: int = 640, half: bool = True, device = devices.device, - augment: bool = None, agnostic: bool = False, retina: bool = False, mask: bool = True, - offload: bool = None, + augment: bool | None = None, + offload: bool | None = None, p = None, ) -> list[YoloResult]: if augment is None: @@ -201,7 +201,7 @@ class YoloRestorer(Detailer): break return result - def load(self, model_name: str = None): + def load(self, model_name: str | None = None): with load_lock: from modules import modelloader model = None diff --git a/modules/processing_callbacks.py b/modules/processing_callbacks.py index 0f6fdb533..e1c2c19ba 100644 --- a/modules/processing_callbacks.py +++ b/modules/processing_callbacks.py @@ -51,7 +51,7 @@ def diffusers_callback_legacy(step: int, timestep: int, latents: torch.FloatTens time.sleep(0.1) -def diffusers_callback(pipe, step: int = 0, timestep: int = 0, kwargs: dict = None): +def diffusers_callback(pipe, step: int = 0, timestep: int = 0, kwargs: dict | None = None): if kwargs is None: kwargs = {} t0 = time.time() diff --git a/modules/processing_class.py b/modules/processing_class.py index 75ab9cb71..287a2b17a 100644 --- a/modules/processing_class.py +++ b/modules/processing_class.py @@ -35,10 +35,10 @@ class StableDiffusionProcessing: width: int = 1024, height: int = 1024, # samplers - sampler_index: int = None, # pylint: disable=unused-argument # used only to set sampler_name - sampler_name: str = None, - hr_sampler_name: str = None, - eta: float = None, + sampler_index: int | None = None, # pylint: disable=unused-argument # used only to set sampler_name + sampler_name: str | None = None, + hr_sampler_name: str | None = None, + eta: float | None = None, # modular guidance guidance_name: str = 'Default', guidance_scale: float = 6.0, @@ -52,7 +52,7 @@ class StableDiffusionProcessing: pag_scale: float = 0.0, pag_adaptive: float = 0.5, # styles - styles: list[str] = None, + styles: list[str] | None = None, # vae tiling: bool = False, vae_type: str = 'Full', @@ -66,53 +66,53 @@ class StableDiffusionProcessing: detailer_steps: int = 10, detailer_strength: float = 0.3, detailer_resolution: int = 1024, - detailer_segmentation: bool = None, - detailer_include_detections: bool = None, - detailer_merge: bool = None, - detailer_sort: bool = None, - detailer_classes: str = None, - detailer_conf: float = None, - detailer_iou: float = None, - detailer_max: int = None, - detailer_min_size: float = None, - detailer_max_size: float = None, - detailer_blur: int = None, - detailer_padding: int = None, - detailer_sigma_adjust: float = None, - detailer_sigma_adjust_max: float = None, - detailer_models: list = None, - detailer_augment: bool = None, + detailer_segmentation: bool | None = None, + detailer_include_detections: bool | None = None, + detailer_merge: bool | None = None, + detailer_sort: bool | None = None, + detailer_classes: str | None = None, + detailer_conf: float | None = None, + detailer_iou: float | None = None, + detailer_max: int | None = None, + detailer_min_size: float | None = None, + detailer_max_size: float | None = None, + detailer_blur: int | None = None, + detailer_padding: int | None = None, + detailer_sigma_adjust: float | None = None, + detailer_sigma_adjust_max: float | None = None, + detailer_models: list | None = None, + detailer_augment: bool | None = None, # img2img and mask - img2img_color_correction: bool = None, - color_correction_method: str = None, - img2img_background_color: str = None, - img2img_fix_steps: bool = None, - mask_apply_overlay: bool = None, - include_mask: bool = None, - inpainting_mask_weight: float = None, + img2img_color_correction: bool | None = None, + color_correction_method: str | None = None, + img2img_background_color: str | None = None, + img2img_fix_steps: bool | None = None, + mask_apply_overlay: bool | None = None, + include_mask: bool | None = None, + inpainting_mask_weight: float | None = None, # output and saving - samples_save: bool = None, - samples_format: str = None, - save_images_before_highres_fix: bool = None, - save_images_before_refiner: bool = None, - save_images_before_detailer: bool = None, - save_images_before_color_correction: bool = None, - grid_save: bool = None, - grid_format: str = None, - return_grid: bool = None, - save_mask: bool = None, - save_mask_composite: bool = None, - return_mask: bool = None, - return_mask_composite: bool = None, - keep_incomplete: bool = None, - image_metadata: bool = None, - jpeg_quality: int = None, + samples_save: bool | None = None, + samples_format: str | None = None, + save_images_before_highres_fix: bool | None = None, + save_images_before_refiner: bool | None = None, + save_images_before_detailer: bool | None = None, + save_images_before_color_correction: bool | None = None, + grid_save: bool | None = None, + grid_format: str | None = None, + return_grid: bool | None = None, + save_mask: bool | None = None, + save_mask_composite: bool | None = None, + return_mask: bool | None = None, + return_mask_composite: bool | None = None, + keep_incomplete: bool | None = None, + image_metadata: bool | None = None, + jpeg_quality: int | None = None, # lora behavior - lora_fuse_native: bool = None, - lora_fuse_diffusers: bool = None, - lora_force_reload: bool = None, - extra_networks_default_multiplier: float = None, - lora_apply_tags: int = None, + lora_fuse_native: bool | None = None, + lora_fuse_diffusers: bool | None = None, + lora_force_reload: bool | None = None, + extra_networks_default_multiplier: float | None = None, + lora_apply_tags: int | None = None, # hdr corrections hdr_mode: int = 0, hdr_brightness: float = 0, @@ -148,11 +148,11 @@ class StableDiffusionProcessing: grading_lut_file: str = "", grading_lut_strength: float = 1.0, # img2img - init_images: list = None, - init_control: list = None, denoising_strength: float = 0.3, - image_cfg_scale: float = None, - initial_noise_multiplier: float = None, # pylint: disable=unused-argument # a1111 compatibility + init_images: list | None = None, + init_control: list | None = None, + image_cfg_scale: float | None = None, + initial_noise_multiplier: float | None = None, # pylint: disable=unused-argument # a1111 compatibility # resize scale_by: float = 1, selected_scale_tab: int = 0, # pylint: disable=unused-argument # a1111 compatibility @@ -199,12 +199,12 @@ class StableDiffusionProcessing: hr_force: bool = False, hr_resize_mode: int = 0, hr_resize_context: str = 'None', - hr_upscaler: str = None, hr_second_pass_steps: int = 0, hr_resize_x: int = 0, hr_resize_y: int = 0, hr_denoising_strength: float = 0.0, refiner_steps: int = 5, + hr_upscaler: str | None = None, refiner_start: float = 0, refiner_prompt: str = '', refiner_negative: str = '', @@ -219,65 +219,65 @@ class StableDiffusionProcessing: # xyz flag xyz: bool = False, # scripts - script_args: list = [], + script_args: list | None = None, # scheduler/noise overrides - schedulers_prediction_type: str = None, - schedulers_beta_schedule: str = None, - schedulers_timesteps: str = None, - schedulers_sigma: str = None, - schedulers_use_thresholding: bool = None, - schedulers_use_loworder: bool = None, - schedulers_solver_order: int = None, - uni_pc_variant: str = None, - schedulers_beta_start: float = None, - schedulers_beta_end: float = None, - schedulers_shift: float = None, - schedulers_dynamic_shift: bool = None, - schedulers_base_shift: float = None, - schedulers_max_shift: float = None, - schedulers_rescale_betas: bool = None, - schedulers_timestep_spacing: str = None, - schedulers_timesteps_range: int = None, - schedulers_sigma_adjust: float = None, - schedulers_sigma_adjust_min: float = None, - schedulers_sigma_adjust_max: float = None, - scheduler_eta: float = None, - eta_noise_seed_delta: int = None, - enable_batch_seeds: bool = None, - diffusers_generator_device: str = None, - nan_skip: bool = None, - sequential_seed: bool = None, + schedulers_prediction_type: str | None = None, + schedulers_beta_schedule: str | None = None, + schedulers_timesteps: str | None = None, + schedulers_sigma: str | None = None, + schedulers_use_thresholding: bool | None = None, + schedulers_use_loworder: bool | None = None, + schedulers_solver_order: int | None = None, + uni_pc_variant: str | None = None, + schedulers_beta_start: float | None = None, + schedulers_beta_end: float | None = None, + schedulers_shift: float | None = None, + schedulers_dynamic_shift: bool | None = None, + schedulers_base_shift: float | None = None, + schedulers_max_shift: float | None = None, + schedulers_rescale_betas: bool | None = None, + schedulers_timestep_spacing: str | None = None, + schedulers_timesteps_range: int | None = None, + schedulers_sigma_adjust: float | None = None, + schedulers_sigma_adjust_min: float | None = None, + schedulers_sigma_adjust_max: float | None = None, + scheduler_eta: float | None = None, + eta_noise_seed_delta: int | None = None, + enable_batch_seeds: bool | None = None, + diffusers_generator_device: str | None = None, + nan_skip: bool | None = None, + sequential_seed: bool | None = None, # prompt/attention overrides - prompt_attention: str = None, - prompt_mean_norm: bool = None, - diffusers_zeros_prompt_pad: bool = None, - te_pooled_embeds: bool = None, - lora_apply_te: bool = None, - te_complex_human_instruction: str = None, - te_use_mask: bool = None, + prompt_attention: str | None = None, + prompt_mean_norm: bool | None = None, + diffusers_zeros_prompt_pad: bool | None = None, + te_pooled_embeds: bool | None = None, + lora_apply_te: bool | None = None, + te_complex_human_instruction: str | None = None, + te_use_mask: bool | None = None, # generation modifier overrides (hijack) - freeu_enabled: bool = None, - freeu_b1: float = None, - freeu_b2: float = None, - freeu_s1: float = None, - freeu_s2: float = None, - hypertile_unet_enabled: bool = None, - hypertile_hires_only: bool = None, - hypertile_unet_tile: int = None, - hypertile_unet_min_tile: int = None, - hypertile_unet_swap_size: int = None, - hypertile_unet_depth: int = None, - hypertile_vae_enabled: bool = None, - hypertile_vae_tile: int = None, - hypertile_vae_swap_size: int = None, - teacache_enabled: bool = None, - teacache_thresh: float = None, - token_merging_method: str = None, - tome_ratio: float = None, - todo_ratio: float = None, + freeu_enabled: bool | None = None, + freeu_b1: float | None = None, + freeu_b2: float | None = None, + freeu_s1: float | None = None, + freeu_s2: float | None = None, + hypertile_unet_enabled: bool | None = None, + hypertile_hires_only: bool | None = None, + hypertile_unet_tile: int | None = None, + hypertile_unet_min_tile: int | None = None, + hypertile_unet_swap_size: int | None = None, + hypertile_unet_depth: int | None = None, + hypertile_vae_enabled: bool | None = None, + hypertile_vae_tile: int | None = None, + hypertile_vae_swap_size: int | None = None, + teacache_enabled: bool | None = None, + teacache_thresh: float | None = None, + token_merging_method: str | None = None, + tome_ratio: float | None = None, + todo_ratio: float | None = None, # overrides - override_settings: dict[str, Any] = None, override_settings_restore_afterwards: bool = True, + override_settings: dict[str, Any] | None = None, # metadata # extra_generation_params: Dict[Any, Any] = {}, # task_args: Dict[str, Any] = {}, diff --git a/modules/processing_helpers.py b/modules/processing_helpers.py index ed3e546a0..f93bcea89 100644 --- a/modules/processing_helpers.py +++ b/modules/processing_helpers.py @@ -155,7 +155,7 @@ def images_tensor_to_samples(image, approximation=None, model=None): # pylint: d return x_latent -def get_sampler_name(sampler_index: int, img: bool = False) -> str: +def get_sampler_name(sampler_index: int | None = None, img: bool = False) -> str: sampler_index = sampler_index or 0 if len(sd_samplers.samplers) > sampler_index: sampler_name = sd_samplers.samplers[sampler_index].name From 4f0fb7cc29415fd12cf03097dedd2d331b954848 Mon Sep 17 00:00:00 2001 From: awsr <43862868+awsr@users.noreply.github.com> Date: Tue, 24 Mar 2026 05:00:41 -0700 Subject: [PATCH 16/25] More RUF013 updates for processing_class --- modules/processing_class.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/processing_class.py b/modules/processing_class.py index 287a2b17a..b0e3df0ec 100644 --- a/modules/processing_class.py +++ b/modules/processing_class.py @@ -18,7 +18,7 @@ debug = log.trace if os.environ.get('SD_PROCESS_DEBUG', None) is not None else l @dataclass(repr=False) class StableDiffusionProcessing: def __init__(self, - sd_model_checkpoint: str = None, # # used only to set sd_model + sd_model_checkpoint: str | None = None, # # used only to set sd_model sd_model=None, # pylint: disable=unused-argument # local instance of sd_model # base params prompt: str = "", @@ -834,7 +834,7 @@ class StableDiffusionProcessingControl(StableDiffusionProcessingImg2Img): debug(f'Process init: mode={self.__class__.__name__} kwargs={kwargs}') # pylint: disable=protected-access super().__init__(**kwargs) - def init_hr(self, scale:float=None, upscaler:str=None, force:bool=False): + def init_hr(self, scale: float | None = None, upscaler: str | None = None, force = False): scale = scale or self.scale_by or self.scale_by_before upscaler = upscaler or self.hr_upscaler or self.resize_name or self.resize_name_before if upscaler is None: @@ -852,7 +852,7 @@ class StableDiffusionProcessingControl(StableDiffusionProcessingImg2Img): self.hr_upscale_to_x, self.hr_upscale_to_y = int(self.hr_resize_x), int(self.hr_resize_y) -def switch_class(p: StableDiffusionProcessing, new_class: type, dct: dict = None): +def switch_class(p: StableDiffusionProcessing, new_class: type, dct: dict | None = None): kwargs = {} signature = inspect.signature(StableDiffusionProcessing.__init__, follow_wrapped=True) # base class possible = list(signature.parameters) From 62d22295200c566cfa0e0301d75cd49d4b63d844 Mon Sep 17 00:00:00 2001 From: awsr <43862868+awsr@users.noreply.github.com> Date: Tue, 24 Mar 2026 05:12:18 -0700 Subject: [PATCH 17/25] RUIF013 updates and formatting --- modules/prompt_parser_diffusers.py | 2 +- modules/prompt_parser_xhinker.py | 20 +++----------------- modules/ras/ras_forward.py | 8 ++++---- 3 files changed, 8 insertions(+), 22 deletions(-) diff --git a/modules/prompt_parser_diffusers.py b/modules/prompt_parser_diffusers.py index 0391bdc4a..93bfcbdab 100644 --- a/modules/prompt_parser_diffusers.py +++ b/modules/prompt_parser_diffusers.py @@ -753,7 +753,7 @@ def get_weighted_text_embeddings(pipe, prompt: str = "", neg_prompt: str = "", c return prompt_embeds, pooled_prompt_embeds, None, negative_prompt_embeds, negative_pooled_prompt_embeds, None -def get_xhinker_text_embeddings(pipe, prompt: str = "", neg_prompt: str = "", clip_skip: int = None): +def get_xhinker_text_embeddings(pipe, prompt: str = "", neg_prompt: str = "", clip_skip: int | None = None): is_sd3 = hasattr(pipe, 'text_encoder_3') prompt, prompt_2, _prompt_3, _ = split_prompts(pipe, prompt, is_sd3) neg_prompt, neg_prompt_2, _neg_prompt_3, _ = split_prompts(pipe, neg_prompt, is_sd3) diff --git a/modules/prompt_parser_xhinker.py b/modules/prompt_parser_xhinker.py index 7b4d32d56..cce59d262 100644 --- a/modules/prompt_parser_xhinker.py +++ b/modules/prompt_parser_xhinker.py @@ -27,10 +27,7 @@ from diffusers import ChromaPipeline from modules.prompt_parser import parse_prompt_attention # use built-in A1111 parser -def get_prompts_tokens_with_weights( - clip_tokenizer: CLIPTokenizer - , prompt: str = None -): +def get_prompts_tokens_with_weights(clip_tokenizer: CLIPTokenizer, prompt: str | None = None): """ Get prompt token ids and weights, this function works for both prompt and negative prompt @@ -754,13 +751,7 @@ def get_weighted_text_embeddings_sdxl_refiner( return prompt_embeds, negative_prompt_embeds, pooled_prompt_embeds, negative_pooled_prompt_embeds -def get_weighted_text_embeddings_sdxl_2p( - pipe: StableDiffusionXLPipeline - , prompt: str = "" - , prompt_2: str = None - , neg_prompt: str = "" - , neg_prompt_2: str = None -): +def get_weighted_text_embeddings_sdxl_2p(pipe: StableDiffusionXLPipeline, prompt: str = "", prompt_2: str | None = None, neg_prompt: str = "", neg_prompt_2: str | None = None): """ This function can process long prompt with weights, no length limitation for Stable Diffusion XL, support two prompt sets. @@ -1345,12 +1336,7 @@ def get_weighted_text_embeddings_sd3( return sd3_prompt_embeds, sd3_neg_prompt_embeds, pooled_prompt_embeds, negative_pooled_prompt_embeds -def get_weighted_text_embeddings_flux1( - pipe: FluxPipeline - , prompt: str = "" - , prompt2: str = None - , device=None -): +def get_weighted_text_embeddings_flux1(pipe: FluxPipeline, prompt: str = "", prompt2: str | None = None, device=None): """ This function can process long prompt with weights for flux1 model diff --git a/modules/ras/ras_forward.py b/modules/ras/ras_forward.py index ef0f245ea..14a2080b7 100644 --- a/modules/ras/ras_forward.py +++ b/modules/ras/ras_forward.py @@ -22,10 +22,10 @@ from . import ras_manager def ras_forward( self, hidden_states: torch.FloatTensor, - encoder_hidden_states: torch.FloatTensor = None, - pooled_projections: torch.FloatTensor = None, - timestep: torch.LongTensor = None, - block_controlnet_hidden_states: list = None, + encoder_hidden_states: torch.FloatTensor | None = None, + pooled_projections: torch.FloatTensor | None = None, + timestep: torch.LongTensor | None = None, + block_controlnet_hidden_states: list | None = None, joint_attention_kwargs: dict[str, Any] | None = None, return_dict: bool = True, skip_layers: list[int] | None = None, From f0bb0a921a8d0727489437615d063858bd3e5db6 Mon Sep 17 00:00:00 2001 From: awsr <43862868+awsr@users.noreply.github.com> Date: Tue, 24 Mar 2026 05:13:11 -0700 Subject: [PATCH 18/25] RUF013 updates and value handling fix --- modules/sd_models.py | 4 +++- modules/ui_models.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/sd_models.py b/modules/sd_models.py index b2f7f3c55..57fd85ff8 100644 --- a/modules/sd_models.py +++ b/modules/sd_models.py @@ -1418,7 +1418,7 @@ def hf_auth_check(checkpoint_info, force:bool=False): return False -def save_model(name: str, path: str = None, shard: str = None, overwrite: bool = False): +def save_model(name: str, path: str | None = None, shard: str = "5GB", overwrite = False): if (name is None) or len(name.strip()) == 0: log.error('Save model: invalid model name') return 'Invalid model name' @@ -1432,6 +1432,8 @@ def save_model(name: str, path: str = None, shard: str = None, overwrite: bool = if os.path.exists(model_name) and not overwrite: log.error(f'Save model: path="{model_name}" exists') return f'Path exists: {model_name}' + if not shard.strip(): + shard = "5GB" # Guard against empty input try: t0 = time.time() save_sdnq_model( diff --git a/modules/ui_models.py b/modules/ui_models.py index 2856448e6..881c11649 100644 --- a/modules/ui_models.py +++ b/modules/ui_models.py @@ -75,7 +75,7 @@ def create_ui(): with gr.Row(): save_path = gr.Textbox(label="Model base path", placeholder="Path to save model to", value=opts.diffusers_dir) with gr.Row(): - save_shard = gr.Textbox(label="Max shard size", placeholder="Maximum shard size", value="10GB") + save_shard = gr.Textbox(label="Max shard size", placeholder="Maximum shard size", value="5GB") save_overwrite = gr.Checkbox(label="Overwrite existing", value=False) with gr.Row(): save_result = gr.HTML(value="", elem_id="model_save_outcome") From 3f830589d122b792997935da2a92346514c5cc3c Mon Sep 17 00:00:00 2001 From: awsr <43862868+awsr@users.noreply.github.com> Date: Tue, 24 Mar 2026 05:19:51 -0700 Subject: [PATCH 19/25] RUF013 updates and typing update --- modules/sd_offload.py | 2 +- modules/sd_offload_aux.py | 2 +- modules/sd_te_remote.py | 2 +- modules/sd_unet.py | 8 ++++---- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/modules/sd_offload.py b/modules/sd_offload.py index e2568f1f5..da478b883 100644 --- a/modules/sd_offload.py +++ b/modules/sd_offload.py @@ -467,7 +467,7 @@ def report_model_stats(module_name, module): log.error(f'Module stats: name={module_name} {e}') -def apply_balanced_offload(sd_model=None, exclude:list[str]=None, force:bool=False, silent:bool=False): +def apply_balanced_offload(sd_model=None, exclude: list[str] | None = None, force: bool = False, silent: bool = False): global offload_hook_instance # pylint: disable=global-statement if shared.opts.diffusers_offload_mode != "balanced": return sd_model diff --git a/modules/sd_offload_aux.py b/modules/sd_offload_aux.py index 8578b8184..26c8c9fa7 100644 --- a/modules/sd_offload_aux.py +++ b/modules/sd_offload_aux.py @@ -33,7 +33,7 @@ def deregister_aux(name: str) -> None: debug_move(f'Offload: type=aux op=deregister name={name}') -def evict_aux(exclude: str = None, reason: str = 'evict') -> None: +def evict_aux(exclude: str | None = None, reason: str = 'evict') -> None: for name, entry in aux_models.items(): if name == exclude: continue diff --git a/modules/sd_te_remote.py b/modules/sd_te_remote.py index 36c920762..6a87df927 100644 --- a/modules/sd_te_remote.py +++ b/modules/sd_te_remote.py @@ -8,7 +8,7 @@ from modules.logger import log def get_t5_prompt_embeds( - prompt: str | list[str] = None, + prompt: str | list[str] | None = None, num_images_per_prompt: int = 1, # pylint: disable=unused-argument max_sequence_length: int = 512, # pylint: disable=unused-argument device: torch.device | None = None, diff --git a/modules/sd_unet.py b/modules/sd_unet.py index a3ac1c633..6b5d94545 100644 --- a/modules/sd_unet.py +++ b/modules/sd_unet.py @@ -35,11 +35,11 @@ def load_unet_sdxl_nunchaku(repo_id): return unet -def load_unet(model, repo_id:str=None): - global loaded_unet # pylint: disable=global-statement +def load_unet(model, repo_id: str | None = None): + global loaded_unet # pylint: disable=global-statement - if ("StableDiffusionXLPipeline" in model.__class__.__name__) and (('stable-diffusion-xl-base' in repo_id) or ('sdxl-turbo' in repo_id)): - if model_quant.check_nunchaku('Model'): + if ("StableDiffusionXLPipeline" in model.__class__.__name__) and repo_id is not None and (("stable-diffusion-xl-base" in repo_id) or ("sdxl-turbo" in repo_id)): + if model_quant.check_nunchaku("Model"): unet = load_unet_sdxl_nunchaku(repo_id) if unet is not None: model.unet = unet From c4ebef29a9dd70148af3229da1cdd63e5bce4be2 Mon Sep 17 00:00:00 2001 From: awsr <43862868+awsr@users.noreply.github.com> Date: Tue, 24 Mar 2026 05:48:19 -0700 Subject: [PATCH 20/25] RUF013 updates --- modules/prompt_parser_diffusers.py | 2 +- modules/sdnq/file_loader.py | 8 ++-- modules/sdnq/layers/conv/conv_fp16.py | 10 ++--- modules/sdnq/layers/conv/conv_fp8.py | 10 ++--- .../sdnq/layers/conv/conv_fp8_tensorwise.py | 10 ++--- modules/sdnq/layers/conv/conv_int8.py | 10 ++--- modules/sdnq/layers/conv/forward.py | 6 +-- modules/sdnq/layers/linear/linear_fp16.py | 10 ++--- modules/sdnq/layers/linear/linear_fp8.py | 10 ++--- .../layers/linear/linear_fp8_tensorwise.py | 10 ++--- modules/sdnq/layers/linear/linear_int8.py | 10 ++--- modules/sdnq/loader.py | 8 ++-- modules/sdnq/quantizer.py | 42 +++++++++---------- 13 files changed, 73 insertions(+), 73 deletions(-) diff --git a/modules/prompt_parser_diffusers.py b/modules/prompt_parser_diffusers.py index 93bfcbdab..8c7dd13ba 100644 --- a/modules/prompt_parser_diffusers.py +++ b/modules/prompt_parser_diffusers.py @@ -600,7 +600,7 @@ def split_prompts(pipe, prompt, SD3 = False): return prompt, prompt2, prompt3, prompt4 -def get_weighted_text_embeddings(pipe, prompt: str = "", neg_prompt: str = "", clip_skip: int = None, prompt_mean_norm=None, diffusers_zeros_prompt_pad=None, te_pooled_embeds=None): +def get_weighted_text_embeddings(pipe, prompt: str = "", neg_prompt: str = "", clip_skip: int | None = None, prompt_mean_norm=None, diffusers_zeros_prompt_pad=None, te_pooled_embeds=None): device = devices.device if prompt is None: prompt = '' diff --git a/modules/sdnq/file_loader.py b/modules/sdnq/file_loader.py index 8028627b8..4390745b7 100644 --- a/modules/sdnq/file_loader.py +++ b/modules/sdnq/file_loader.py @@ -13,7 +13,7 @@ def map_keys(key: str, key_mapping: dict) -> str: return new_key -def load_safetensors(files: list[str], state_dict: dict = None, key_mapping: dict = None, device: torch.device = "cpu") -> dict: +def load_safetensors(files: list[str], state_dict: dict | None = None, key_mapping: dict | None = None, device: torch.device = "cpu") -> dict: from safetensors.torch import safe_open if state_dict is None: state_dict = {} @@ -23,7 +23,7 @@ def load_safetensors(files: list[str], state_dict: dict = None, key_mapping: dic state_dict[map_keys(key, key_mapping)] = f.get_tensor(key) -def load_threaded(files: list[str], state_dict: dict = None, key_mapping: dict = None, device: torch.device = "cpu") -> dict: +def load_threaded(files: list[str], state_dict: dict | None = None, key_mapping: dict | None = None, device: torch.device = "cpu") -> dict: future_items = {} if state_dict is None: state_dict = {} @@ -34,7 +34,7 @@ def load_threaded(files: list[str], state_dict: dict = None, key_mapping: dict = future.result() -def load_streamer(files: list[str], state_dict: dict = None, key_mapping: dict = None, device: torch.device = "cpu") -> dict: +def load_streamer(files: list[str], state_dict: dict | None = None, key_mapping: dict | None = None, device: torch.device = "cpu") -> dict: # requires pip install runai_model_streamer from runai_model_streamer import SafetensorsStreamer if state_dict is None: @@ -45,7 +45,7 @@ def load_streamer(files: list[str], state_dict: dict = None, key_mapping: dict = state_dict[map_keys(key, key_mapping)] = tensor.to(device) -def load_files(files: list[str], state_dict: dict = None, key_mapping: dict = None, device: torch.device = "cpu", method: str = None) -> dict: +def load_files(files: list[str], state_dict: dict | None = None, key_mapping: dict | None = None, device: torch.device = "cpu", method: str | None = None) -> dict: # note: files is list-of-files within a module for chunked loading, not accross model if isinstance(files, str): files = [files] diff --git a/modules/sdnq/layers/conv/conv_fp16.py b/modules/sdnq/layers/conv/conv_fp16.py index 31beb017e..4f6cdecef 100644 --- a/modules/sdnq/layers/conv/conv_fp16.py +++ b/modules/sdnq/layers/conv/conv_fp16.py @@ -20,11 +20,11 @@ def conv_fp16_matmul( padding_mode: str, conv_type: int, groups: int, stride: list[int], padding: list[int], dilation: list[int], - bias: torch.FloatTensor = None, - svd_up: torch.FloatTensor = None, - svd_down: torch.FloatTensor = None, - quantized_weight_shape: torch.Size = None, - weights_dtype: str = None, + bias: torch.FloatTensor | None = None, + svd_up: torch.FloatTensor | None = None, + svd_down: torch.FloatTensor | None = None, + quantized_weight_shape: torch.Size | None = None, + weights_dtype: str | None = None, ) -> torch.FloatTensor: return_dtype = input.dtype input, mm_output_shape = process_conv_input(conv_type, input, reversed_padding_repeated_twice, padding_mode, result_shape, stride, padding, dilation) diff --git a/modules/sdnq/layers/conv/conv_fp8.py b/modules/sdnq/layers/conv/conv_fp8.py index 3595a5366..2099dfd18 100644 --- a/modules/sdnq/layers/conv/conv_fp8.py +++ b/modules/sdnq/layers/conv/conv_fp8.py @@ -19,11 +19,11 @@ def conv_fp8_matmul( padding_mode: str, conv_type: int, groups: int, stride: list[int], padding: list[int], dilation: list[int], - bias: torch.FloatTensor = None, - svd_up: torch.FloatTensor = None, - svd_down: torch.FloatTensor = None, - quantized_weight_shape: torch.Size = None, - weights_dtype: str = None, + bias: torch.FloatTensor | None = None, + svd_up: torch.FloatTensor | None = None, + svd_down: torch.FloatTensor | None = None, + quantized_weight_shape: torch.Size | None = None, + weights_dtype: str | None = None, ) -> torch.FloatTensor: return_dtype = input.dtype input, mm_output_shape = process_conv_input(conv_type, input, reversed_padding_repeated_twice, padding_mode, result_shape, stride, padding, dilation) diff --git a/modules/sdnq/layers/conv/conv_fp8_tensorwise.py b/modules/sdnq/layers/conv/conv_fp8_tensorwise.py index 38258bff7..2ad268eb8 100644 --- a/modules/sdnq/layers/conv/conv_fp8_tensorwise.py +++ b/modules/sdnq/layers/conv/conv_fp8_tensorwise.py @@ -20,11 +20,11 @@ def conv_fp8_matmul_tensorwise( padding_mode: str, conv_type: int, groups: int, stride: list[int], padding: list[int], dilation: list[int], - bias: torch.FloatTensor = None, - svd_up: torch.FloatTensor = None, - svd_down: torch.FloatTensor = None, - quantized_weight_shape: torch.Size = None, - weights_dtype: str = None, + bias: torch.FloatTensor | None = None, + svd_up: torch.FloatTensor | None = None, + svd_down: torch.FloatTensor | None = None, + quantized_weight_shape: torch.Size | None = None, + weights_dtype: str | None = None, ) -> torch.FloatTensor: return_dtype = input.dtype input, mm_output_shape = process_conv_input(conv_type, input, reversed_padding_repeated_twice, padding_mode, result_shape, stride, padding, dilation) diff --git a/modules/sdnq/layers/conv/conv_int8.py b/modules/sdnq/layers/conv/conv_int8.py index df54830ef..b5b2dcebf 100644 --- a/modules/sdnq/layers/conv/conv_int8.py +++ b/modules/sdnq/layers/conv/conv_int8.py @@ -20,11 +20,11 @@ def conv_int8_matmul( padding_mode: str, conv_type: int, groups: int, stride: list[int], padding: list[int], dilation: list[int], - bias: torch.FloatTensor = None, - svd_up: torch.FloatTensor = None, - svd_down: torch.FloatTensor = None, - quantized_weight_shape: torch.Size = None, - weights_dtype: str = None, + bias: torch.FloatTensor | None = None, + svd_up: torch.FloatTensor | None = None, + svd_down: torch.FloatTensor | None = None, + quantized_weight_shape: torch.Size | None = None, + weights_dtype: str | None = None, ) -> torch.FloatTensor: return_dtype = input.dtype input, mm_output_shape = process_conv_input(conv_type, input, reversed_padding_repeated_twice, padding_mode, result_shape, stride, padding, dilation) diff --git a/modules/sdnq/layers/conv/forward.py b/modules/sdnq/layers/conv/forward.py index 9f6336173..2504b9090 100644 --- a/modules/sdnq/layers/conv/forward.py +++ b/modules/sdnq/layers/conv/forward.py @@ -76,16 +76,16 @@ def quantized_conv_forward(self, input) -> torch.FloatTensor: return self._conv_forward(input, self.sdnq_dequantizer(self.weight, self.scale, self.zero_point, self.svd_up, self.svd_down), self.bias) -def quantized_conv_transpose_1d_forward(self, input: torch.FloatTensor, output_size: list[int] = None) -> torch.FloatTensor: +def quantized_conv_transpose_1d_forward(self, input: torch.FloatTensor, output_size: list[int] | None = None) -> torch.FloatTensor: output_padding = self._output_padding(input, output_size, self.stride, self.padding, self.kernel_size, 1, self.dilation) return torch.nn.functional.conv_transpose1d(input, self.sdnq_dequantizer(self.weight, self.scale, self.zero_point, self.svd_up, self.svd_down), self.bias, self.stride, self.padding, output_padding, self.groups, self.dilation) -def quantized_conv_transpose_2d_forward(self, input: torch.FloatTensor, output_size: list[int] = None) -> torch.FloatTensor: +def quantized_conv_transpose_2d_forward(self, input: torch.FloatTensor, output_size: list[int] | None = None) -> torch.FloatTensor: output_padding = self._output_padding(input, output_size, self.stride, self.padding, self.kernel_size, 2, self.dilation) return torch.nn.functional.conv_transpose2d(input, self.sdnq_dequantizer(self.weight, self.scale, self.zero_point, self.svd_up, self.svd_down), self.bias, self.stride, self.padding, output_padding, self.groups, self.dilation) -def quantized_conv_transpose_3d_forward(self, input: torch.FloatTensor, output_size: list[int] = None) -> torch.FloatTensor: +def quantized_conv_transpose_3d_forward(self, input: torch.FloatTensor, output_size: list[int] | None = None) -> torch.FloatTensor: output_padding = self._output_padding(input, output_size, self.stride, self.padding, self.kernel_size, 3, self.dilation) return torch.nn.functional.conv_transpose3d(input, self.sdnq_dequantizer(self.weight, self.scale, self.zero_point, self.svd_up, self.svd_down), self.bias, self.stride, self.padding, output_padding, self.groups, self.dilation) diff --git a/modules/sdnq/layers/linear/linear_fp16.py b/modules/sdnq/layers/linear/linear_fp16.py index 2999d09cb..705aaeb6f 100644 --- a/modules/sdnq/layers/linear/linear_fp16.py +++ b/modules/sdnq/layers/linear/linear_fp16.py @@ -14,11 +14,11 @@ def fp16_matmul( input: torch.FloatTensor, weight: torch.Tensor, scale: torch.FloatTensor, - bias: torch.FloatTensor = None, - svd_up: torch.FloatTensor = None, - svd_down: torch.FloatTensor = None, - quantized_weight_shape: torch.Size = None, - weights_dtype: str = None, + bias: torch.FloatTensor | None = None, + svd_up: torch.FloatTensor | None = None, + svd_down: torch.FloatTensor | None = None, + quantized_weight_shape: torch.Size | None = None, + weights_dtype: str | None = None, ) -> torch.FloatTensor: if quantized_weight_shape is not None: weight = unpack_float(weight, weights_dtype, quantized_weight_shape).to(dtype=torch.float16).t_() diff --git a/modules/sdnq/layers/linear/linear_fp8.py b/modules/sdnq/layers/linear/linear_fp8.py index c0b005b75..132dcb647 100644 --- a/modules/sdnq/layers/linear/linear_fp8.py +++ b/modules/sdnq/layers/linear/linear_fp8.py @@ -19,11 +19,11 @@ def fp8_matmul( input: torch.FloatTensor, weight: torch.Tensor, scale: torch.FloatTensor, - bias: torch.FloatTensor = None, - svd_up: torch.FloatTensor = None, - svd_down: torch.FloatTensor = None, - quantized_weight_shape: torch.Size = None, - weights_dtype: str = None, + bias: torch.FloatTensor | None = None, + svd_up: torch.FloatTensor | None = None, + svd_down: torch.FloatTensor | None = None, + quantized_weight_shape: torch.Size | None = None, + weights_dtype: str | None = None, ) -> torch.FloatTensor: if quantized_weight_shape is not None: weight = unpack_float(weight, weights_dtype, quantized_weight_shape).to(dtype=torch.float8_e4m3fn).t_() diff --git a/modules/sdnq/layers/linear/linear_fp8_tensorwise.py b/modules/sdnq/layers/linear/linear_fp8_tensorwise.py index 9977fbe7c..235dde48d 100644 --- a/modules/sdnq/layers/linear/linear_fp8_tensorwise.py +++ b/modules/sdnq/layers/linear/linear_fp8_tensorwise.py @@ -22,11 +22,11 @@ def fp8_matmul_tensorwise( input: torch.FloatTensor, weight: torch.Tensor, scale: torch.FloatTensor, - bias: torch.FloatTensor = None, - svd_up: torch.FloatTensor = None, - svd_down: torch.FloatTensor = None, - quantized_weight_shape: torch.Size = None, - weights_dtype: str = None, + bias: torch.FloatTensor | None = None, + svd_up: torch.FloatTensor | None = None, + svd_down: torch.FloatTensor | None = None, + quantized_weight_shape: torch.Size | None = None, + weights_dtype: str | None = None, ) -> torch.FloatTensor: if quantized_weight_shape is not None: weight = unpack_float(weight, weights_dtype, quantized_weight_shape).to(dtype=torch.float8_e4m3fn).t_() diff --git a/modules/sdnq/layers/linear/linear_int8.py b/modules/sdnq/layers/linear/linear_int8.py index 21eed8e10..e34222c5f 100644 --- a/modules/sdnq/layers/linear/linear_int8.py +++ b/modules/sdnq/layers/linear/linear_int8.py @@ -22,11 +22,11 @@ def int8_matmul( input: torch.FloatTensor, weight: torch.Tensor, scale: torch.FloatTensor, - bias: torch.FloatTensor = None, - svd_up: torch.FloatTensor = None, - svd_down: torch.FloatTensor = None, - quantized_weight_shape: torch.Size = None, - weights_dtype: str = None, + bias: torch.FloatTensor | None = None, + svd_up: torch.FloatTensor | None = None, + svd_down: torch.FloatTensor | None = None, + quantized_weight_shape: torch.Size | None = None, + weights_dtype: str | None = None, ) -> torch.FloatTensor: if quantized_weight_shape is not None: weight = unpack_int(weight, weights_dtype, quantized_weight_shape, dtype=torch.int8).t_() diff --git a/modules/sdnq/loader.py b/modules/sdnq/loader.py index 0edda40c0..1b3c5ae3e 100644 --- a/modules/sdnq/loader.py +++ b/modules/sdnq/loader.py @@ -25,7 +25,7 @@ def unset_config_on_save(quantization_config: SDNQConfig) -> SDNQConfig: return quantization_config -def save_sdnq_model(model: ModelMixin, model_path: str, max_shard_size: str = "5GB", is_pipeline: bool = False, sdnq_config: SDNQConfig = None) -> None: +def save_sdnq_model(model: ModelMixin, model_path: str, max_shard_size: str = "5GB", is_pipeline: bool = False, sdnq_config: SDNQConfig | None = None) -> None: if is_pipeline: for module_name in get_module_names(model): module = getattr(model, module_name, None) @@ -63,7 +63,7 @@ def save_sdnq_model(model: ModelMixin, model_path: str, max_shard_size: str = "5 model.config.quantization_config.to_json_file(quantization_config_path) -def load_sdnq_model(model_path: str, model_cls: ModelMixin = None, file_name: str = None, dtype: torch.dtype = None, device: torch.device = "cpu", dequantize_fp32: bool = None, use_quantized_matmul: bool = None, model_config: dict = None, quantization_config: dict = None, load_method: str = "safetensors") -> ModelMixin: +def load_sdnq_model(model_path: str, model_cls: ModelMixin | None = None, file_name: str | None = None, dtype: torch.dtype | None = None, device: torch.device = "cpu", dequantize_fp32: bool | None = None, use_quantized_matmul: bool | None = None, model_config: dict | None = None, quantization_config: dict | None = None, load_method: str = "safetensors") -> ModelMixin: from accelerate import init_empty_weights with init_empty_weights(): @@ -162,7 +162,7 @@ def post_process_model(model): return model -def apply_sdnq_options_to_module(model, dtype: torch.dtype = None, dequantize_fp32: bool = None, use_quantized_matmul: bool = None): +def apply_sdnq_options_to_module(model, dtype: torch.dtype | None = None, dequantize_fp32: bool | None = None, use_quantized_matmul: bool | None = None): has_children = list(model.children()) if not has_children: if dtype is not None and getattr(model, "dtype", torch.float32) not in {torch.float32, torch.float64}: @@ -231,7 +231,7 @@ def apply_sdnq_options_to_module(model, dtype: torch.dtype = None, dequantize_fp return model -def apply_sdnq_options_to_model(model, dtype: torch.dtype = None, dequantize_fp32: bool = None, use_quantized_matmul: bool = None): +def apply_sdnq_options_to_model(model, dtype: torch.dtype | None = None, dequantize_fp32: bool | None = None, use_quantized_matmul: bool | None = None): if use_quantized_matmul and not check_torch_compile(): raise RuntimeError("SDNQ Quantized MatMul requires a working Triton install.") model = apply_sdnq_options_to_module(model, dtype=dtype, dequantize_fp32=dequantize_fp32, use_quantized_matmul=use_quantized_matmul) diff --git a/modules/sdnq/quantizer.py b/modules/sdnq/quantizer.py index e4b462ad0..13c89d77a 100644 --- a/modules/sdnq/quantizer.py +++ b/modules/sdnq/quantizer.py @@ -189,7 +189,7 @@ def get_quant_kwargs(quant_kwargs: dict, modules_quant_config: dict[str, dict]) return quant_kwargs -def add_module_skip_keys(model, modules_to_not_convert: list[str] = None, modules_dtype_dict: dict[str, list[str]] = None): +def add_module_skip_keys(model, modules_to_not_convert: list[str] | None = None, modules_dtype_dict: dict[str, list[str]] | None = None): if modules_to_not_convert is None: modules_to_not_convert = [] if modules_dtype_dict is None: @@ -547,7 +547,7 @@ def sdnq_quantize_layer(layer, weights_dtype="int8", quantized_matmul_dtype=None @devices.inference_context() -def apply_sdnq_to_module(model, weights_dtype="int8", quantized_matmul_dtype=None, torch_dtype=None, group_size=0, svd_rank=32, svd_steps=8, dynamic_loss_threshold=1e-2, use_svd=False, quant_conv=False, use_quantized_matmul=False, use_quantized_matmul_conv=False, use_dynamic_quantization=False, use_stochastic_rounding=False, dequantize_fp32=True, non_blocking=False, modules_to_not_convert: list[str] = None, modules_dtype_dict: dict[str, list[str]] = None, modules_quant_config: dict[str, dict] = None, quantization_device=None, return_device=None, full_param_name=""): # pylint: disable=unused-argument +def apply_sdnq_to_module(model, weights_dtype="int8", quantized_matmul_dtype=None, torch_dtype=None, group_size=0, svd_rank=32, svd_steps=8, dynamic_loss_threshold=1e-2, use_svd=False, quant_conv=False, use_quantized_matmul=False, use_quantized_matmul_conv=False, use_dynamic_quantization=False, use_stochastic_rounding=False, dequantize_fp32=True, non_blocking=False, modules_to_not_convert: list[str] | None = None, modules_dtype_dict: dict[str, list[str]] | None = None, modules_quant_config: dict[str, dict] | None = None, quantization_device=None, return_device=None, full_param_name=""): # pylint: disable=unused-argument has_children = list(model.children()) if not has_children: return model, modules_to_not_convert, modules_dtype_dict @@ -628,8 +628,8 @@ def apply_sdnq_to_module(model, weights_dtype="int8", quantized_matmul_dtype=Non def sdnq_post_load_quant( model: torch.nn.Module, weights_dtype: str = "int8", - quantized_matmul_dtype: str = None, - torch_dtype: torch.dtype = None, + quantized_matmul_dtype: str | None = None, + torch_dtype: torch.dtype | None = None, group_size: int = 0, svd_rank: int = 32, svd_steps: int = 8, @@ -643,11 +643,11 @@ def sdnq_post_load_quant( dequantize_fp32: bool = True, non_blocking: bool = False, add_skip_keys:bool = True, - quantization_device: torch.device = None, - return_device: torch.device = None, - modules_to_not_convert: list[str] = None, - modules_dtype_dict: dict[str, list[str]] = None, - modules_quant_config: dict[str, dict] = None, + quantization_device: torch.device | None = None, + return_device: torch.device | None = None, + modules_to_not_convert: list[str] | None = None, + modules_dtype_dict: dict[str, list[str]] | None = None, + modules_quant_config: dict[str, dict] | None = None, ): if modules_to_not_convert is None: modules_to_not_convert = [] @@ -735,9 +735,9 @@ class SDNQQuantize: def convert( self, input_dict: dict[str, list[torch.Tensor]], - model: torch.nn.Module = None, - full_layer_name: str = None, - missing_keys: list[str] = None, # pylint: disable=unused-argument + model: torch.nn.Module | None = None, + full_layer_name: str | None = None, + missing_keys: list[str] | None = None, # pylint: disable=unused-argument **kwargs, # pylint: disable=unused-argument ) -> dict[str, torch.FloatTensor]: _module_name, value = tuple(input_dict.items())[0] @@ -898,11 +898,11 @@ class SDNQQuantizer(DiffusersQuantizer, HfQuantizer): def adjust_target_dtype(self, target_dtype: torch.dtype) -> torch.dtype: # pylint: disable=unused-argument,arguments-renamed return dtype_dict[self.quantization_config.weights_dtype]["target_dtype"] - def update_torch_dtype(self, torch_dtype: torch.dtype = None) -> torch.dtype: + def update_torch_dtype(self, torch_dtype: torch.dtype | None = None) -> torch.dtype: self.torch_dtype = torch_dtype return torch_dtype - def update_dtype(self, dtype: torch.dtype = None) -> torch.dtype: + def update_dtype(self, dtype: torch.dtype | None = None) -> torch.dtype: """ needed for transformers compatibilty, returns self.update_torch_dtype """ @@ -912,7 +912,7 @@ class SDNQQuantizer(DiffusersQuantizer, HfQuantizer): self, model, device_map, # pylint: disable=unused-argument - keep_in_fp32_modules: list[str] = None, + keep_in_fp32_modules: list[str] | None = None, **kwargs, # pylint: disable=unused-argument ): if self.pre_quantized: @@ -1055,7 +1055,7 @@ class SDNQConfig(QuantizationConfigMixin): def __init__( # pylint: disable=super-init-not-called self, weights_dtype: str = "int8", - quantized_matmul_dtype: str = None, + quantized_matmul_dtype: str | None = None, group_size: int = 0, svd_rank: int = 32, svd_steps: int = 8, @@ -1071,11 +1071,11 @@ class SDNQConfig(QuantizationConfigMixin): dequantize_fp32: bool = True, non_blocking: bool = False, add_skip_keys: bool = True, - quantization_device: torch.device = None, - return_device: torch.device = None, - modules_to_not_convert: list[str] = None, - modules_dtype_dict: dict[str, list[str]] = None, - modules_quant_config: dict[str, dict] = None, + quantization_device: torch.device | None = None, + return_device: torch.device | None = None, + modules_to_not_convert: list[str] | None = None, + modules_dtype_dict: dict[str, list[str]] | None = None, + modules_quant_config: dict[str, dict] | None = None, is_training: bool = False, **kwargs, # pylint: disable=unused-argument ): From 09ab19c43883765b6a487853bb2c889619278add Mon Sep 17 00:00:00 2001 From: awsr <43862868+awsr@users.noreply.github.com> Date: Tue, 24 Mar 2026 06:07:34 -0700 Subject: [PATCH 21/25] RUF013 updates and formatting --- modules/shared_state.py | 4 +- modules/taesd/hybrid_small.py | 4 +- modules/textual_inversion.py | 2 +- modules/todo/todo_merge.py | 2 +- modules/ui_control.py | 4 +- modules/ui_sections.py | 4 +- modules/ui_video_vlm.py | 2 +- modules/vae/sd_vae_fal.py | 8 ++-- modules/video_models/video_save.py | 59 +++++++++++++++--------------- 9 files changed, 45 insertions(+), 44 deletions(-) diff --git a/modules/shared_state.py b/modules/shared_state.py index f1ac2b975..8357cea4d 100644 --- a/modules/shared_state.py +++ b/modules/shared_state.py @@ -148,7 +148,7 @@ class State: return job return None - def history(self, op:str, task_id:str=None, results:list=None): + def history(self, op: str, task_id: str | None = None, results: list | None = None): if results is None: results = [] job = { @@ -174,7 +174,7 @@ class State: if len(self.results) > 0: self.history('output', self.id, results=self.results) - def get_id(self, task_id:str=None): + def get_id(self, task_id: str | None = None): if task_id is None or task_id == 0: task_id = uuid.uuid4().hex[:15] if not isinstance(task_id, str): diff --git a/modules/taesd/hybrid_small.py b/modules/taesd/hybrid_small.py index 8ca1135ab..964541d2c 100644 --- a/modules/taesd/hybrid_small.py +++ b/modules/taesd/hybrid_small.py @@ -76,8 +76,8 @@ class AutoencoderSmall(ModelMixin, ConfigMixin, FromOriginalModelMixin): down_block_types: tuple[str] = ("DownEncoderBlock2D",), up_block_types: tuple[str] = ("UpDecoderBlock2D",), block_out_channels: tuple[int] = (64,), - encoder_block_out_channels: tuple[int] = None, - decoder_block_out_channels: tuple[int] = None, + encoder_block_out_channels: tuple[int] | None = None, + decoder_block_out_channels: tuple[int] | None = None, layers_per_block: int = 1, act_fn: str = "silu", latent_channels: int = 4, diff --git a/modules/textual_inversion.py b/modules/textual_inversion.py index 32ca1a54e..de2d22278 100644 --- a/modules/textual_inversion.py +++ b/modules/textual_inversion.py @@ -254,7 +254,7 @@ class EmbeddingDatabase: self.ids_lookup[first_id] = sorted(self.ids_lookup[first_id] + [(ids, embedding)], key=lambda x: len(x[0]), reverse=True) return embedding - def load_diffusers_embedding(self, filename: str | list[str] = None, data: dict = None): + def load_diffusers_embedding(self, filename: str | list[str] | None = None, data: dict | None = None): """ File names take precidence over bundled embeddings passed as a dict. Bundled embeddings are automatically set to overwrite previous embeddings. diff --git a/modules/todo/todo_merge.py b/modules/todo/todo_merge.py index cde8381fe..bc03b96c7 100644 --- a/modules/todo/todo_merge.py +++ b/modules/todo/todo_merge.py @@ -38,7 +38,7 @@ def init_generator(device: torch.device, fallback: torch.Generator = None): return fallback -def do_nothing(x: torch.Tensor, mode: str = None): # pylint: disable=unused-argument +def do_nothing(x: torch.Tensor, mode: str | None = None): # pylint: disable=unused-argument return x diff --git a/modules/ui_control.py b/modules/ui_control.py index 9be9bc3b3..da4784403 100644 --- a/modules/ui_control.py +++ b/modules/ui_control.py @@ -19,7 +19,7 @@ debug('Trace: CONTROL') use_generator = os.environ.get('SD_USE_GENERATOR', None) is not None -def return_stats(t: float = None): +def return_stats(t: float | None = None): if t is None: elapsed_text = '' else: @@ -48,7 +48,7 @@ def return_stats(t: float = None): return f"

{elapsed_text} {summary} {gpu} {cpu}

" -def return_controls(res, t: float = None): +def return_controls(res, t: float | None = None): # return preview, image, video, gallery, text debug(f'Control received: type={type(res)} {res}') if t is None: diff --git a/modules/ui_sections.py b/modules/ui_sections.py index 886e392a2..32b71b193 100644 --- a/modules/ui_sections.py +++ b/modules/ui_sections.py @@ -5,7 +5,7 @@ from modules.ui_components import ToolButton from modules.caption import caption -def create_toprow(is_img2img: bool = False, id_part: str = None, generate_visible: bool = True, negative_visible: bool = True, reprocess_visible: bool = True): +def create_toprow(is_img2img: bool = False, id_part: str | None = None, generate_visible: bool = True, negative_visible: bool = True, reprocess_visible: bool = True): def apply_styles(prompt, prompt_neg, styles): prompt = shared.prompt_styles.apply_styles_to_prompt(prompt, styles, wildcards=not shared.opts.extra_networks_apply_unparsed) prompt_neg = shared.prompt_styles.apply_negative_styles_to_prompt(prompt_neg, styles, wildcards=not shared.opts.extra_networks_apply_unparsed) @@ -92,7 +92,7 @@ def create_resolution_inputs(tab, default_width=1024, default_height=1024): return width, height -def create_caption_button(tab: str, inputs: list = None, outputs: str = None, what: str = ''): +def create_caption_button(tab: str, inputs: list | None = None, outputs: str | None = None, what: str = ''): button_caption = gr.Button(ui_symbols.caption, elem_id=f"{tab}_caption_{what}", elem_classes=['caption']) if inputs is not None and outputs is not None: button_caption.click(fn=caption.caption, inputs=inputs, outputs=[outputs]) diff --git a/modules/ui_video_vlm.py b/modules/ui_video_vlm.py index 4e702c771..da3ed1ec7 100644 --- a/modules/ui_video_vlm.py +++ b/modules/ui_video_vlm.py @@ -21,7 +21,7 @@ system_prompts = { } -def enhance_prompt(enable:bool, model:str=None, image=None, prompt:str='', system_prompt:str='', nsfw:bool=True): +def enhance_prompt(enable: bool, model: str | None = None, image=None, prompt: str = "", system_prompt: str = "", nsfw: bool = True): from modules.caption import vqa if not enable: return prompt diff --git a/modules/vae/sd_vae_fal.py b/modules/vae/sd_vae_fal.py index d132fa77e..dc8042d1b 100644 --- a/modules/vae/sd_vae_fal.py +++ b/modules/vae/sd_vae_fal.py @@ -50,12 +50,12 @@ class Flux2TinyAutoEncoder(ModelMixin, ConfigMixin): in_channels: int = 3, out_channels: int = 3, latent_channels: int = 128, - encoder_block_out_channels: list[int] = None, - decoder_block_out_channels: list[int] = None, + encoder_block_out_channels: list[int] | None = None, + decoder_block_out_channels: list[int] | None = None, act_fn: str = "silu", upsampling_scaling_factor: int = 2, - num_encoder_blocks: list[int] = None, - num_decoder_blocks: list[int] = None, + num_encoder_blocks: list[int] | None = None, + num_decoder_blocks: list[int] | None = None, latent_magnitude: float = 3.0, latent_shift: float = 0.5, force_upcast: bool = False, diff --git a/modules/video_models/video_save.py b/modules/video_models/video_save.py index 175bc3433..bd67c321f 100644 --- a/modules/video_models/video_save.py +++ b/modules/video_models/video_save.py @@ -29,7 +29,7 @@ def get_video_filename(p:processing.StableDiffusionProcessingVideo): return filename -def save_params(p, filename: str = None): +def save_params(p, filename: str | None = None): from modules.paths import params_path if p is None: dct = {} @@ -129,17 +129,18 @@ def write_audio( container.mux(packet) -def atomic_save_video(filename: str, - tensor:torch.Tensor, - audio:torch.Tensor=None, - fps:float=24, - codec:str='libx264', - pix_fmt:str='yuv420p', - options:str='', - aac:int=24000, - metadata:dict=None, - pbar=None, - ): +def atomic_save_video( + filename: str, + tensor: torch.Tensor, + audio: torch.Tensor | None = None, + fps: float = 24, + codec: str = "libx264", + pix_fmt: str = "yuv420p", + options: str = "", + aac: int = 24000, + metadata: dict | None = None, + pbar=None, +): if metadata is None: metadata = {} av = check_av() @@ -212,23 +213,23 @@ def save_thumbnail(video_path, tensor=None): def save_video( - p:processing.StableDiffusionProcessingVideo, - pixels:torch.Tensor=None, - audio:torch.Tensor=None, - binary:bytes=None, - mp4_fps:int=24, - mp4_codec:str='libx264', - mp4_opt:str='', - mp4_ext:str='mp4', - mp4_sf:bool=False, # save safetensors - mp4_video:bool=True, # save video - mp4_frames:bool=False, # save frames - mp4_interpolate:int=0, # rife interpolation - aac_sample_rate:int=24000, # audio sample rate - stream=None, # async progress reporting stream - metadata:dict=None, # metadata for video - pbar=None, # progress bar for video - ): + p: processing.StableDiffusionProcessingVideo, + pixels: torch.Tensor | None = None, + audio: torch.Tensor | None = None, + binary: bytes | None = None, + mp4_fps: int = 24, + mp4_codec: str = "libx264", + mp4_opt: str = "", + mp4_ext: str = "mp4", + mp4_sf: bool = False, # save safetensors + mp4_video: bool = True, # save video + mp4_frames: bool = False, # save frames + mp4_interpolate: int = 0, # rife interpolation + aac_sample_rate: int = 24000, # audio sample rate + stream=None, # async progress reporting stream + metadata: dict | None = None, # metadata for video + pbar=None, # progress bar for video +): if metadata is None: metadata = {} output_video = None From 1003926646a1b3a8c39d3fb3924334a89b7ab24f Mon Sep 17 00:00:00 2001 From: awsr <43862868+awsr@users.noreply.github.com> Date: Tue, 24 Mar 2026 06:08:15 -0700 Subject: [PATCH 22/25] RUF013 updates and import updates --- modules/upscaler.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/modules/upscaler.py b/modules/upscaler.py index 44dfad48d..6bafa3db0 100644 --- a/modules/upscaler.py +++ b/modules/upscaler.py @@ -1,10 +1,16 @@ +from __future__ import annotations + import os from abc import abstractmethod +from typing import TYPE_CHECKING from PIL import Image from modules import modelloader, shared, paths from modules.logger import log +if TYPE_CHECKING: + from torch import Tensor + models = None @@ -92,10 +98,10 @@ class Upscaler: return scalers @abstractmethod - def do_upscale(self, img: Image, selected_model: str): + def do_upscale(self, img: Image.Image | Tensor, selected_model: str): return img - def upscale(self, img: Image, scale, selected_model: str = None): + def upscale(self, img: Image.Image | Tensor, scale, selected_model: str | None = None): jobid = shared.state.begin('Upscale') self.scale = scale if isinstance(img, Image.Image): @@ -153,10 +159,10 @@ class UpscalerData: name = None data_path = None scale: int = 4 - scaler: Upscaler = None + scaler: Upscaler | None = None model: None - def __init__(self, name: str, path: str = None, upscaler: Upscaler = None, scale: int = 4, model=None): + def __init__(self, name: str, path: str | None = None, upscaler: Upscaler | None = None, scale: int = 4, model=None): self.name = name self.data_path = path self.local_data_path = path From 876e3b9897f959dfb0fd98190f485c8376f1ff8e Mon Sep 17 00:00:00 2001 From: awsr <43862868+awsr@users.noreply.github.com> Date: Tue, 24 Mar 2026 06:15:26 -0700 Subject: [PATCH 23/25] RUF013 updates --- modules/apg/pipeline_stable_cascade_prior_apg.py | 4 ++-- modules/apg/pipeline_stable_diffision_xl_apg.py | 8 ++++---- modules/apg/pipeline_stable_diffusion_apg.py | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/modules/apg/pipeline_stable_cascade_prior_apg.py b/modules/apg/pipeline_stable_cascade_prior_apg.py index 6ffaf8089..c4747612d 100644 --- a/modules/apg/pipeline_stable_cascade_prior_apg.py +++ b/modules/apg/pipeline_stable_cascade_prior_apg.py @@ -372,7 +372,7 @@ class StableCascadePriorPipelineAPG(DiffusionPipeline): height: int = 1024, width: int = 1024, num_inference_steps: int = 20, - timesteps: list[float] = None, + timesteps: list[float] | None = None, guidance_scale: float = 4.0, negative_prompt: str | list[str] | None = None, prompt_embeds: torch.Tensor | None = None, @@ -386,7 +386,7 @@ class StableCascadePriorPipelineAPG(DiffusionPipeline): output_type: str | None = "pt", return_dict: bool = True, callback_on_step_end: Callable[[int, int, dict], None] | None = None, - callback_on_step_end_tensor_inputs: list[str] = None, + callback_on_step_end_tensor_inputs: list[str] | None = None, ): """ Function invoked when calling the pipeline for generation. diff --git a/modules/apg/pipeline_stable_diffision_xl_apg.py b/modules/apg/pipeline_stable_diffision_xl_apg.py index b09ae242f..5da74c299 100644 --- a/modules/apg/pipeline_stable_diffision_xl_apg.py +++ b/modules/apg/pipeline_stable_diffision_xl_apg.py @@ -793,13 +793,13 @@ class StableDiffusionXLPipelineAPG( @replace_example_docstring(EXAMPLE_DOC_STRING) def __call__( self, - prompt: str | list[str] = None, + prompt: str | list[str] | None = None, prompt_2: str | list[str] | None = None, height: int | None = None, width: int | None = None, num_inference_steps: int = 50, - timesteps: list[int] = None, - sigmas: list[float] = None, + timesteps: list[int] | None = None, + sigmas: list[float] | None = None, denoising_end: float | None = None, guidance_scale: float = 5.0, negative_prompt: str | list[str] | None = None, @@ -826,7 +826,7 @@ class StableDiffusionXLPipelineAPG( negative_target_size: tuple[int, int] | None = None, clip_skip: int | None = None, callback_on_step_end: Callable[[int, int, dict], None] | PipelineCallback | MultiPipelineCallbacks | None = None, - callback_on_step_end_tensor_inputs: list[str] = None, + callback_on_step_end_tensor_inputs: list[str] | None = None, **kwargs, ): r""" diff --git a/modules/apg/pipeline_stable_diffusion_apg.py b/modules/apg/pipeline_stable_diffusion_apg.py index ae1eb26e0..57aebafca 100644 --- a/modules/apg/pipeline_stable_diffusion_apg.py +++ b/modules/apg/pipeline_stable_diffusion_apg.py @@ -749,12 +749,12 @@ class StableDiffusionPipelineAPG( @replace_example_docstring(EXAMPLE_DOC_STRING) def __call__( self, - prompt: str | list[str] = None, + prompt: str | list[str] | None = None, height: int | None = None, width: int | None = None, num_inference_steps: int = 50, - timesteps: list[int] = None, - sigmas: list[float] = None, + timesteps: list[int] | None = None, + sigmas: list[float] | None = None, guidance_scale: float = 7.5, negative_prompt: str | list[str] | None = None, num_images_per_prompt: int | None = 1, @@ -771,7 +771,7 @@ class StableDiffusionPipelineAPG( guidance_rescale: float = 0.0, clip_skip: int | None = None, callback_on_step_end: Callable[[int, int, dict], None] | PipelineCallback | MultiPipelineCallbacks | None = None, - callback_on_step_end_tensor_inputs: list[str] = None, + callback_on_step_end_tensor_inputs: list[str] | None = None, **kwargs, ): r""" From a6f6a37dea8a2ac9d0e936e7f830b7564ae25ca4 Mon Sep 17 00:00:00 2001 From: awsr <43862868+awsr@users.noreply.github.com> Date: Tue, 24 Mar 2026 06:16:13 -0700 Subject: [PATCH 24/25] Add Ruff per-file-ignores --- pyproject.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 57db5b62f..b339015a1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -107,6 +107,9 @@ fixable = ["ALL"] unfixable = [] dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$" +[tool.ruff.lint.per-file-ignores] +"modules/caption/joytag.py" = ["RUF013"] # Per header comment: "Do not modify directly — sync from upstream" + [tool.ruff.format] quote-style = "double" indent-style = "space" From fd1646cc37f66773a8f2eda9c56736e4f737b4ea Mon Sep 17 00:00:00 2001 From: awsr <43862868+awsr@users.noreply.github.com> Date: Tue, 24 Mar 2026 06:28:39 -0700 Subject: [PATCH 25/25] Revert extra return statement --- modules/image/grid.py | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/image/grid.py b/modules/image/grid.py index 9ba8801fe..0c8745dbd 100644 --- a/modules/image/grid.py +++ b/modules/image/grid.py @@ -54,7 +54,6 @@ def get_grid_size(imgs, batch_size=1, rows: int | None = None, cols: int | None while len(imgs) % rows != 0: rows -= 1 cols = math.ceil(len(imgs) / rows) - return rows, cols elif rows is not None and cols is None: cols = math.ceil(len(imgs) / rows) elif rows is None and cols is not None: