From fcd759d6feb9142736a19f8a753be975a120be87 Mon Sep 17 00:00:00 2001 From: Branch Vincent Date: Sun, 14 May 2023 11:47:14 -0700 Subject: [PATCH] chore: migrate to ruff (#113) --- .flake8 | 3 --- .pre-commit-config.yaml | 22 +++------------- install-poetry.py | 57 ++++++++++++++++++++++------------------- pyproject.toml | 39 ++++++++++++++++++++-------- 4 files changed, 63 insertions(+), 58 deletions(-) delete mode 100644 .flake8 diff --git a/.flake8 b/.flake8 deleted file mode 100644 index 1ad81e7..0000000 --- a/.flake8 +++ /dev/null @@ -1,3 +0,0 @@ -[flake8] -max-line-length = 88 -ignore = E501, E203, W503 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f6ff660..02fbdd2 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -4,18 +4,6 @@ repos: hooks: - id: black - - repo: https://github.com/PyCQA/flake8 - rev: 6.0.0 - hooks: - - id: flake8 - additional_dependencies: - - flake8-bugbear==22.7.1 - - - repo: https://github.com/PyCQA/isort - rev: 5.12.0 - hooks: - - id: isort - - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.4.0 hooks: @@ -23,12 +11,10 @@ repos: - id: end-of-file-fixer - id: debug-statements - id: pretty-format-json - args: - - --autofix + args: [--autofix] - id: check-json - - repo: https://github.com/asottile/pyupgrade - rev: v3.4.0 + - repo: https://github.com/charliermarsh/ruff-pre-commit + rev: v0.0.267 hooks: - - id: pyupgrade - args: [--py36-plus] + - id: ruff diff --git a/install-poetry.py b/install-poetry.py index b6d32d4..4a96007 100644 --- a/install-poetry.py +++ b/install-poetry.py @@ -22,7 +22,15 @@ installs, or use of pipx as alternatives to executing arbitrary, unversioned cod script to alternatives, consider maintaining a local copy as part of your infrastructure. For full documentation, visit https://python-poetry.org/docs/#installation. -""" +""" # noqa: E501 +import sys + + +# Eager version check so we fail nicely before possible syntax errors +if sys.version_info < (3, 6): # noqa: UP036 + sys.stdout.write("Poetry installer requires Python 3.6 or newer to run!\n") + sys.exit(1) + import argparse import json @@ -30,7 +38,6 @@ import os import re import shutil import subprocess -import sys import sysconfig import tempfile @@ -107,8 +114,8 @@ def is_decorated(): if WINDOWS: return ( os.getenv("ANSICON") is not None - or "ON" == os.getenv("ConEmuANSI") - or "xterm" == os.getenv("Term") + or os.getenv("ConEmuANSI") == "ON" # noqa: SIM112 + or os.getenv("Term") == "xterm" # noqa: SIM112 ) if not hasattr(sys.stdout, "fileno"): @@ -278,7 +285,7 @@ class VirtualEnvironment: self._bin_path = self._path.joinpath( "Scripts" if WINDOWS and not MINGW else "bin" ) - # str is required for compatibility with subprocess run on CPython <= 3.7 on Windows + # str is for compatibility with subprocess.run on CPython <= 3.7 on Windows self._python = str( self._path.joinpath(self._bin_path, "python.exe" if WINDOWS else "python") ) @@ -295,7 +302,8 @@ class VirtualEnvironment: def make(cls, target: Path) -> "VirtualEnvironment": if not sys.executable: raise ValueError( - "Unable to determine sys.executable. Set PATH to a sane value or set it explicitly with PYTHONEXECUTABLE." + "Unable to determine sys.executable. Set PATH to a sane value or set it" + " explicitly with PYTHONEXECUTABLE." ) try: @@ -340,7 +348,7 @@ class VirtualEnvironment: env = cls(target) - # we do this here to ensure that outdated system default pip does not trigger older bugs + # this ensures that outdated system default pip does not trigger older bugs env.pip("install", "--disable-pip-version-check", "--upgrade", "pip") return env @@ -527,18 +535,20 @@ class Installer: mx = self.VERSION_REGEX.match(x) if mx is None: - # the version is not semver, perhaps scm or file, we assume upgrade is supported + # the version is not semver, perhaps scm or file + # we assume upgrade is supported return True - vx = tuple(int(p) for p in mx.groups()[:3]) + (mx.group(5),) + vx = (*tuple(int(p) for p in mx.groups()[:3]), mx.group(5)) return vx >= (1, 1, 7) if version and not _is_self_upgrade_supported(version): self._write( colorize( "warning", - f"You are installing {version}. When using the current installer, this version does not support " - f"updating using the 'self update' command. Please use 1.1.7 or later.", + f"You are installing {version}. When using the current installer, " + "this version does not support updating using the 'self update' " + "command. Please use 1.1.7 or later.", ) ) if not self._accept_all: @@ -551,7 +561,7 @@ class Installer: except subprocess.CalledProcessError as e: raise PoetryInstallationError( return_code=e.returncode, log=e.output.decode() - ) + ) from e self._write("") self.display_post_message(version) @@ -713,11 +723,12 @@ class Installer: def get_windows_path_var(self) -> Optional[str]: import winreg - with winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER) as root: - with winreg.OpenKey(root, "Environment", 0, winreg.KEY_ALL_ACCESS) as key: - path, _ = winreg.QueryValueEx(key, "PATH") + with winreg.ConnectRegistry( + None, winreg.HKEY_CURRENT_USER + ) as root, winreg.OpenKey(root, "Environment", 0, winreg.KEY_ALL_ACCESS) as key: + path, _ = winreg.QueryValueEx(key, "PATH") - return path + return path def display_post_message_fish(self, version: str) -> None: fish_user_paths = subprocess.check_output( @@ -778,8 +789,8 @@ class Installer: mx = self.VERSION_REGEX.match(x) my = self.VERSION_REGEX.match(y) - vx = tuple(int(p) for p in mx.groups()[:3]) + (mx.group(5),) - vy = tuple(int(p) for p in my.groups()[:3]) + (my.group(5),) + vx = (*tuple(int(p) for p in mx.groups()[:3]), mx.group(5)) + vy = (*tuple(int(p) for p in my.groups()[:3]), my.group(5)) if vx < vy: return -1 @@ -838,13 +849,6 @@ class Installer: def main(): - if sys.version_info < (3, 6): - sys.stdout.write( - colorize("error", "Poetry installer requires Python 3.6 or newer to run!") - ) - # return error code - return 1 - parser = argparse.ArgumentParser( description="Installs the latest (or given) version of poetry" ) @@ -930,7 +934,8 @@ def main(): text=True, ) installer._write(colorize("error", f"See {path} for error logs.")) - text = f"{e.log}\nTraceback:\n\n{''.join(traceback.format_tb(e.__traceback__))}" + tb = "".join(traceback.format_tb(e.__traceback__)) + text = f"{e.log}\nTraceback:\n\n{tb}" Path(path).write_text(text) return e.return_code diff --git a/pyproject.toml b/pyproject.toml index 35ef6c3..6abfcb9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,12 +1,29 @@ -[tool.isort] -profile = "black" -force_single_line = true -atomic = true -lines_after_imports = 2 -lines_between_types = 1 -filter_files = true - - -[tool.black] +[tool.ruff] +fix = true +unfixable = [ + "ERA", # do not autoremove commented out code +] +target-version = "py37" line-length = 88 -include = '\.pyi?$' +extend-select = [ + "B", # flake8-bugbear + "C4", # flake8-comprehensions + "ERA", # flake8-eradicate/eradicate + "I", # isort + "N", # pep8-naming + "PIE", # flake8-pie + "PGH", # pygrep + "RUF", # ruff checks + "SIM", # flake8-simplify + "TCH", # flake8-type-checking + "TID", # flake8-tidy-imports + "UP", # pyupgrade +] + +[tool.ruff.flake8-tidy-imports] +ban-relative-imports = "all" + +[tool.ruff.isort] +force-single-line = true +lines-between-types = 1 +lines-after-imports = 2