Skip to content

Commit

Permalink
Automatically sort imports (isort CLI tool) (#2033)
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo authored Dec 14, 2021
1 parent b490b5d commit 39dc44b
Show file tree
Hide file tree
Showing 62 changed files with 264 additions and 169 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,14 @@ jobs:
- uses: actions/setup-python@v2
- name: 'Run linters'
run: |
# py2
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py
python2 get-pip.py
python2 -m pip install flake8
python3 -m pip install flake8
python2 -m flake8 .
# py3
python3 -m pip install flake8 isort
python3 -m flake8 .
echo "flake8 linting OK"
python3 -m isort --settings=.isort.cfg .
# clinter
find . -type f \( -iname "*.c" -o -iname "*.h" \) | xargs python3 scripts/internal/clinter.py
echo "C linting OK"
7 changes: 7 additions & 0 deletions .isort.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# See: https://pycqa.github.io/isort/docs/configuration/options

[settings]
# one import per line
force_single_line = true
# blank spaces after import section
lines_after_imports = 2
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
include .coveragerc
include .flake8
include .gitignore
include .isort.cfg
include CONTRIBUTING.md
include CREDITS
include HISTORY.rst
Expand Down
24 changes: 18 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ DEPS = \
coverage \
flake8 \
flake8-print \
isort \
pyperf \
pypinfo \
requests \
Expand Down Expand Up @@ -187,19 +188,30 @@ test-coverage: ## Run test coverage.
# Linters
# ===================================================================

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

lint-c: ## Run C linter.
check-imports: ## Run isort linter.
@git ls-files '*.py' | xargs $(PYTHON) -m isort --settings=.isort.cfg --check-only

check-c-code: ## Run C linter.
@git ls-files '*.c' '*.h' | xargs $(PYTHON) scripts/internal/clinter.py

lint: ## Run Python (flake8) and C linters.
${MAKE} lint-py
${MAKE} lint-c
lint: ## Run all linters
${MAKE} check-flake8
${MAKE} check-imports
${MAKE} check-c-code

fix-lint: ## Attempt to automatically fix some Python lint issues.
# ===================================================================
# Fixers
# ===================================================================

fix-flake8: ## Attempt to automatically fix some Python flake8 issues.
@git ls-files | grep \\.py$ | xargs $(PYTHON) -m flake8 --exit-zero | $(PYTHON) scripts/internal/fix_flake8.py

fix-imports: ## Fix imports with isort.
@git ls-files '*.py' | xargs $(PYTHON) -m isort --settings=.isort.cfg

# ===================================================================
# GIT
# ===================================================================
Expand Down
53 changes: 27 additions & 26 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"""

from __future__ import division

import collections
import contextlib
import datetime
Expand All @@ -31,25 +32,16 @@
import sys
import threading
import time


try:
import pwd
except ImportError:
pwd = None

from . import _common
from ._common import AccessDenied
from ._common import Error
from ._common import memoize_when_activated
from ._common import NoSuchProcess
from ._common import TimeoutExpired
from ._common import wrap_numbers as _wrap_numbers
from ._common import ZombieProcess
from ._compat import long
from ._compat import PermissionError
from ._compat import ProcessLookupError
from ._compat import SubprocessTimeoutExpired as _SubprocessTimeoutExpired
from ._compat import PY3 as _PY3

from ._common import AIX
from ._common import BSD
from ._common import CONN_CLOSE
from ._common import CONN_CLOSE_WAIT
from ._common import CONN_CLOSING
Expand All @@ -62,9 +54,16 @@
from ._common import CONN_SYN_RECV
from ._common import CONN_SYN_SENT
from ._common import CONN_TIME_WAIT
from ._common import FREEBSD # NOQA
from ._common import LINUX
from ._common import MACOS
from ._common import NETBSD # NOQA
from ._common import NIC_DUPLEX_FULL
from ._common import NIC_DUPLEX_HALF
from ._common import NIC_DUPLEX_UNKNOWN
from ._common import OPENBSD # NOQA
from ._common import OSX # deprecated alias
from ._common import POSIX # NOQA
from ._common import POWER_TIME_UNKNOWN
from ._common import POWER_TIME_UNLIMITED
from ._common import STATUS_DEAD
Expand All @@ -79,26 +78,28 @@
from ._common import STATUS_WAITING
from ._common import STATUS_WAKING
from ._common import STATUS_ZOMBIE

from ._common import AIX
from ._common import BSD
from ._common import FREEBSD # NOQA
from ._common import LINUX
from ._common import MACOS
from ._common import NETBSD # NOQA
from ._common import OPENBSD # NOQA
from ._common import OSX # deprecated alias
from ._common import POSIX # NOQA
from ._common import SUNOS
from ._common import WINDOWS
from ._common import AccessDenied
from ._common import Error
from ._common import NoSuchProcess
from ._common import TimeoutExpired
from ._common import ZombieProcess
from ._common import memoize_when_activated
from ._common import wrap_numbers as _wrap_numbers
from ._compat import PY3 as _PY3
from ._compat import PermissionError
from ._compat import ProcessLookupError
from ._compat import SubprocessTimeoutExpired as _SubprocessTimeoutExpired
from ._compat import long


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

from . import _pslinux as _psplatform

from ._pslinux import IOPRIO_CLASS_BE # NOQA
from ._pslinux import IOPRIO_CLASS_IDLE # NOQA
from ._pslinux import IOPRIO_CLASS_NONE # NOQA
Expand All @@ -113,10 +114,10 @@
from ._psutil_windows import NORMAL_PRIORITY_CLASS # NOQA
from ._psutil_windows import REALTIME_PRIORITY_CLASS # NOQA
from ._pswindows import CONN_DELETE_TCB # NOQA
from ._pswindows import IOPRIO_VERYLOW # NOQA
from ._pswindows import IOPRIO_HIGH # NOQA
from ._pswindows import IOPRIO_LOW # NOQA
from ._pswindows import IOPRIO_NORMAL # NOQA
from ._pswindows import IOPRIO_HIGH # NOQA
from ._pswindows import IOPRIO_VERYLOW # NOQA

elif MACOS:
from . import _psosx as _psplatform
Expand Down
4 changes: 3 additions & 1 deletion psutil/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
# Note: this module is imported by setup.py so it should not import
# psutil or third-party modules.

from __future__ import division, print_function
from __future__ import division
from __future__ import print_function

import collections
import contextlib
Expand All @@ -24,6 +25,7 @@
from socket import SOCK_DGRAM
from socket import SOCK_STREAM


try:
from socket import AF_INET6
except ImportError:
Expand Down
3 changes: 2 additions & 1 deletion psutil/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import sys
import types


__all__ = [
# constants
"PY3",
Expand Down Expand Up @@ -413,8 +414,8 @@ def _access_check(fn, mode):
def get_terminal_size(fallback=(80, 24)):
try:
import fcntl
import termios
import struct
import termios
except ImportError:
return fallback
else:
Expand Down
12 changes: 6 additions & 6 deletions psutil/_psaix.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@
from . import _psposix
from . import _psutil_aix as cext
from . import _psutil_posix as cext_posix
from ._common import AccessDenied
from ._common import conn_to_ntuple
from ._common import get_procfs_path
from ._common import memoize_when_activated
from ._common import NIC_DUPLEX_FULL
from ._common import NIC_DUPLEX_HALF
from ._common import NIC_DUPLEX_UNKNOWN
from ._common import AccessDenied
from ._common import NoSuchProcess
from ._common import usage_percent
from ._common import ZombieProcess
from ._common import conn_to_ntuple
from ._common import get_procfs_path
from ._common import memoize_when_activated
from ._common import usage_percent
from ._compat import PY3
from ._compat import FileNotFoundError
from ._compat import PermissionError
from ._compat import ProcessLookupError
from ._compat import PY3


__extra__all__ = ["PROCFS_PATH"]
Expand Down
12 changes: 6 additions & 6 deletions psutil/_psbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@
import functools
import os
import xml.etree.ElementTree as ET
from collections import namedtuple
from collections import defaultdict
from collections import namedtuple

from . import _common
from . import _psposix
from . import _psutil_bsd as cext
from . import _psutil_posix as cext_posix
from ._common import FREEBSD
from ._common import NETBSD
from ._common import OPENBSD
from ._common import AccessDenied
from ._common import NoSuchProcess
from ._common import ZombieProcess
from ._common import conn_tmap
from ._common import conn_to_ntuple
from ._common import FREEBSD
from ._common import memoize
from ._common import memoize_when_activated
from ._common import NETBSD
from ._common import NoSuchProcess
from ._common import OPENBSD
from ._common import usage_percent
from ._common import ZombieProcess
from ._compat import FileNotFoundError
from ._compat import PermissionError
from ._compat import ProcessLookupError
Expand Down
17 changes: 9 additions & 8 deletions psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,31 @@
from . import _psposix
from . import _psutil_linux as cext
from . import _psutil_posix as cext_posix
from ._common import NIC_DUPLEX_FULL
from ._common import NIC_DUPLEX_HALF
from ._common import NIC_DUPLEX_UNKNOWN
from ._common import AccessDenied
from ._common import NoSuchProcess
from ._common import ZombieProcess
from ._common import debug
from ._common import decode
from ._common import get_procfs_path
from ._common import isfile_strict
from ._common import memoize
from ._common import memoize_when_activated
from ._common import NIC_DUPLEX_FULL
from ._common import NIC_DUPLEX_HALF
from ._common import NIC_DUPLEX_UNKNOWN
from ._common import NoSuchProcess
from ._common import open_binary
from ._common import open_text
from ._common import parse_environ_block
from ._common import path_exists_strict
from ._common import supports_ipv6
from ._common import usage_percent
from ._common import ZombieProcess
from ._compat import b
from ._compat import basestring
from ._compat import PY3
from ._compat import FileNotFoundError
from ._compat import PermissionError
from ._compat import ProcessLookupError
from ._compat import PY3
from ._compat import b
from ._compat import basestring


if sys.version_info >= (3, 4):
import enum
Expand Down
4 changes: 2 additions & 2 deletions psutil/_psosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
from . import _psutil_osx as cext
from . import _psutil_posix as cext_posix
from ._common import AccessDenied
from ._common import NoSuchProcess
from ._common import ZombieProcess
from ._common import conn_tmap
from ._common import conn_to_ntuple
from ._common import isfile_strict
from ._common import memoize_when_activated
from ._common import NoSuchProcess
from ._common import parse_environ_block
from ._common import usage_percent
from ._common import ZombieProcess
from ._compat import PermissionError
from ._compat import ProcessLookupError

Expand Down
5 changes: 3 additions & 2 deletions psutil/_psposix.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@
import sys
import time

from ._common import TimeoutExpired
from ._common import memoize
from ._common import sdiskusage
from ._common import TimeoutExpired
from ._common import usage_percent
from ._compat import PY3
from ._compat import ChildProcessError
from ._compat import FileNotFoundError
from ._compat import InterruptedError
from ._compat import PermissionError
from ._compat import ProcessLookupError
from ._compat import PY3
from ._compat import unicode


if sys.version_info >= (3, 4):
import enum
else:
Expand Down
10 changes: 5 additions & 5 deletions psutil/_pssunos.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@
from . import _psposix
from . import _psutil_posix as cext_posix
from . import _psutil_sunos as cext
from ._common import AccessDenied
from ._common import AF_INET6
from ._common import AccessDenied
from ._common import NoSuchProcess
from ._common import ZombieProcess
from ._common import debug
from ._common import get_procfs_path
from ._common import isfile_strict
from ._common import memoize_when_activated
from ._common import NoSuchProcess
from ._common import sockfam_to_enum
from ._common import socktype_to_enum
from ._common import usage_percent
from ._common import ZombieProcess
from ._compat import b
from ._compat import PY3
from ._compat import FileNotFoundError
from ._compat import PermissionError
from ._compat import ProcessLookupError
from ._compat import PY3
from ._compat import b


__extra__all__ = ["CONN_IDLE", "CONN_BOUND", "PROCFS_PATH"]
Expand Down
Loading

0 comments on commit 39dc44b

Please sign in to comment.