Skip to content

Commit

Permalink
Fix pylint warnings / cleanup (#2218)
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Apr 1, 2023
1 parent 7d55ce4 commit 7eadee3
Show file tree
Hide file tree
Showing 30 changed files with 146 additions and 143 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/issues.py
Expand Up @@ -222,7 +222,7 @@ def should_add(issue, label):
issue.add_to_labels(label)


def _guess_labels_from_text(issue, text):
def _guess_labels_from_text(text):
assert isinstance(text, str), text
for label, keywords in LABELS_MAP.items():
for keyword in keywords:
Expand All @@ -232,7 +232,7 @@ def _guess_labels_from_text(issue, text):

def add_labels_from_text(issue, text):
assert isinstance(text, str), text
for label, keyword in _guess_labels_from_text(issue, text):
for label, keyword in _guess_labels_from_text(text):
add_label(issue, label)


Expand Down
15 changes: 10 additions & 5 deletions Makefile
Expand Up @@ -22,6 +22,7 @@ PY3_DEPS = \
flake8-quotes \
isort \
pep8-naming \
pylint \
pyperf \
pypinfo \
requests \
Expand All @@ -36,6 +37,7 @@ PY2_DEPS = \
mock
PY_DEPS = `$(PYTHON) -c \
"import sys; print('$(PY3_DEPS)' if sys.version_info[0] == 3 else '$(PY2_DEPS)')"`
NUM_WORKERS = `$(PYTHON) -c "import os; print(os.cpu_count() or 1)"`
# "python3 setup.py build" can be parallelized on Python >= 3.6.
BUILD_OPTS = `$(PYTHON) -c \
"import sys, os; \
Expand Down Expand Up @@ -191,10 +193,13 @@ test-coverage: ## Run test coverage.
# ===================================================================

flake8: ## Run flake8 linter.
@git ls-files '*.py' | xargs $(PYTHON) -m flake8 --config=.flake8
@git ls-files '*.py' | xargs $(PYTHON) -m flake8 --config=.flake8 --jobs=${NUM_WORKERS}

isort: ## Run isort linter.
@git ls-files '*.py' | xargs $(PYTHON) -m isort --check-only
@git ls-files '*.py' | xargs $(PYTHON) -m isort --check-only --jobs=${NUM_WORKERS}

pylint: ## Python pylint (not mandatory, just run it from time to time)
@git ls-files '*.py' | xargs $(PYTHON) -m pylint --rcfile=pyproject.toml --jobs=${NUM_WORKERS}

c-linter: ## Run C linter.
@git ls-files '*.c' '*.h' | xargs $(PYTHON) scripts/internal/clinter.py
Expand All @@ -209,11 +214,11 @@ lint-all: ## Run all linters
# ===================================================================

fix-flake8: ## Run autopep8, fix some Python flake8 / pep8 issues.
@git ls-files '*.py' | xargs $(PYTHON) -m autopep8 --in-place --jobs 0 --global-config=.flake8
@git ls-files '*.py' | xargs $(PYTHON) -m autoflake --in-place --jobs 0 --remove-all-unused-imports --remove-unused-variables --remove-duplicate-keys
@git ls-files '*.py' | xargs $(PYTHON) -m autopep8 --in-place --jobs=${NUM_WORKERS} --global-config=.flake8
@git ls-files '*.py' | xargs $(PYTHON) -m autoflake --in-place --jobs=${NUM_WORKERS} --remove-all-unused-imports --remove-unused-variables --remove-duplicate-keys

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

fix-all: ## Run all code fixers.
${MAKE} fix-flake8
Expand Down
2 changes: 1 addition & 1 deletion psutil/__init__.py
Expand Up @@ -516,7 +516,7 @@ def as_dict(self, attrs=None, ad_value=None):
"s" if len(invalid_names) > 1 else "",
", ".join(map(repr, invalid_names))))

retdict = dict()
retdict = {}
ls = attrs or valid_names
with self.oneshot():
for name in ls:
Expand Down
4 changes: 2 additions & 2 deletions psutil/_common.py
Expand Up @@ -43,7 +43,7 @@

# can't take it from _common.py as this script is imported by setup.py
PY3 = sys.version_info[0] == 3
PSUTIL_DEBUG = bool(os.getenv('PSUTIL_DEBUG', 0))
PSUTIL_DEBUG = bool(os.getenv('PSUTIL_DEBUG'))
_DEFAULT = object()

__all__ = [
Expand Down Expand Up @@ -924,7 +924,7 @@ def debug(msg):
"""If PSUTIL_DEBUG env var is set, print a debug message to stderr."""
if PSUTIL_DEBUG:
import inspect
fname, lineno, func_name, lines, index = inspect.getframeinfo(
fname, lineno, _, lines, index = inspect.getframeinfo(
inspect.currentframe().f_back)
if isinstance(msg, Exception):
if isinstance(msg, (OSError, IOError, EnvironmentError)):
Expand Down
4 changes: 2 additions & 2 deletions psutil/_compat.py
Expand Up @@ -254,7 +254,7 @@ def lru_cache(maxsize=100, typed=False):
http://docs.python.org/3/library/functools.html#functools.lru_cache
"""
def decorating_function(user_function):
cache = dict()
cache = {}
stats = [0, 0]
HITS, MISSES = 0, 1
make_key = _make_key
Expand Down Expand Up @@ -432,7 +432,7 @@ def get_terminal_size(fallback=(80, 24)):
try:
from subprocess import TimeoutExpired as SubprocessTimeoutExpired
except ImportError:
class SubprocessTimeoutExpired:
class SubprocessTimeoutExpired(Exception):
pass


Expand Down
4 changes: 2 additions & 2 deletions psutil/_psaix.py
Expand Up @@ -465,8 +465,8 @@ def gids(self):

@wrap_exceptions
def cpu_times(self):
cpu_times = cext.proc_cpu_times(self.pid, self._procfs_path)
return _common.pcputimes(*cpu_times)
t = cext.proc_cpu_times(self.pid, self._procfs_path)
return _common.pcputimes(*t)

@wrap_exceptions
def terminal(self):
Expand Down
6 changes: 3 additions & 3 deletions psutil/_pslinux.py
Expand Up @@ -327,8 +327,8 @@ class StructRlimit(ctypes.Structure):
pid, resource_, ctypes.byref(new), ctypes.byref(current))

if ret != 0:
errno = ctypes.get_errno()
raise OSError(errno, os.strerror(errno))
errno_ = ctypes.get_errno()
raise OSError(errno_, os.strerror(errno_))
return (current.rlim_cur, current.rlim_max)


Expand Down Expand Up @@ -1837,7 +1837,7 @@ def io_counters(self):
)
except KeyError as err:
raise ValueError("%r field was not found in %s; found fields "
"are %r" % (err[0], fname, fields))
"are %r" % (err.args[0], fname, fields))

@wrap_exceptions
def cpu_times(self):
Expand Down
2 changes: 1 addition & 1 deletion psutil/_pssunos.py
Expand Up @@ -143,7 +143,7 @@ def swap_memory():
p = subprocess.Popen(['/usr/bin/env', 'PATH=/usr/sbin:/sbin:%s' %
os.environ['PATH'], 'swap', '-l'],
stdout=subprocess.PIPE)
stdout, stderr = p.communicate()
stdout, _ = p.communicate()
if PY3:
stdout = stdout.decode(sys.stdout.encoding)
if p.returncode != 0:
Expand Down
2 changes: 1 addition & 1 deletion psutil/_pswindows.py
Expand Up @@ -699,7 +699,7 @@ def retry_error_partial_copy(fun):
def wrapper(self, *args, **kwargs):
delay = 0.0001
times = 33
for x in range(times): # retries for roughly 1 second
for _ in range(times): # retries for roughly 1 second
try:
return fun(self, *args, **kwargs)
except WindowsError as _:
Expand Down
23 changes: 9 additions & 14 deletions psutil/tests/__init__.py
Expand Up @@ -85,22 +85,19 @@
"HAS_CPU_AFFINITY", "HAS_CPU_FREQ", "HAS_ENVIRON", "HAS_PROC_IO_COUNTERS",
"HAS_IONICE", "HAS_MEMORY_MAPS", "HAS_PROC_CPU_NUM", "HAS_RLIMIT",
"HAS_SENSORS_BATTERY", "HAS_BATTERY", "HAS_SENSORS_FANS",
"HAS_SENSORS_TEMPERATURES", "HAS_MEMORY_FULL_INFO", "MACOS_11PLUS",
"HAS_SENSORS_TEMPERATURES", "MACOS_11PLUS",
"MACOS_12PLUS", "COVERAGE",
# subprocesses
'pyrun', 'terminate', 'reap_children', 'spawn_testproc', 'spawn_zombie',
'spawn_children_pair',
# threads
'ThreadTask'
'ThreadTask',
# test utils
'unittest', 'skip_on_access_denied', 'skip_on_not_implemented',
'retry_on_failure', 'TestMemoryLeak', 'PsutilTestCase',
'process_namespace', 'system_namespace', 'print_sysinfo',
# install utils
'install_pip', 'install_test_deps',
# fs utils
'chdir', 'safe_rmpath', 'create_exe', 'decode_path', 'encode_path',
'get_testfn',
'chdir', 'safe_rmpath', 'create_exe', 'get_testfn',
# os
'get_winver', 'kernel_version',
# sync primitives
Expand Down Expand Up @@ -458,7 +455,7 @@ def spawn_zombie():
zpid = int(conn.recv(1024))
_pids_started.add(zpid)
zombie = psutil.Process(zpid)
call_until(lambda: zombie.status(), "ret == psutil.STATUS_ZOMBIE")
call_until(zombie.status, "ret == psutil.STATUS_ZOMBIE")
return (parent, zombie)
finally:
conn.close()
Expand Down Expand Up @@ -628,7 +625,7 @@ def reap_children(recursive=False):
if children:
for p in children:
terminate(p, wait_timeout=None)
gone, alive = psutil.wait_procs(children, timeout=GLOBAL_TIMEOUT)
_, alive = psutil.wait_procs(children, timeout=GLOBAL_TIMEOUT)
for p in alive:
warn("couldn't terminate process %r; attempting kill()" % p)
terminate(p, sig=signal.SIGKILL)
Expand Down Expand Up @@ -999,7 +996,7 @@ def test_fun(self):
retries = 10 if CI_TESTING else 5
verbose = True
_thisproc = psutil.Process()
_psutil_debug_orig = bool(os.getenv('PSUTIL_DEBUG', 0))
_psutil_debug_orig = bool(os.getenv('PSUTIL_DEBUG'))

@classmethod
def setUpClass(cls):
Expand Down Expand Up @@ -1059,7 +1056,7 @@ def _call_ntimes(self, fun, times):
diff = mem2 - mem1 # can also be negative
return diff

def _check_mem(self, fun, times, warmup_times, retries, tolerance):
def _check_mem(self, fun, times, retries, tolerance):
messages = []
prev_mem = 0
increase = times
Expand Down Expand Up @@ -1104,8 +1101,7 @@ def execute(self, fun, times=None, warmup_times=None, retries=None,

self._call_ntimes(fun, warmup_times) # warm up
self._check_fds(fun)
self._check_mem(fun, times=times, warmup_times=warmup_times,
retries=retries, tolerance=tolerance)
self._check_mem(fun, times=times, retries=retries, tolerance=tolerance)

def execute_w_exc(self, exc, fun, **kwargs):
"""Convenience method to test a callable while making sure it
Expand All @@ -1122,7 +1118,6 @@ def print_sysinfo():
import datetime
import getpass
import locale
import platform
import pprint
try:
import pip
Expand Down Expand Up @@ -1616,7 +1611,7 @@ def check_net_address(addr, family):
elif family == psutil.AF_LINK:
assert re.match(r'([a-fA-F0-9]{2}[:|\-]?){6}', addr) is not None, addr
else:
raise ValueError("unknown family %r", family)
raise ValueError("unknown family %r" % family)


def check_connection_ntuple(conn):
Expand Down
2 changes: 1 addition & 1 deletion psutil/tests/test_bsd.py
Expand Up @@ -36,7 +36,7 @@

PAGESIZE = getpagesize()
# muse requires root privileges
MUSE_AVAILABLE = True if os.getuid() == 0 and which('muse') else False
MUSE_AVAILABLE = os.getuid() == 0 and which('muse')
else:
PAGESIZE = None
MUSE_AVAILABLE = False
Expand Down
2 changes: 1 addition & 1 deletion psutil/tests/test_connections.py
Expand Up @@ -501,7 +501,7 @@ def test_multi_sockets_procs(self):
pids = []
times = 10
fnames = []
for i in range(times):
for _ in range(times):
fname = self.get_testfn()
fnames.append(fname)
src = textwrap.dedent("""\
Expand Down
2 changes: 1 addition & 1 deletion psutil/tests/test_contracts.py
Expand Up @@ -176,7 +176,7 @@ def test_rlimit(self):

def test_io_counters(self):
hasit = hasattr(psutil.Process, "io_counters")
self.assertEqual(hasit, False if MACOS or SUNOS else True)
self.assertEqual(hasit, not (MACOS or SUNOS))

def test_num_fds(self):
self.assertEqual(hasattr(psutil.Process, "num_fds"), POSIX)
Expand Down
18 changes: 9 additions & 9 deletions psutil/tests/test_linux.py
Expand Up @@ -749,7 +749,7 @@ def test_emulate_fallbacks(self):
# this way we'll fall back on relying on /proc/stat
with mock_open_content('/proc/cpuinfo', b"") as m:
self.assertEqual(psutil._pslinux.cpu_count_logical(), original)
m.called
assert m.called


@unittest.skipIf(not LINUX, "LINUX only")
Expand Down Expand Up @@ -817,8 +817,8 @@ def path_exists_mock(path):
self.assertEqual(ret.max, 0.0)
self.assertEqual(ret.min, 0.0)
for freq in psutil.cpu_freq(percpu=True):
self.assertEqual(ret.max, 0.0)
self.assertEqual(ret.min, 0.0)
self.assertEqual(freq.max, 0.0)
self.assertEqual(freq.min, 0.0)
finally:
reload_module(psutil._pslinux)
reload_module(psutil)
Expand Down Expand Up @@ -1057,7 +1057,7 @@ class TestSystemNetIOCounters(PsutilTestCase):
def test_against_ifconfig(self):
def ifconfig(nic):
ret = {}
out = sh("ifconfig %s" % name)
out = sh("ifconfig %s" % nic)
ret['packets_recv'] = int(
re.findall(r'RX packets[: ](\d+)', out)[0])
ret['packets_sent'] = int(
Expand Down Expand Up @@ -1150,7 +1150,7 @@ def df(path):

for part in psutil.disk_partitions(all=False):
usage = psutil.disk_usage(part.mountpoint)
dev, total, used, free = df(part.mountpoint)
_, total, used, free = df(part.mountpoint)
self.assertEqual(usage.total, total)
self.assertAlmostEqual(usage.free, free,
delta=TOLERANCE_DISK_USAGE)
Expand Down Expand Up @@ -2034,28 +2034,28 @@ def test_threads_mocked(self):
# which no longer exists by the time we open() it (race
# condition). threads() is supposed to ignore that instead
# of raising NSP.
def open_mock(name, *args, **kwargs):
def open_mock_1(name, *args, **kwargs):
if name.startswith('/proc/%s/task' % os.getpid()):
raise IOError(errno.ENOENT, "")
else:
return orig_open(name, *args, **kwargs)

orig_open = open
patch_point = 'builtins.open' if PY3 else '__builtin__.open'
with mock.patch(patch_point, side_effect=open_mock) as m:
with mock.patch(patch_point, side_effect=open_mock_1) as m:
ret = psutil.Process().threads()
assert m.called
self.assertEqual(ret, [])

# ...but if it bumps into something != ENOENT we want an
# exception.
def open_mock(name, *args, **kwargs):
def open_mock_2(name, *args, **kwargs):
if name.startswith('/proc/%s/task' % os.getpid()):
raise IOError(errno.EPERM, "")
else:
return orig_open(name, *args, **kwargs)

with mock.patch(patch_point, side_effect=open_mock):
with mock.patch(patch_point, side_effect=open_mock_2):
self.assertRaises(psutil.AccessDenied, psutil.Process().threads)

def test_exe_mocked(self):
Expand Down

0 comments on commit 7eadee3

Please sign in to comment.