Skip to content

Commit ff1449c

Browse files
committed
chore: Move formatting and linting to Ruff
Replace Black, isort, Flake8, flynt, and pylint with Ruff.
1 parent 6d1b177 commit ff1449c

File tree

10 files changed

+51
-475
lines changed

10 files changed

+51
-475
lines changed

.github/dependabot.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ updates:
88
ignore:
99
# Ignore all tooling (it can get old, it's mature enough)
1010
- dependency-name: selenium
11-
- dependency-name: black
12-
- dependency-name: pylint
13-
- dependency-name: flake8
14-
- dependency-name: isort
1511
- dependency-name: mypy
16-
- dependency-name: flynt
1712
# Ignore all patch updates.
1813
- dependency-name: '*'
1914
update-types: ["version-update:semver-patch"]

.github/workflows/lint.yml

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ jobs:
1111
with:
1212
python-version: "3.9"
1313

14+
- uses: pre-commit/[email protected]
15+
1416
- name: Install Poetry
1517
uses: snok/[email protected]
1618
with:
@@ -19,7 +21,7 @@ jobs:
1921

2022
- name: Load cached venv
2123
id: cached-poetry-dependencies
22-
uses: actions/cache@v2
24+
uses: actions/cache@v4
2325
with:
2426
path: .venv
2527
key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
@@ -34,21 +36,5 @@ jobs:
3436
echo "PYTHONPATH=$GITHUB_WORKSPACE" >> $GITHUB_ENV
3537
echo "$GITHUB_WORKSPACE/.venv/bin" >> $GITHUB_PATH
3638
37-
- name: Black Code Formatter
38-
uses: psf/black@stable
39-
40-
- name: isort Import Sorter
41-
uses: isort/[email protected]
42-
43-
- name: pylint Error Checker
44-
run: pylint -E -f colorized xray
45-
4639
- name: mypy Static Type Checker
4740
run: mypy .
48-
49-
- name: Flynt f-string Formatter
50-
run: >
51-
flynt .
52-
--line-length=79
53-
--transform-concats
54-
--fail-on-change

.pre-commit-config.yaml

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ci:
55
autoupdate_schedule: quarterly
66
repos:
77
- repo: https://github.com/pre-commit/pre-commit-hooks
8-
rev: v4.6.0
8+
rev: v5.0.0
99
hooks:
1010
- id: check-added-large-files
1111
args: [--maxkb=5000]
@@ -24,28 +24,9 @@ repos:
2424
- id: trailing-whitespace
2525
args: [--markdown-linebreak-ext=md]
2626

27-
- repo: https://github.com/ikamensh/flynt/
28-
rev: '1.0.1'
27+
- repo: https://github.com/astral-sh/ruff-pre-commit
28+
rev: v0.11.8
2929
hooks:
30-
- id: flynt
31-
args: [--line-length=79, --transform-concats]
32-
33-
- repo: https://github.com/psf/black
34-
rev: 24.4.2
35-
hooks:
36-
- id: black
37-
38-
- repo: https://github.com/PyCQA/isort
39-
rev: 5.13.2
40-
hooks:
41-
- id: isort
42-
name: isort (python)
43-
44-
#
45-
# Tried and Failed
46-
#
47-
# 1. I tried doing mypy too, but it was a mess because it runs in its own
48-
# isolated environment, that lacks all our dependencies.
49-
# 2. We might want flake8 someday. It's easy to turn on, but it's so noisy on
50-
# our current code that it'd take some fine-tuning to get it right.
51-
# 3. semgrep was too slow and had to be moved to a github action.
30+
- id: ruff
31+
args: [ --fix ]
32+
- id: ruff-format

poetry.lock

Lines changed: 1 addition & 382 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,32 +45,43 @@ requests = "^2.26.0"
4545
types-requests = "^2.26.0"
4646

4747
[tool.poetry.dev-dependencies]
48-
black = "^23.1"
49-
isort = "^5.12.0"
5048
mypy = "^0.910"
51-
pylint = "^2.7.1"
5249
wheel = "^0.38.2"
53-
flynt = "^0.69"
5450
pre-commit = "^3.3.3"
5551
ipython = "^8.12.0"
5652

