Skip to content

Commit a06679b

Browse files
committed
Add ruff to linting job
1 parent 31d0c6b commit a06679b

6 files changed

Lines changed: 92 additions & 47 deletions

File tree

.config/ruff.toml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Python 2.7 and 3.3+ compatibility requirements (using py37 as minimum ruff supports)
2+
target-version = "py37"
3+
4+
# Define Python 2.7 builtins that don't exist in Python 3
5+
builtins = ["unicode"]
6+
7+
line-length = 200
8+
9+
[lint]
10+
# Enable most rules
11+
select = [
12+
"E", # pycodestyle errors
13+
"W", # pycodestyle warnings
14+
"F", # Pyflakes
15+
"I", # isort
16+
]
17+
18+
ignore = [
19+
"I001", # Import block unsorted
20+
"W292", # No newline at end of file
21+
"W293", # Blank line contains whitespace
22+
]
23+
24+
# Per-file ignores for specific compatibility needs
25+
[lint.per-file-ignores]
26+
# AST compatibility module needs star imports for Python version compatibility
27+
"src/python_minifier/ast_compat.py" = ["F403", "F405"]
28+
29+
# __init__.py files intentionally re-export for public API
30+
"*/__init__.py" = ["F401"]
31+
32+
# Type comparisons needed for Python 2.7 compatibility in specific files
33+
"src/python_minifier/ast_compare.py" = ["E721"]
34+
"src/python_minifier/rename/rename_literals.py" = ["E721"]
35+
"src/python_minifier/transforms/constant_folding.py" = ["E721"]
36+
37+
# Compatibility imports in utility modules
38+
"src/python_minifier/rename/util.py" = ["F401"]
39+
40+
[lint.isort]
41+
force-single-line = false
42+
known-first-party = ["python_minifier"]

.github/github_sucks

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11

22

33

4+

.github/workflows/test.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ jobs:
8989
show-progress: false
9090
persist-credentials: false
9191

