Skip to content

Commit 854d5ba

Browse files
authored
Add isort pre-commit and lint with isort and yapf in CI (google#1093)
* add empty .isort.cfg * force_single_line * add pre-commit-config, apply sort * remove unused pre-commit-hooks * Lint with isort * Lint with Yapf. * move yapftests to its own section * verbose output
1 parent 1ab5df0 commit 854d5ba

22 files changed

+34
-29
lines changed

.github/workflows/ci.yml

+8
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,11 @@ jobs:
3232
pip install pytest
3333
pip install pytest-cov
3434
pytest
35+
- name: Lint with isort
36+
run: |
37+
pip install isort
38+
isort . --check --diff
39+
- name: Lint with yapf
40+
run: |
41+
pip install .
42+
yapf . -vv --diff --recursive

.isort.cfg

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[settings]
2+
force_single_line=true
3+
known_yapftests=yapftests
4+
5+
sections=FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER,YAPFTESTS

.pre-commit-config.yaml

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
# to enable run `pip install pre-commit && pre-commit install`
33

44
repos:
5+
- repo: https://github.com/pycqa/isort
6+
rev: 5.11.5
7+
hooks:
8+
- id: isort
9+
name: isort (python)
510
- repo: local
611
hooks:
712
- id: yapf
@@ -15,15 +20,12 @@ repos:
1520
hooks:
1621
- id: trailing-whitespace
1722
- id: check-docstring-first
18-
- id: check-json
1923
- id: check-added-large-files
2024
- id: check-yaml
2125
- id: debug-statements
22-
- id: requirements-txt-fixer
2326
- id: check-merge-conflict
2427
- id: double-quote-string-fixer
2528
- id: end-of-file-fixer
26-
- id: sort-simple-yaml
2729
- repo: meta
2830
hooks:
2931
- id: check-hooks-apply

setup.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
import sys
1818
import unittest
1919

20-
from setuptools import find_packages, setup, Command
20+
from setuptools import Command
21+
from setuptools import find_packages
22+
from setuptools import setup
2123

2224
import yapf
2325

third_party/yapf_diff/yapf_diff.py

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import re
3131
import subprocess
3232
import sys
33-
3433
from io import StringIO
3534

3635

yapf/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ def FormatFiles(filenames,
200200
"""
201201
changed = False
202202
if parallel:
203-
import multiprocessing # pylint: disable=g-import-not-at-top
204203
import concurrent.futures # pylint: disable=g-import-not-at-top
204+
import multiprocessing # pylint: disable=g-import-not-at-top
205205
workers = min(multiprocessing.cpu_count(), len(filenames))
206206
with concurrent.futures.ProcessPoolExecutor(workers) as executor:
207207
future_formats = [

yapf/pytree/blank_line_calculator.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -174,5 +174,4 @@ def _StartsInZerothColumn(node):
174174

175175

176176
def _AsyncFunction(node):
177-
return (node.prev_sibling and
178-
node.prev_sibling.type == grammar_token.ASYNC)
177+
return (node.prev_sibling and node.prev_sibling.type == grammar_token.ASYNC)

yapf/pytree/pytree_utils.py

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
import ast
2828
import os
29-
3029
from lib2to3 import pygram
3130
from lib2to3 import pytree
3231
from lib2to3.pgen2 import driver

yapf/pytree/pytree_visitor.py

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
"""
2626

2727
import sys
28-
2928
from lib2to3 import pytree
3029

3130
from yapf.pytree import pytree_utils

yapf/pytree/split_penalty.py

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
"""Computation of split penalties before/between tokens."""
1515

1616
import re
17-
1817
from lib2to3 import pytree
1918
from lib2to3.pgen2 import token as grammar_token
2019

yapf/yapflib/format_token.py

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import keyword
1717
import re
18-
1918
from functools import lru_cache
2019
from lib2to3.pgen2 import token
2120

yapf/yapflib/logical_line.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
perform the wrapping required to comply with the style guide.
2020
"""
2121

22+
from lib2to3.fixer_util import syms as python_symbols
23+
2224
from yapf.pytree import pytree_utils
2325
from yapf.pytree import split_penalty
2426
from yapf.yapflib import format_token
2527
from yapf.yapflib import style
2628
from yapf.yapflib import subtypes
2729

28-
from lib2to3.fixer_util import syms as python_symbols
29-
3030

3131
class LogicalLine(object):
3232
"""Represents a single logical line in the output.

yapf/yapflib/reformatter.py

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import collections
2323
import heapq
2424
import re
25-
2625
from lib2to3 import pytree
2726
from lib2to3.pgen2 import token
2827

yapf/yapflib/yapf_api.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,11 @@
3838
import sys
3939

4040
from yapf.pyparser import pyparser
41-
42-
from yapf.pytree import pytree_unwrapper
43-
from yapf.pytree import pytree_utils
4441
from yapf.pytree import blank_line_calculator
4542
from yapf.pytree import comment_splicer
4643
from yapf.pytree import continuation_splicer
44+
from yapf.pytree import pytree_unwrapper
45+
from yapf.pytree import pytree_utils
4746
from yapf.pytree import split_penalty
4847
from yapf.pytree import subtype_assigner
4948
from yapf.yapflib import errors

yapftests/comment_splicer_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import textwrap
1717
import unittest
1818

19-
from yapf.pytree import pytree_utils
2019
from yapf.pytree import comment_splicer
20+
from yapf.pytree import pytree_utils
2121

2222

2323
class CommentSplicerTest(unittest.TestCase):

yapftests/format_decision_state_test.py

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import unittest
1818

1919
from yapf.pytree import pytree_utils
20-
2120
from yapf.yapflib import format_decision_state
2221
from yapf.yapflib import logical_line
2322
from yapf.yapflib import style

yapftests/format_token_test.py

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
"""Tests for yapf.format_token."""
1515

1616
import unittest
17-
1817
from lib2to3 import pytree
1918
from lib2to3.pgen2 import token
2019

yapftests/logical_line_test.py

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import textwrap
1717
import unittest
18-
1918
from lib2to3 import pytree
2019
from lib2to3.pgen2 import token
2120

yapftests/main_test.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
# limitations under the License.
1515
"""Tests for yapf.__init__.main."""
1616

17-
from contextlib import contextmanager
18-
from io import StringIO
1917
import sys
2018
import unittest
19+
from contextlib import contextmanager
20+
from io import StringIO
21+
2122
import yapf
2223

2324
from yapftests import yapf_test_helper

yapftests/pytree_utils_test.py

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
"""Tests for yapf.pytree_utils."""
1515

1616
import unittest
17-
1817
from lib2to3 import pygram
1918
from lib2to3 import pytree
2019
from lib2to3.pgen2 import token

yapftests/split_penalty_test.py

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import sys
1717
import textwrap
1818
import unittest
19-
2019
from lib2to3 import pytree
2120

2221
from yapf.pytree import pytree_utils

yapftests/yapf_test.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import tempfile
2424
import textwrap
2525
import unittest
26-
2726
from io import StringIO
2827
from lib2to3.pgen2 import tokenize
2928

@@ -285,8 +284,9 @@ def testNoFile(self):
285284
with self.assertRaises(IOError) as context:
286285
yapf_api.FormatFile('not_a_file.py')
287286

288-
self.assertEqual(str(context.exception),
289-
"[Errno 2] No such file or directory: 'not_a_file.py'")
287+
self.assertEqual(
288+
str(context.exception),
289+
"[Errno 2] No such file or directory: 'not_a_file.py'")
290290

291291
def testCommentsUnformatted(self):
292292
code = textwrap.dedent("""\

0 commit comments

Comments
 (0)