57-
[tool.black]
58-
include = '''.*\.pyi?$'''
53+
[tool.ruff]
5954
line-length = 79
60-
61-
[tool.isort]
62-
force_grid_wrap = 0
63-
include_trailing_comma = true
64-
known_third_party = []
65-
line_length = 79
66-
multi_line_output = 3
67-
use_parentheses = true
68-
69-
[tool.pylint.messages_control]
70-
disable = "C0330, C0326"
71-
72-
[tool.pylint.format]
73-
max-line-length = "79"
55+
lint.select = [
56+
# flake8-bugbear
57+
"B",
58+
# flake8-comprehensions
59+
"C4",
60+
# pycodestyle
61+
"E",
62+
# Pyflakes errors
63+
"F",
64+
# isort
65+
"I",
66+
# flake8-simplify
67+
"SIM",
68+
# flake8-tidy-imports
69+
"TID",
70+
# pyupgrade
71+
"UP",
72+
# Pyflakes warnings
73+
"W",
74+
]
75+
lint.ignore = [
76+
# flake8-bugbear opinionated rules
77+
"B9",
78+
# line-too-long
79+
"E501",
80+
# suppressible-exception
81+
"SIM105",
82+
# if-else-block-instead-of-if-exp
83+
"SIM108",
84+
]
7485

7586
[build-system]
7687
build-backend = "poetry.core.masonry.api"

setup.cfg

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
1-
# Flake8 does not yet support pyproject.toml.
2-
# Issue: https://gitlab.com/pycqa/flake8/-/issues/428
3-
4-
[flake8]
5-
max-line-length = 79
6-
extend-ignore = E203, W503
7-
exclude = .git,__pycache__,docs/source/conf.py,old,build,dist,.eggs,.env,.venv
8-
9-
# Neither does mypy, but this is in progress.
10-
# See:
11-
# - https://github.com/python/mypy/issues/5205
12-
# - https://github.com/python/mypy/pull/5208
1+
# Mypy configuration pending move to pyproject.toml
132

143
[mypy]
154
warn_return_any = True

tools/layout-analyzer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161

6262
if r.is_empty: # do not rely on meaningful rects
6363
print(
64-
"skipping text of empty rect at (%g, %g) on page %i"
64+
"skipping text of empty rect at (%g, %g) on page %i" # noqa: UP031
6565
% (r.x0, r.y0, page1.number)
6666
)
6767
else:

xray/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ def inspect(file: Union[str, bytes, Path]) -> PdfRedactionsDict:
2424
:return: A dict with the bad redaction information. If no bad redactions
2525
are found, returns an empty dict.
2626
"""
27-
if type(file) == bytes:
27+
if isinstance(file, bytes):
2828
pdf = Document(stream=file, filetype="pdf")
29-
elif type(file) == str and file.startswith("https://"):
29+
elif isinstance(file, str) and file.startswith("https://"):
3030
r = requests.get(file, timeout=10)
3131
r.raise_for_status()
3232
pdf = Document(stream=r.content, filetype="pdf")

xray/pdf_utils.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ def intersects(
104104
else False.
105105
"""
106106
for rect in rectangles + [text_rect]:
107-
assert all(
108-
[hasattr(rect, "seqno"), hasattr(rect, "fill")]
109-
), "Rectangle lacks required 'seqno' or 'fill' attribute."
107+
assert all([hasattr(rect, "seqno"), hasattr(rect, "fill")]), (
108+
"Rectangle lacks required 'seqno' or 'fill' attribute."
109+
)
110110

111111
overlapping_areas = []
112112
for rect in rectangles:
@@ -128,9 +128,7 @@ def intersects(
128128
area_of_bbox = abs(text_rect.get_area())
129129

130130
percent_occluded = greatest_occluded / area_of_bbox
131-
if percent_occluded > occlusion_threshold:
132-
return True
133-
return False
131+
return percent_occluded > occlusion_threshold
134132

135133

136134
def get_intersecting_chars(

xray/text_utils.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@ def is_repeated_chars(text: str) -> bool:
1717
if len(text) <= 1:
1818
return False
1919

20-
if len(set(text)) == 1:
21-
# There's only one unique character in the string
22-
return True
23-
24-
return False
20+
# Return True if there's only one unique character in the string
21+
return len(set(text)) == 1
2522

2623

2724
def is_ok_words(text: str) -> bool:

0 commit comments

Comments
 (0)