92-
- name: Actions workflows
92+
- name: Lint Actions workflows
9393
run: |
9494
bash <(curl https://raw.githubusercontent.com/rhysd/actionlint/main/scripts/download-actionlint.bash)
9595
./actionlint
@@ -115,7 +115,8 @@ jobs:
115115
globs: |
116116
**/README.md
117117
118-
- name: Lint Dockerfile
119-
uses: hadolint/hadolint-action@54c9adbab1582c2ef04b2016b760714a4bfde3cf # v3.1.0
118+
- name: Lint Python code
119+
uses: astral-sh/ruff-action@9828f49eb4cadf267b40eaa330295c412c68c1f9 # v3
120120
with:
121-
dockerfile: ./docker/Dockerfile-*
121+
args: --config=.config/ruff.toml check
122+
src: src

src/python_minifier/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ def minify(
8686
:param str source: The python module source code
8787
:param str filename: The original source filename if known
8888
89-
:param remove_annotations: Configures the removal of type annotations. True removes all annotations, False removes none. RemoveAnnotationsOptions can be used to configure the removal of specific annotations.
89+
:param remove_annotations: Configures the removal of type annotations. True removes all annotations, False removes none.
90+
RemoveAnnotationsOptions can be used to configure the removal of specific annotations.
9091
:type remove_annotations: bool or RemoveAnnotationsOptions
9192
:param bool remove_pass: If Pass statements should be removed where possible
9293
:param bool remove_literal_statements: If statements consisting of a single literal should be removed, including docstrings

src/python_minifier/ast_compare.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,26 +77,26 @@ def counter():
7777
% (type(l_ast), field, len(l_list), type(r_ast), field, len(r_list)),
7878
)
7979

80-
for i, l, r in zip(counter(), l_list, r_list):
81-
if isinstance(l, ast.AST) or isinstance(r, ast.AST):
82-
compare_ast(l, r)
83-
elif l != r:
80+
for i, left, right in zip(counter(), l_list, r_list):
81+
if isinstance(left, ast.AST) or isinstance(right, ast.AST):
82+
compare_ast(left, right)
83+
elif left != right:
8484
raise CompareError(
8585
l_ast,
8686
r_ast,
8787
'Fields do not match! %s.%s[%i]=%r, %s.%s[%i]=%r'
88-
% (type(l_ast), field, i, l, type(r_ast), field, i, r),
88+
% (type(l_ast), field, i, left, type(r_ast), field, i, right),
8989
)
9090

9191
else:
92-
l = getattr(l_ast, field, None)
93-
r = getattr(r_ast, field, None)
92+
left_field = getattr(l_ast, field, None)
93+
right_field = getattr(r_ast, field, None)
9494

95-
if isinstance(l, ast.AST) or isinstance(r, ast.AST):
96-
compare_ast(l, r)
97-
elif l != r:
95+
if isinstance(left_field, ast.AST) or isinstance(right_field, ast.AST):
96+
compare_ast(left_field, right_field)
97+
elif left_field != right_field:
9898
raise CompareError(
9999
l_ast,
100100
r_ast,
101-
'Fields do not match! %s.%s=%r, %s.%s=%r' % (type(l_ast), field, l, type(r_ast), field, r),
101+
'Fields do not match! %s.%s=%r, %s.%s=%r' % (type(l_ast), field, left_field, type(r_ast), field, right_field),
102102
)

src/python_minifier/f_string.py

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -272,31 +272,31 @@ def _get_quote(self, c):
272272
raise ValueError("Couldn't find a quote")
273273

274274
def _literals(self):
275-
l = ''
275+
literal = ''
276276
for c in self._s:
277277
if not self._can_quote(c):
278-
if l:
279-
l += self.current_quote
280-
yield l
281-
l = ''
278+
if literal:
279+
literal += self.current_quote
280+
yield literal
281+
literal = ''
282282

283283
self.current_quote = self._get_quote(c)
284284

285-
if l == '':
286-
l += self.current_quote
285+
if literal == '':
286+
literal += self.current_quote
287287

288288
if c == '\n':
289-
l += '\\n'
289+
literal += '\\n'
290290
elif c == '\r':
291-
l += '\\r'
291+
literal += '\\r'
292292
elif c == '\\':
293-
l += '\\\\'
293+
literal += '\\\\'
294294
else:
295-
l += c
295+
literal += c
296296

297-
if l:
298-
l += self.current_quote
299-
yield l
297+
if literal:
298+
literal += self.current_quote
299+
yield literal
300300

301301
def __str__(self):
302302
if self._s == '':
@@ -315,10 +315,10 @@ def __str__(self):
315315
for start_quote in self.allowed_quotes:
316316
self.current_quote = start_quote
317317
s = ''
318-
for l in self._literals():
319-
if s and s[-1] == l[0]:
318+
for literal in self._literals():
319+
if s and s[-1] == literal[0]:
320320
s += ' '
321-
s += l
321+
s += literal
322322

323323
if eval(s) == self._s:
324324
candidates.append(s)
@@ -399,23 +399,23 @@ def _get_quote(self, c):
399399
raise ValueError("Couldn't find a quote")
400400

401401
def _literals(self):
402-
l = ''
402+
literal = ''
403403
for b in self._b:
404404
if not self._can_quote(b):
405-
if l:
406-
l += self.current_quote
407-
yield l
408-
l = ''
405+
if literal:
406+
literal += self.current_quote
407+
yield literal
408+
literal = ''
409409

410410
self.current_quote = self._get_quote(b)
411411

412-
if l == '':
413-
l = 'b' + self.current_quote
414-
l += chr(b)
412+
if literal == '':
413+
literal = 'b' + self.current_quote
414+
literal += chr(b)
415415

416-
if l:
417-
l += self.current_quote
418-
yield l
416+
if literal:
417+
literal += self.current_quote
418+
yield literal
419419

420420
def __str__(self):
421421
if self._b == b'':
@@ -434,10 +434,10 @@ def __str__(self):
434434
for start_quote in self.allowed_quotes:
435435
self.current_quote = start_quote
436436
s = ''
437-
for l in self._literals():
438-
if s and s[-1] == l[0]:
437+
for literal in self._literals():
438+
if s and s[-1] == literal[0]:
439439
s += ' '
440-
s += l
440+
s += literal
441441

442442
assert eval(s) == self._b
443443
candidates.append(s)

0 commit comments

Comments
 (0)