From c80b1ebc367ff4296406abc2004f6a7ab23671d0 Mon Sep 17 00:00:00 2001 From: Vladimir Mandic Date: Tue, 27 Jun 2023 10:28:47 -0400 Subject: [PATCH] stricter lint rules --- .github/workflows/on_pull_request.yaml | 11 +++++-- .pylintrc | 2 ++ installer.py | 2 +- modules/extras.py | 2 +- modules/processing.py | 2 +- modules/safe.py | 6 ++-- modules/scripts.py | 2 +- modules/ui.py | 6 ++-- modules/ui_extra_networks.py | 2 +- pyproject.toml | 44 +++++++++++++++++++------- 10 files changed, 54 insertions(+), 25 deletions(-) diff --git a/.github/workflows/on_pull_request.yaml b/.github/workflows/on_pull_request.yaml index c6111da58..924ab3266 100644 --- a/.github/workflows/on_pull_request.yaml +++ b/.github/workflows/on_pull_request.yaml @@ -16,11 +16,16 @@ jobs: python-version: 3.10.6 cache: pip cache-dependency-path: requirements.txt - - name: run-lint + - name: run-ruff run: | python -m pip install --upgrade pip - pip install pylint - pylint $(git ls-files '*.py') + pip install ruff + ruff . + # - name: run-pylint + # run: | + # python -m pip install --upgrade pip + # pip install pylint + # pylint $(git ls-files '*.py') - name: test-startup run: | export COMMANDLINE_ARGS="--debug --test" diff --git a/.pylintrc b/.pylintrc index d4ff272de..68a2f4093 100644 --- a/.pylintrc +++ b/.pylintrc @@ -13,6 +13,8 @@ ignore-paths=^repositories/.*$, ^extensions/.*$, ^extensions-builtin/.*$, /usr/lib/.*$, + ^modules/dml/.*$, + ^modules/models/diffusion/.*$, ignore-patterns= ignored-modules= jobs=0 diff --git a/installer.py b/installer.py index 1e07c5922..c48d28fbb 100644 --- a/installer.py +++ b/installer.py @@ -381,7 +381,7 @@ def check_modified_files(): try: res = git('status --porcelain') files = [x[2:].strip() for x in res.split('\n')] - files = [x for x in files if len(x) > 0 and (not x.startswith('extensions')) and (not x.startswith('wiki')) and (not x.endswith('.json')) and (not '.log' in x)] + files = [x for x in files if len(x) > 0 and (not x.startswith('extensions')) and (not x.startswith('wiki')) and (not x.endswith('.json')) and ('.log' not in x)] if len(files) > 0: log.warning(f'Modified files: {files}') except Exception: diff --git a/modules/extras.py b/modules/extras.py index a26908cf5..f13b61306 100644 --- a/modules/extras.py +++ b/modules/extras.py @@ -350,7 +350,7 @@ def run_modelconvert(model, checkpoint_formats, precision, conv_type, custom_nam ema_k = "___" try: ema_k = "model_ema." + k[6:].replace(".", "") - except: + except Exception: pass if ema_k in state_dict: _hf(k, state_dict[ema_k]) diff --git a/modules/processing.py b/modules/processing.py index 3d276e290..6d05beed4 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -239,7 +239,7 @@ class StableDiffusionProcessing: pass def sample(self, conditioning, unconditional_conditioning, seeds, subseeds, subseed_strength, prompts): - raise NotImplementedError() + raise NotImplementedError def close(self): self.sampler = None # pylint: disable=attribute-defined-outside-init diff --git a/modules/safe.py b/modules/safe.py index c68e87aaa..b3b98897e 100644 --- a/modules/safe.py +++ b/modules/safe.py @@ -6,7 +6,7 @@ import zipfile import re import torch -import numpy +import numpy as np import _codecs # PyTorch 1.13 and later have _TypedStorage renamed to TypedStorage @@ -43,9 +43,9 @@ class RestrictedUnpickler(pickle.Unpickler): if module == 'torch.nn.modules.container' and name in ['ParameterDict']: return getattr(torch.nn.modules.container, name) if module == 'numpy.core.multiarray' and name in ['scalar', '_reconstruct']: - return getattr(numpy.core.multiarray, name) + return getattr(np.core.multiarray, name) if module == 'numpy' and name in ['dtype', 'ndarray']: - return getattr(numpy, name) + return getattr(np, name) if module == '_codecs' and name == 'encode': return encode if module == "pytorch_lightning.callbacks" and name == 'model_checkpoint': diff --git a/modules/scripts.py b/modules/scripts.py index 1ee340b44..55baa562b 100644 --- a/modules/scripts.py +++ b/modules/scripts.py @@ -33,7 +33,7 @@ class Script: def title(self): """this function should return the title of the script. This is what will be displayed in the dropdown menu.""" - raise NotImplementedError() + raise NotImplementedError def ui(self, is_img2img): """this function should create gradio UI elements. See https://gradio.app/docs/#components diff --git a/modules/ui.py b/modules/ui.py index 5b74223b0..d90c12d1a 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -556,9 +556,9 @@ def create_ui(): with gr.TabItem('Batch', id='batch', elem_id="img2img_batch_tab") as tab_batch: hidden = '
Disabled when launched with --hide-ui-dir-config.' if modules.shared.cmd_opts.hide_ui_dir_config else '' gr.HTML( - "

