Skip to content

Commit 28bb96c

Browse files
authored
Add ruff fast linter and formatter (#1336)
* Add ruff fast linter and formatter in preparation for using it to replace black, isort, and flake8 * Fix mypy issue * Replace black, isort, and flake8 with ruff and remove former three * Fix typo in ruff linting * Removed a couple empty .format() calls
1 parent 3062aaa commit 28bb96c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+472
-258
lines changed

.github/CONTRIBUTING.md

Lines changed: 139 additions & 101 deletions
Large diffs are not rendered by default.

.github/workflows/format.yml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions
33
name: Format
44

5-
on: [push, pull_request]
5+
on: [ push, pull_request ]
66

77
permissions:
88
contents: read
@@ -11,8 +11,8 @@ jobs:
1111
lint:
1212
strategy:
1313
matrix:
14-
os: [ubuntu-latest]
15-
python-version: ["3.12"]
14+
os: [ ubuntu-latest ]
15+
python-version: [ "3.12" ]
1616
fail-fast: false
1717
runs-on: ${{ matrix.os }}
1818
steps:
@@ -26,8 +26,6 @@ jobs:
2626
with:
2727
python-version: ${{ matrix.python-version }}
2828
- name: Install python prerequisites
29-
run: pip install -U --user pip setuptools setuptools-scm black isort
30-
- name: Black
31-
run: python -m black --check --diff .
32-
- name: isort
33-
run: python -m isort --check-only .
29+
run: pip install -U --user ruff
30+
- name: Ruff format
31+
run: ruff format --check

.github/workflows/lint.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions
33
name: Lint
44

5-
on: [push, pull_request]
5+
on: [ push, pull_request ]
66

77
permissions:
88
contents: read
@@ -11,8 +11,8 @@ jobs:
1111
lint:
1212
strategy:
1313
matrix:
14-
os: [ubuntu-latest]
15-
python-version: ["3.12"]
14+
os: [ ubuntu-latest ]
15+
python-version: [ "3.12" ]
1616
fail-fast: false
1717
runs-on: ${{ matrix.os }}
1818
steps:
@@ -26,6 +26,6 @@ jobs:
2626
with:
2727
python-version: ${{ matrix.python-version }}
2828
- name: Install python prerequisites
29-
run: pip install -U --user pip setuptools setuptools-scm nox
30-
- name: Lint
31-
run: python -m nox --non-interactive --session validate-${{ matrix.python-version }} -k flake8
29+
run: pip install -U --user ruff
30+
- name: Ruff lint
31+
run: ruff check

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,6 @@ Pipfile.lock
4343

4444
# pyenv version file
4545
.python-version
46+
47+
# uv
48+
uv.lock

Pipfile

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,15 @@ setuptools = ">=34.4"
99
wcwidth = ">=0.1.7"
1010

1111
[dev-packages]
12-
black = "*"
13-
cmd2 = {editable = true,path = "."}
14-
cmd2_ext_test = {editable = true,path = "plugins/ext_test"}
12+
cmd2 = { editable = true, path = "." }
13+
cmd2_ext_test = { editable = true, path = "plugins/ext_test" }
1514
codecov = "*"
1615
doc8 = "*"
17-
flake8 = "*"
18-
gnureadline = {version = "*",sys_platform = "== 'darwin'"}
16+
gnureadline = { version = "*", sys_platform = "== 'darwin'" }
1917
invoke = "*"
2018
ipython = "*"
21-
isort = "*"
2219
mypy = "*"
23-
pyreadline3 = {version = ">=3.4",sys_platform = "== 'win32'"}
20+
pyreadline3 = { version = ">=3.4", sys_platform = "== 'win32'" }
2421
pytest = "*"
2522
pytest-cov = "*"
2623
pytest-mock = "*"

cmd2/ansi.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"""
33
Support for ANSI escape sequences which are used for things like applying style to text,
44
setting the window title, and asynchronous alerts.
5-
"""
5+
"""
6+
67
import functools
78
import re
89
from enum import (
@@ -62,25 +63,25 @@ def __repr__(self) -> str:
6263
"""
6364

6465
# Regular expression to match ANSI style sequence
65-
ANSI_STYLE_RE = re.compile(fr'{ESC}\[[^m]*m')
66+
ANSI_STYLE_RE = re.compile(rf'{ESC}\[[^m]*m')
6667

6768
# Matches standard foreground colors: CSI(30-37|90-97|39)m
68-
STD_FG_RE = re.compile(fr'{ESC}\[(?:[39][0-7]|39)m')
69+
STD_FG_RE = re.compile(rf'{ESC}\[(?:[39][0-7]|39)m')
6970

7071
# Matches standard background colors: CSI(40-47|100-107|49)m
71-
STD_BG_RE = re.compile(fr'{ESC}\[(?:(?:4|10)[0-7]|49)m')
72+
STD_BG_RE = re.compile(rf'{ESC}\[(?:(?:4|10)[0-7]|49)m')
7273

7374
# Matches eight-bit foreground colors: CSI38;5;(0-255)m
74-
EIGHT_BIT_FG_RE = re.compile(fr'{ESC}\[38;5;(?:1?[0-9]?[0-9]?|2[0-4][0-9]|25[0-5])m')
75+
EIGHT_BIT_FG_RE = re.compile(rf'{ESC}\[38;5;(?:1?[0-9]?[0-9]?|2[0-4][0-9]|25[0-5])m')
7576

7677
# Matches eight-bit background colors: CSI48;5;(0-255)m
77-
EIGHT_BIT_BG_RE = re.compile(fr'{ESC}\[48;5;(?:1?[0-9]?[0-9]?|2[0-4][0-9]|25[0-5])m')
78+
EIGHT_BIT_BG_RE = re.compile(rf'{ESC}\[48;5;(?:1?[0-9]?[0-9]?|2[0-4][0-9]|25[0-5])m')
7879

7980
# Matches RGB foreground colors: CSI38;2;(0-255);(0-255);(0-255)m
80-
RGB_FG_RE = re.compile(fr'{ESC}\[38;2(?:;(?:1?[0-9]?[0-9]?|2[0-4][0-9]|25[0-5])){{3}}m')
81+
RGB_FG_RE = re.compile(rf'{ESC}\[38;2(?:;(?:1?[0-9]?[0-9]?|2[0-4][0-9]|25[0-5])){{3}}m')
8182

8283
# Matches RGB background colors: CSI48;2;(0-255);(0-255);(0-255)m
83-
RGB_BG_RE = re.compile(fr'{ESC}\[48;2(?:;(?:1?[0-9]?[0-9]?|2[0-4][0-9]|25[0-5])){{3}}m')
84+
RGB_BG_RE = re.compile(rf'{ESC}\[48;2(?:;(?:1?[0-9]?[0-9]?|2[0-4][0-9]|25[0-5])){{3}}m')
8485

8586

8687
def strip_style(text: str) -> str:

cmd2/argparse_completer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def _looks_like_flag(token: str, parser: argparse.ArgumentParser) -> bool:
9292
return False
9393

9494
# Flags have to start with a prefix character
95-
if not token[0] in parser.prefix_chars:
95+
if token[0] not in parser.prefix_chars:
9696
return False
9797

9898
# If it looks like a negative number, it is not a flag unless there are negative-number-like flags

cmd2/clipboard.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""
33
This module provides basic ability to copy from and paste to the clipboard/pastebuffer.
44
"""
5+
56
import typing
67

78
import pyperclip # type: ignore[import]

cmd2/cmd2.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
2222
Git repository on GitHub at https://github.com/python-cmd2/cmd2
2323
"""
24+
2425
# This module has many imports, quite a few of which are only
2526
# infrequently utilized. To reduce the initial overhead of
2627
# import this module, many of these imports are lazy-loaded
@@ -2019,9 +2020,8 @@ def _determine_ap_completer_type(parser: argparse.ArgumentParser) -> Type[argpar
20192020
:param parser: the parser to examine
20202021
:return: type of ArgparseCompleter
20212022
"""
2022-
completer_type: Optional[
2023-
Type[argparse_completer.ArgparseCompleter]
2024-
] = parser.get_ap_completer_type() # type: ignore[attr-defined]
2023+
Completer = Optional[Type[argparse_completer.ArgparseCompleter]]
2024+
completer_type: Completer = parser.get_ap_completer_type() # type: ignore[attr-defined]
20252025

20262026
if completer_type is None:
20272027
completer_type = argparse_completer.DEFAULT_AP_COMPLETER
@@ -5537,7 +5537,7 @@ def cmdloop(self, intro: Optional[str] = None) -> int: # type: ignore[override]
55375537
"""
55385538
# cmdloop() expects to be run in the main thread to support extensive use of KeyboardInterrupts throughout the
55395539
# other built-in functions. You are free to override cmdloop, but much of cmd2's features will be limited.
5540-
if not threading.current_thread() is threading.main_thread():
5540+
if threading.current_thread() is not threading.main_thread():
55415541
raise RuntimeError("cmdloop must be run in the main thread")
55425542

55435543
# Register signal handlers

cmd2/command_definition.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""
33
Supports the definition of commands in separate classes to be composed into cmd2.Cmd
44
"""
5+
56
from typing import (
67
TYPE_CHECKING,
78
Callable,

0 commit comments

Comments
 (0)