Skip to content

Commit 39dc44b

Browse files
authored
Automatically sort imports (isort CLI tool) (#2033)
1 parent b490b5d commit 39dc44b

62 files changed

Lines changed: 264 additions & 169 deletions

Some content is hidden

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

.github/workflows/build.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,14 @@ jobs:
161161
- uses: actions/setup-python@v2
162162
- name: 'Run linters'
163163
run: |
164+
# py2
164165
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py
165166
python2 get-pip.py
166167
python2 -m pip install flake8
167-
python3 -m pip install flake8
168168
python2 -m flake8 .
169+
# py3
170+
python3 -m pip install flake8 isort
169171
python3 -m flake8 .
170-
echo "flake8 linting OK"
172+
python3 -m isort --settings=.isort.cfg .
173+
# clinter
171174
find . -type f \( -iname "*.c" -o -iname "*.h" \) | xargs python3 scripts/internal/clinter.py
172-
echo "C linting OK"

.isort.cfg

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# See: https://pycqa.github.io/isort/docs/configuration/options
2+
3+
[settings]
4+
# one import per line
5+
force_single_line = true
6+
# blank spaces after import section
7+
lines_after_imports = 2

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
include .coveragerc
22
include .flake8
33
include .gitignore
4+
include .isort.cfg
45
include CONTRIBUTING.md
56
include CREDITS
67
include HISTORY.rst

Makefile

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ DEPS = \
1515
coverage \
1616
flake8 \
1717
flake8-print \
18+
isort \
1819
pyperf \
1920
pypinfo \
2021
requests \
@@ -187,19 +188,30 @@ test-coverage: ## Run test coverage.
187188
# Linters
188189
# ===================================================================
189190

190-
lint-py: ## Run Python (flake8) linter.
191+
check-flake8: ## Run flake8 linter.
191192
@git ls-files '*.py' | xargs $(PYTHON) -m flake8 --config=.flake8
192193

193-
lint-c: ## Run C linter.
194+
check-imports: ## Run isort linter.
195+
@git ls-files '*.py' | xargs $(PYTHON) -m isort --settings=.isort.cfg --check-only
196+
197+
check-c-code: ## Run C linter.
194198
@git ls-files '*.c' '*.h' | xargs $(PYTHON) scripts/internal/clinter.py
195199

196-
lint: ## Run Python (flake8) and C linters.
197-
${MAKE} lint-py
198-
${MAKE} lint-c
200+
lint: ## Run all linters
201+
${MAKE} check-flake8
202+
${MAKE} check-imports
203+
${MAKE} check-c-code
199204

200-
fix-lint: ## Attempt to automatically fix some Python lint issues.
205+
# ===================================================================
206+
# Fixers
207+
# ===================================================================
208+
209+
fix-flake8: ## Attempt to automatically fix some Python flake8 issues.
201210
@git ls-files | grep \\.py$ | xargs $(PYTHON) -m flake8 --exit-zero | $(PYTHON) scripts/internal/fix_flake8.py
202211

212+
fix-imports: ## Fix imports with isort.
213+
@git ls-files '*.py' | xargs $(PYTHON) -m isort --settings=.isort.cfg
214+
203215
# ===================================================================
204216
# GIT
205217
# ===================================================================

psutil/__init__.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"""
2222

2323
from __future__ import division
24+
2425
import collections
2526
import contextlib
2627
import datetime
@@ -31,25 +32,16 @@
3132
import sys
3233
import threading
3334
import time
35+
36+
3437
try:
3538
import pwd
3639
except ImportError:
3740
pwd = None
3841

3942
from . import _common
40-
from ._common import AccessDenied
41-
from ._common import Error
42-
from ._common import memoize_when_activated
43-
from ._common import NoSuchProcess
44-
from ._common import TimeoutExpired
45-
from ._common import wrap_numbers as _wrap_numbers
46-
from ._common import ZombieProcess
47-
from ._compat import long
48-
from ._compat import PermissionError
49-
from ._compat import ProcessLookupError
50-
from ._compat import SubprocessTimeoutExpired as _SubprocessTimeoutExpired
51-
from ._compat import PY3 as _PY3
52-
43+
from ._common import AIX
44+
from ._common import BSD
5345
from ._common import CONN_CLOSE
5446
from ._common import CONN_CLOSE_WAIT
5547
from ._common import CONN_CLOSING
@@ -62,9 +54,16 @@
6254
from ._common import CONN_SYN_RECV
6355
from ._common import CONN_SYN_SENT
6456
from ._common import CONN_TIME_WAIT
57+
from ._common import FREEBSD # NOQA
58+
from ._common import LINUX
59+
from ._common import MACOS
60+
from ._common import NETBSD # NOQA
6561
from ._common import NIC_DUPLEX_FULL
6662
from ._common import NIC_DUPLEX_HALF
6763
from ._common import NIC_DUPLEX_UNKNOWN
64+
from ._common import OPENBSD # NOQA
65+
from ._common import OSX # deprecated alias
66+
from ._common import POSIX # NOQA
6867
from ._common import POWER_TIME_UNKNOWN
6968
from ._common import POWER_TIME_UNLIMITED
7069
from ._common import STATUS_DEAD
@@ -79,26 +78,28 @@
7978
from ._common import STATUS_WAITING
8079
from ._common import STATUS_WAKING
8180
from ._common import STATUS_ZOMBIE
82-
83-
from ._common import AIX
84-
from ._common import BSD
85-
from ._common import FREEBSD # NOQA
86-
from ._common import LINUX
87-
from ._common import MACOS
88-
from ._common import NETBSD # NOQA
89-
from ._common import OPENBSD # NOQA
90-
from ._common import OSX # deprecated alias
91-
from ._common import POSIX # NOQA
9281
from ._common import SUNOS
9382
from ._common import WINDOWS
83+
from ._common import AccessDenied
84+
from ._common import Error
85+
from ._common import NoSuchProcess
86+
from ._common import TimeoutExpired
87+
from ._common import ZombieProcess
88+
from ._common import memoize_when_activated
89+
from ._common import wrap_numbers as _wrap_numbers
90+
from ._compat import PY3 as _PY3
91+
from ._compat import PermissionError
92+
from ._compat import ProcessLookupError
93+
from ._compat import SubprocessTimeoutExpired as _SubprocessTimeoutExpired
94+
from ._compat import long
95+
9496

9597
if LINUX:
9698
# This is public API and it will be retrieved from _pslinux.py
9799
# via sys.modules.
98100
PROCFS_PATH = "/proc"
99101

100102
from . import _pslinux as _psplatform
101-
102103
from ._pslinux import IOPRIO_CLASS_BE # NOQA
103104
from ._pslinux import IOPRIO_CLASS_IDLE # NOQA
104105
from ._pslinux import IOPRIO_CLASS_NONE # NOQA
@@ -113,10 +114,10 @@
113114
from ._psutil_windows import NORMAL_PRIORITY_CLASS # NOQA
114115
from ._psutil_windows import REALTIME_PRIORITY_CLASS # NOQA
115116
from ._pswindows import CONN_DELETE_TCB # NOQA
116-
from ._pswindows import IOPRIO_VERYLOW # NOQA
117+
from ._pswindows import IOPRIO_HIGH # NOQA
117118
from ._pswindows import IOPRIO_LOW # NOQA
118119
from ._pswindows import IOPRIO_NORMAL # NOQA
119-
from ._pswindows import IOPRIO_HIGH # NOQA
120+
from ._pswindows import IOPRIO_VERYLOW # NOQA
120121

121122
elif MACOS:
122123
from . import _psosx as _psplatform

psutil/_common.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
# Note: this module is imported by setup.py so it should not import
88
# psutil or third-party modules.
99

10-
from __future__ import division, print_function
10+
from __future__ import division
11+
from __future__ import print_function
1112

1213
import collections
1314
import contextlib
@@ -24,6 +25,7 @@
2425
from socket import SOCK_DGRAM
2526
from socket import SOCK_STREAM
2627

28+
2729
try:
2830
from socket import AF_INET6
2931
except ImportError:

psutil/_compat.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import sys
1616
import types
1717

18+
1819
__all__ = [
1920
# constants
2021
"PY3",
@@ -413,8 +414,8 @@ def _access_check(fn, mode):
413414
def get_terminal_size(fallback=(80, 24)):
414415
try:
415416
import fcntl
416-
import termios
417417
import struct
418+
import termios
418419
except ImportError:
419420
return fallback
420421
else:

psutil/_psaix.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@
1818
from . import _psposix
1919
from . import _psutil_aix as cext
2020
from . import _psutil_posix as cext_posix
21-
from ._common import AccessDenied
22-
from ._common import conn_to_ntuple
23-
from ._common import get_procfs_path
24-
from ._common import memoize_when_activated
2521
from ._common import NIC_DUPLEX_FULL
2622
from ._common import NIC_DUPLEX_HALF
2723
from ._common import NIC_DUPLEX_UNKNOWN
24+
from ._common import AccessDenied
2825
from ._common import NoSuchProcess
29-
from ._common import usage_percent
3026
from ._common import ZombieProcess
27+
from ._common import conn_to_ntuple
28+
from ._common import get_procfs_path
29+
from ._common import memoize_when_activated
30+
from ._common import usage_percent
31+
from ._compat import PY3
3132
from ._compat import FileNotFoundError
3233
from ._compat import PermissionError
3334
from ._compat import ProcessLookupError
34-
from ._compat import PY3
3535

3636

3737
__extra__all__ = ["PROCFS_PATH"]

psutil/_psbsd.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,24 @@
99
import functools
1010
import os
1111
import xml.etree.ElementTree as ET
12-
from collections import namedtuple
1312
from collections import defaultdict
13+
from collections import namedtuple
1414

1515
from . import _common
1616
from . import _psposix
1717
from . import _psutil_bsd as cext
1818
from . import _psutil_posix as cext_posix
19+
from ._common import FREEBSD
20+
from ._common import NETBSD
21+
from ._common import OPENBSD
1922
from ._common import AccessDenied
23+
from ._common import NoSuchProcess
24+
from ._common import ZombieProcess
2025
from ._common import conn_tmap
2126
from ._common import conn_to_ntuple
22-
from ._common import FREEBSD
2327
from ._common import memoize
2428
from ._common import memoize_when_activated
25-
from ._common import NETBSD
26-
from ._common import NoSuchProcess
27-
from ._common import OPENBSD
2829
from ._common import usage_percent
29-
from ._common import ZombieProcess
3030
from ._compat import FileNotFoundError
3131
from ._compat import PermissionError
3232
from ._compat import ProcessLookupError

psutil/_pslinux.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,31 @@
2525
from . import _psposix
2626
from . import _psutil_linux as cext
2727
from . import _psutil_posix as cext_posix
28+
from ._common import NIC_DUPLEX_FULL
29+
from ._common import NIC_DUPLEX_HALF
30+
from ._common import NIC_DUPLEX_UNKNOWN
2831
from ._common import AccessDenied
32+
from ._common import NoSuchProcess
33+
from ._common import ZombieProcess
2934
from ._common import debug
3035
from ._common import decode
3136
from ._common import get_procfs_path
3237
from ._common import isfile_strict
3338
from ._common import memoize
3439
from ._common import memoize_when_activated
35-
from ._common import NIC_DUPLEX_FULL
36-
from ._common import NIC_DUPLEX_HALF
37-
from ._common import NIC_DUPLEX_UNKNOWN
38-
from ._common import NoSuchProcess
3940
from ._common import open_binary
4041
from ._common import open_text
4142
from ._common import parse_environ_block
4243
from ._common import path_exists_strict
4344
from ._common import supports_ipv6
4445
from ._common import usage_percent
45-
from ._common import ZombieProcess
46-
from ._compat import b
47-
from ._compat import basestring
46+
from ._compat import PY3
4847
from ._compat import FileNotFoundError
4948
from ._compat import PermissionError
5049
from ._compat import ProcessLookupError
51-
from ._compat import PY3
50+
from ._compat import b
51+
from ._compat import basestring
52+
5253

5354
if sys.version_info >= (3, 4):
5455
import enum

0 commit comments

Comments
 (0)