From 45e603cdc77959fa59a703f99cdce9a3bd83710e Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Fri, 1 Dec 2023 00:57:26 -0600 Subject: [PATCH] Update ruff and typing (#213) --- .github/actions/check-links/check_links.py | 12 +++--- .../create_constraints_file.py | 3 +- .github/actions/pr-script/pr_script.py | 10 ++--- .pre-commit-config.yaml | 10 ++--- foobar.py | 1 - pyproject.toml | 43 ++++++++++++------- 6 files changed, 45 insertions(+), 34 deletions(-) diff --git a/.github/actions/check-links/check_links.py b/.github/actions/check-links/check_links.py index 584f11a..8714ea5 100644 --- a/.github/actions/check-links/check_links.py +++ b/.github/actions/check-links/check_links.py @@ -29,7 +29,7 @@ def check_links(ignore_glob: list[str], ignore_links: list[str], links_expire: s ignored = [] for spec in ignore_glob: cmd += f' --ignore-glob "{spec}"' - ignored.extend(glob(spec, recursive=True)) + ignored.extend(glob(spec, recursive=True)) # noqa: PTH207 ignore_links = [ *list(ignore_links), @@ -48,7 +48,7 @@ def check_links(ignore_glob: list[str], ignore_links: list[str], links_expire: s # Gather all of the markdown, RST, and ipynb files files: list[str] = [] for ext in [".md", ".rst", ".ipynb"]: - matched = glob(f"**/*{ext}", recursive=True) + matched = glob(f"**/*{ext}", recursive=True) # noqa: PTH207 files.extend(m for m in matched if m not in ignored and "node_modules" not in m) separator = f"\n\n{'-' * 80}\n" @@ -61,17 +61,17 @@ def check_links(ignore_glob: list[str], ignore_links: list[str], links_expire: s file_cmd = shlex.split(file_cmd_str) try: log(f"{separator}{f}...") - subprocess.check_output(file_cmd, shell=False) # noqa S603 + subprocess.check_output(file_cmd, shell=False) # noqa: S603 except Exception as e: # Return code 5 means no tests were run (no links found) - if e.returncode != 5: # type:ignore[attr-defined] # noqa + if e.returncode != 5: # type:ignore[attr-defined] try: log(f"\n{f} (second attempt)...\n") - subprocess.check_output([*file_cmd, "--lf"], shell=False) # noqa S603 + subprocess.check_output([*file_cmd, "--lf"], shell=False) # noqa: S603 except subprocess.CalledProcessError as e: log(e.output.decode("utf-8")) fails += 1 - if fails == 3: # noqa + if fails == 3: msg = "Found three failed links, bailing" raise RuntimeError(msg) from e if fails: diff --git a/.github/actions/install-minimums/create_constraints_file.py b/.github/actions/install-minimums/create_constraints_file.py index c7ac821..2d26770 100644 --- a/.github/actions/install-minimums/create_constraints_file.py +++ b/.github/actions/install-minimums/create_constraints_file.py @@ -1,4 +1,5 @@ import sys +from pathlib import Path from zipfile import ZipFile from packaging.requirements import Requirement @@ -30,5 +31,5 @@ constraints_list = [f"{key}{value}\n" for (key, value) in constraints.items()] # Write the constraints to to a pip constraints file. -with open(output_file, "w") as fid: +with Path(output_file).open("w") as fid: fid.writelines(constraints_list) diff --git a/.github/actions/pr-script/pr_script.py b/.github/actions/pr-script/pr_script.py index 5baac22..4c861e9 100644 --- a/.github/actions/pr-script/pr_script.py +++ b/.github/actions/pr-script/pr_script.py @@ -29,7 +29,7 @@ def run(cmd: str, **kwargs: t.Any) -> str: parts[0] = executable try: - return str(check_output(parts, **kwargs).decode("utf-8").strip()) # noqa S603 + return str(check_output(parts, **kwargs).decode("utf-8").strip()) # noqa: S603 except CalledProcessError as e: print("output:", e.output.decode("utf-8").strip()) if e.stderr: @@ -37,7 +37,7 @@ def run(cmd: str, **kwargs: t.Any) -> str: raise e -def run_script() -> None: # noqa +def run_script() -> None: """Run a script on the target pull request URL""" # e.g. https://github.com/foo/bar/pull/81 @@ -51,7 +51,7 @@ def run_script() -> None: # noqa script = "[]" try: script_obj = json.loads(script) - except Exception: # noqa S110 + except Exception: # noqa: S110 pass if not isinstance(script_obj, list): script_obj = [script_obj] @@ -95,14 +95,14 @@ def run_script() -> None: # noqa url = f"https://empty:{auth}@github.com/{user_name}/{repo}" run(f"git clone {url} --filter=blob:none -b {branch} test") if dry_run: - os.mkdir("./test") + Path("./test").mkdir() os.chdir("test") run("pip install -e '.[test]'") for cmd in script: try: run(cmd) - except Exception: # noqa S110 + except Exception: # noqa: S112 continue # Use GitHub Actions bot user and email by default. diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 261262e..83eacd7 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -21,7 +21,7 @@ repos: - id: trailing-whitespace - repo: https://github.com/python-jsonschema/check-jsonschema - rev: 0.27.1 + rev: 0.27.2 hooks: - id: check-github-workflows @@ -31,7 +31,7 @@ repos: - id: mdformat - repo: https://github.com/pre-commit/mirrors-prettier - rev: "v3.0.3" + rev: "v3.1.0" hooks: - id: prettier types_or: [yaml, html, json] @@ -43,7 +43,7 @@ repos: additional_dependencies: [black==23.7.0] - repo: https://github.com/pre-commit/mirrors-mypy - rev: "v1.6.1" + rev: "v1.7.1" hooks: - id: mypy stages: [manual] @@ -64,7 +64,7 @@ repos: - id: rst-inline-touching-normal - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.1.5 + rev: v0.1.6 hooks: - id: ruff types_or: [python, jupyter] @@ -73,7 +73,7 @@ repos: types_or: [python, jupyter] - repo: https://github.com/scientific-python/cookie - rev: "2023.10.27" + rev: "2023.11.17" hooks: - id: sp-repo-review additional_dependencies: ["repo-review[cli]"] diff --git a/foobar.py b/foobar.py index 7191afc..0cf675c 100644 --- a/foobar.py +++ b/foobar.py @@ -3,4 +3,3 @@ def test_no_op() -> None: """Single test to get pytest to pass.""" - pass diff --git a/pyproject.toml b/pyproject.toml index 61b9c1c..99331c0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -54,7 +54,6 @@ testpaths = ["foobar.py"] files = "foobar.py" python_version = "3.8" strict = true -show_error_codes = true enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"] warn_unreachable = true @@ -62,22 +61,34 @@ warn_unreachable = true line-length = 100 [tool.ruff.lint] -select = [ - "A", "B", "C", "DTZ", "E", "EM", "F", "FBT", "I", "ICN", "N", - "PLC", "PLE", "PLR", "PLW", "Q", "RUF", "S", "SIM", "T", "TID", "UP", - "W", "YTT", +extend-select = [ + "B", # flake8-bugbear + "I", # isort + "ARG", # flake8-unused-arguments + "C4", # flake8-comprehensions + "EM", # flake8-errmsg + "ICN", # flake8-import-conventions + "G", # flake8-logging-format + "PGH", # pygrep-hooks + "PIE", # flake8-pie + "PL", # pylint + "PTH", # flake8-use-pathlib + "PT", # flake8-pytest-style + "RET", # flake8-return + "RUF", # Ruff-specific + "SIM", # flake8-simplify + "T20", # flake8-print + "UP", # pyupgrade + "YTT", # flake8-2020 + "EXE", # flake8-executable + "PYI", # flake8-pyi + "S", # flake8-bandit ] ignore = [ - # Q000 Single quotes found but double quotes preferred - "Q000", - # FBT001 Boolean positional arg in function definition - "FBT001", "FBT002", "FBT003", - # E501 Line too long (158 > 100 characters) - "E501", - # SIM105 Use `contextlib.suppress(...)` - "SIM105", - # T201 `print` found - "T201", + "E501", # Line too long (158 > 100 characters) + "SIM105", # Use `contextlib.suppress(...)` + "T201", # `print` found + "PLR", # Design related pylint codes ] unfixable = [ # Don't touch print statements @@ -91,4 +102,4 @@ relative_files = true source = ["foobar"] [tool.repo-review] -ignore = ["PY004", "PY005", "PY007", "PP308", "GH102", "PC140", "RTD100"] +ignore = ["PY004", "PY005", "GH102", "RTD100"]