Process images in a directory on the same machine where the server is running." + - "
Use an empty output directory to save pictures normally instead of writing to the output directory." + - "
Add inpaint batch mask directory to enable inpaint batch processing." + "

Process images in a directory on the same machine where the server is running" + + "
Use an empty output directory to save pictures normally instead of writing to the output directory" + + "
Add inpaint batch mask directory to enable inpaint batch processing" f"{hidden}

" ) img2img_batch_input_dir = gr.Textbox(label="Inpaint batch input directory", **modules.shared.hide_dirs, elem_id="img2img_batch_input_dir") diff --git a/modules/ui_extra_networks.py b/modules/ui_extra_networks.py index 977b09bca..6d86ff30c 100644 --- a/modules/ui_extra_networks.py +++ b/modules/ui_extra_networks.py @@ -143,7 +143,7 @@ class ExtraNetworksPage: return '' def list_items(self): - raise NotImplementedError() + raise NotImplementedError def allowed_directories_for_previews(self): return [] diff --git a/pyproject.toml b/pyproject.toml index c2a238aed..36978b354 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,25 +1,44 @@ [tool.ruff] - -target-version = "py39" - -extend-select = [ - "B", - "C", - "I", +target-version = "py310" +select = [ + "F", + "E", "W", + "C", + "B", + "I", + "YTT", + "ASYNC", + "RUF", + "AIR", + "NPY", + "C4", + "T10", + "EXE", + "ISC", + "ICN", + "RSE", + "TCH", + "TID", + "INT", + "PLE", + # "FIX", + # "A", + # "T20", + # "S", + # "PL", ] - exclude = [ "extensions", - "extensions-disabled", "extensions-builtin", "modules/lora", "modules/lycoris", "modules/dml", + "modules/models/diffusion", ] - ignore = [ - "C408", # Rewrite as a literal + "A003", # Class attirbute shadowing builtin + "C408", # Rewrite as a literal "C901", # Function is too complex "E501", # Line too long "E731", # Do not assign a `lambda` expression, use a `def` @@ -27,6 +46,9 @@ ignore = [ "W605", # invalid escape sequence, messes with some docstrings "E402", # Module level import not at top of file "F401", # Imported but unused + "B905", # Without explicit scrict + "RUF005", # Consider concatenation + "ISC003", # Implicit string concatenation ] [tool.ruff.flake8-bugbear]