Skip to content

Commit c8a4d66

Browse files
authored
Merge pull request #114 from dflook/lint
Address linter warnings
2 parents 22823f3 + ba50166 commit c8a4d66

Some content is hidden

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

42 files changed

+277
-288
lines changed

.github/workflows/release_test.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ jobs:
155155
pyminify setup.py src test --in-place
156156
157157
test_typing:
158+
name: Test Typing
158159
runs-on: ubuntu-latest
159160
needs: [package_python3]
160161
strategy:
@@ -187,7 +188,7 @@ jobs:
187188

188189
- name: Test typing
189190
run: |
190-
pip3.13 install mypy types-setuptools
191+
pip3.13 install mypy!=1.12.0 types-setuptools
191192
mypy --strict typing_test/test_typing.py
192193
193194
if mypy --strict typing_test/test_badtyping.py; then

corpus_test/generate_report.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ def format_difference(compare: Iterable[Result], base: Iterable[Result]) -> str:
165165
:param base: The results to compare against
166166
"""
167167

168-
compare_set = set(result.corpus_entry for result in compare)
169-
base_set = set(result.corpus_entry for result in base)
168+
compare_set = {result.corpus_entry for result in compare}
169+
base_set = {result.corpus_entry for result in base}
170170

171171
s = str(len(compare_set))
172172

@@ -293,6 +293,22 @@ def report_slowest(results_dir: str, python_versions: str, minifier_sha: str) ->
293293
for entry in sorted(summary.entries.values(), key=lambda entry: entry.time, reverse=True)[:10]:
294294
yield f'| {entry.corpus_entry} | {entry.original_size} | {entry.minified_size} | {entry.time:.3f} |'
295295

296+
def format_size_change_detail(summary, base_summary) -> str:
297+
mean_percent_of_original_change = summary.mean_percent_of_original - base_summary.mean_percent_of_original
298+
299+
s = f'{summary.mean_percent_of_original:.3f}% ({mean_percent_of_original_change:+.3f}%'
300+
301+
got_bigger_count = len(list(summary.compare_size_increase(base_summary)))
302+
got_smaller_count = len(list(summary.compare_size_decrease(base_summary)))
303+
304+
if got_bigger_count > 0:
305+
s += f', {got_bigger_count} :chart_with_upwards_trend:'
306+
if got_smaller_count > 0:
307+
s += f', {got_smaller_count} :chart_with_downwards_trend:'
308+
309+
s += ')'
310+
311+
return s
296312

297313
def report(results_dir: str, minifier_ref: str, minifier_sha: str, base_ref: str, base_sha: str) -> Iterable[str]:
298314
"""
@@ -336,28 +352,11 @@ def report(results_dir: str, minifier_ref: str, minifier_sha: str, base_ref: str
336352

337353
mean_time_change = summary.mean_time - base_summary.mean_time
338354

339-
def format_size_change_detail() -> str:
340-
mean_percent_of_original_change = summary.mean_percent_of_original - base_summary.mean_percent_of_original
341-
342-
s = f'{summary.mean_percent_of_original:.3f}% ({mean_percent_of_original_change:+.3f}%'
343-
344-
got_bigger_count = len(list(summary.compare_size_increase(base_summary)))
345-
got_smaller_count = len(list(summary.compare_size_decrease(base_summary)))
346-
347-
if got_bigger_count > 0:
348-
s += f', {got_bigger_count} :chart_with_upwards_trend:'
349-
if got_smaller_count > 0:
350-
s += f', {got_smaller_count} :chart_with_downwards_trend:'
351-
352-
s += ')'
353-
354-
return s
355-
356355
yield (
357356
f'| {python_version} ' +
358357
f'| {summary.valid_count} ' +
359358
f'| {summary.mean_time:.3f} ({mean_time_change:+.3f}) ' +
360-
f'| {format_size_change_detail()} ' +
359+
f'| {format_size_change_detail(summary, base_summary)} ' +
361360
f'| {format_difference(summary.larger_than_original(), base_summary.larger_than_original())} ' +
362361
f'| {format_difference(summary.recursion_error(), base_summary.recursion_error())} ' +
363362
f'| {format_difference(summary.unstable_minification(), base_summary.unstable_minification())} ' +

corpus_test/generate_results.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
from result import Result, ResultWriter
1212

1313
try:
14-
RE = RecursionError
14+
RError = RecursionError
1515
except NameError:
1616
# Python 2
17-
class RE(Exception):
17+
class RError(Exception):
1818
pass
1919

2020

@@ -46,7 +46,7 @@ def minify_corpus_entry(corpus_path, corpus_entry):
4646

4747
result.outcome = 'Minified'
4848

49-
except RE:
49+
except RError:
5050
# Source is too deep
5151
result.outcome = 'RecursionError'
5252

@@ -60,7 +60,7 @@ def minify_corpus_entry(corpus_path, corpus_entry):
6060
result.time = end_time - start_time
6161
result.outcome = 'UnstableMinification'
6262

63-
except AssertionError as assertion_error:
63+
except AssertionError:
6464
result.outcome = 'Exception: AssertionError'
6565

6666
except Exception as exception:
@@ -113,7 +113,7 @@ def corpus_test(corpus_path, results_path, sha, regenerate_results):
113113
if entry in result_writer:
114114
continue
115115

116-
logging.debug('Corpus entry [' + entry + ']')
116+
logging.debug('Corpus entry [%s]', entry)
117117

118118
result = minify_corpus_entry(corpus_path, entry)
119119
result_writer.write(result)

corpus_test/result.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def __init__(self, results_path):
7979
def __enter__(self):
8080
self.results = open(self._results_path, 'r')
8181
header = self.results.readline()
82-
assert header == 'corpus_entry,original_size,minified_size,time,result\n' or header == ''
82+
assert header in ['corpus_entry,original_size,minified_size,time,result\n', '']
8383
return self
8484

8585
def __exit__(self, exc_type, exc_val, exc_tb):
@@ -99,12 +99,12 @@ def __next__(self):
9999

100100
if line == '':
101101
raise StopIteration
102-
else:
103-
result_line = line.split(',')
104-
return Result(
105-
result_line[0],
106-
int(result_line[1]),
107-
int(result_line[2]),
108-
float(result_line[3]),
109-
result_line[4].rstrip()
110-
)
102+
103+
result_line = line.split(',')
104+
return Result(
105+
result_line[0],
106+
int(result_line[1]),
107+
int(result_line[2]),
108+
float(result_line[3]),
109+
result_line[4].rstrip()
110+
)

hypo_test/expressions.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def name(draw) -> SearchStrategy:
119119
assume(' ' not in normalised)
120120
try:
121121
ast.parse(normalised, mode='eval')
122-
except:
122+
except Exception:
123123
assume(False)
124124
return normalised
125125

@@ -212,14 +212,6 @@ def Attribute(draw, expression) -> ast.Attribute:
212212
attr = draw(text(alphabet=string.ascii_letters, min_size=1, max_size=3).filter(lambda n: n not in keyword.kwlist))
213213
return ast.Attribute(value, attr, ast.Load())
214214

215-
216-
@composite
217-
def Subscript(draw, expression) -> ast.Subscript:
218-
value = draw(expression)
219-
attr = draw(text(alphabet=string.ascii_letters, min_size=1, max_size=3))
220-
return ast.Subscript(value, attr, ast.Load())
221-
222-
223215
@composite
224216
def Yield(draw, expression) -> ast.Yield:
225217
return ast.Yield(draw(expression))
@@ -296,7 +288,7 @@ def arg(draw, allow_annotation=True) -> ast.arg:
296288
@composite
297289
def arguments(draw, for_lambda=False) -> ast.arguments:
298290

299-
allow_annotation = False if for_lambda else True
291+
allow_annotation = not for_lambda
300292

301293
args = draw(lists(arg(allow_annotation), max_size=2))
302294
posonlyargs = draw(lists(arg(allow_annotation), max_size=2))

src/python_minifier/__main__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from python_minifier import minify
88
from python_minifier.transforms.remove_annotations_options import RemoveAnnotationsOptions
99

10+
1011
if sys.version_info >= (3, 8):
1112
from importlib import metadata
1213

@@ -270,9 +271,9 @@ def error(os_error):
270271

271272
for path_arg in args.path:
272273
if os.path.isdir(path_arg):
273-
for root, dirs, files in os.walk(path_arg, onerror=error, followlinks=True):
274+
for root, _dirs, files in os.walk(path_arg, onerror=error, followlinks=True):
274275
for file in files:
275-
if file.endswith('.py') or file.endswith('.pyw'):
276+
if file.endswith(('.py', '.pyw')):
276277
yield os.path.join(root, file)
277278
else:
278279
yield path_arg

src/python_minifier/ast_compat.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from ast import *
1010

11+
1112
# Ideally we don't import anything else
1213

1314
if 'TypeAlias' in globals():

src/python_minifier/ast_printer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from python_minifier.util import is_ast_node
1616

17+
1718
INDENT = ' '
1819

1920
# The field name that can be omitted for each node

src/python_minifier/expression_printer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,8 @@ def visit_arguments(self, node):
629629
def visit_arg(self, node):
630630
if isinstance(node, ast.Name):
631631
# Python 2 uses Name nodes
632-
return self.visit_Name(node)
632+
self.visit_Name(node)
633+
return
633634

634635
self.printer.identifier(node.arg)
635636

src/python_minifier/f_string.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def is_correct_ast(self, code):
3636
c = ast.parse(code, 'FString candidate', mode='eval')
3737
compare_ast(self.node, c.body)
3838
return True
39-
except Exception as e:
39+
except Exception:
4040
return False
4141

4242
def complete_debug_specifier(self, partial_specifier_candidates, value_node):
@@ -79,12 +79,12 @@ def candidates(self):
7979
# Maybe!
8080
try:
8181
debug_specifier_candidates = [x + '{' + v.s for x in candidates]
82-
except Exception as e:
82+
except Exception:
8383
continue
8484

8585
try:
8686
candidates = [x + self.str_for(v.s, quote) for x in candidates]
87-
except Exception as e:
87+
except Exception:
8888
continue
8989
elif isinstance(v, ast.FormattedValue):
9090
try:
@@ -93,15 +93,14 @@ def candidates(self):
9393
x + y for x in candidates for y in FormattedValue(v, nested_allowed, self.pep701).get_candidates()
9494
] + completed
9595
debug_specifier_candidates = []
96-
except Exception as e:
96+
except Exception:
9797
continue
9898
else:
9999
raise RuntimeError('Unexpected JoinedStr value')
100100

101101
actual_candidates += ['f' + quote + x + quote for x in candidates]
102102

103-
actual_candidates = filter(self.is_correct_ast, actual_candidates)
104-
return actual_candidates
103+
return filter(self.is_correct_ast, actual_candidates)
105104

106105
def str_for(self, s, quote):
107106
return s.replace('{', '{{').replace('}', '}}')

0 commit comments

Comments
 (0)