Skip to content

Commit

Permalink
fix minor linting errors and warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
wookayin committed Nov 24, 2023
1 parent 313b58d commit f1464ea
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions gpustat/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
TypedDict = None
# pyright: reportOptionalOperand = false
# pyright: reportTypedDictNotRequiredAccess = false
# pylint: disable=redefined-builtin

import json
import locale
Expand All @@ -30,8 +31,8 @@
import psutil
from blessed import Terminal

import gpustat.util as util
import gpustat.nvml as nvml
from gpustat import util
from gpustat import nvml
from gpustat.nvml import pynvml as N
from gpustat.nvml import check_driver_nvml_version

Expand Down Expand Up @@ -225,7 +226,7 @@ def _conditional(cond_fn, true_value, false_value,
error_value=term.bold_black):
try:
return cond_fn() and true_value or false_value
except Exception:
except Exception: # pylint: disable=broad-exception-caught
return error_value

_ENC_THRESHOLD = 50
Expand Down Expand Up @@ -260,7 +261,7 @@ def _conditional(cond_fn, true_value, false_value,
for k in list(colors.keys()):
colors[k] = ''

def _repr(v, none_value='??'):
def _repr(v, none_value: Any = '??'):
return none_value if v is None else v

# build one-line display information
Expand All @@ -271,6 +272,7 @@ def _repr(v, none_value='??'):
def _write(*args, color=None, end=''):
args = [str(x) for x in args]
if color:
# pylint: disable-next=consider-using-get
if color in colors:
color = colors[color]
args = [color] + args + [term.normal]
Expand All @@ -284,16 +286,16 @@ def rjustify(x, size):
# Null-safe property accessor like self.xxxx,
# but fall backs to '?' for None or missing values
class SafePropertyAccessor:
def __init__(self):
pass
def __getattr__(_, name): # type: ignore
def __init__(self, obj):
self.obj = obj
def __getattr__(self, name): # type: ignore
try:
v = getattr(self, name)
v = getattr(self.obj, name)
return v if v is not None else '??'
except TypeError: # possibly int(None), etc.
return '??'

safe_self = cast(GPUStat, SafePropertyAccessor())
safe_self = cast(GPUStat, SafePropertyAccessor(self))

_write(f"[{self.index}]", color=term.cyan)
_write(" ")
Expand Down Expand Up @@ -368,7 +370,7 @@ def full_process_info(p: ProcessInfo):
r += "{C0}({CCPUUtil}{:4.0f}%{C0}, {CCPUMemU}{:>6}{C0})".format(
_repr(p['cpu_percent'], '--'),
util.bytes2human(_repr(p['cpu_memory_usage'], 0)), **colors
)
) # type: ignore
full_command_pretty = util.prettify_commandline(
p['full_command'], colors['C1'], colors['CCmd'])
r += "{C0}: {CCmd}{}{C0}".format(
Expand All @@ -383,7 +385,7 @@ def full_process_info(p: ProcessInfo):
# None (not available)
_write(' ', '(', NOT_SUPPORTED, ')')
elif not no_processes:
for p in processes:
for p in (processes or []):
_write(' ', process_repr(p))
if show_full_cmd:
full_processes.append(eol_char + full_process_info(p))
Expand All @@ -404,15 +406,17 @@ def jsonify(self):

class InvalidGPU(GPUStat):
class FallbackDict(dict):
# pylint: disable-next=useless-return
def __missing__(self, key):
del key
return None

def __init__(self, gpu_index, message, ex):
super().__init__(self.FallbackDict(
index=gpu_index,
name=message,
processes=None
))
processes=None,
)) # type: ignore
self.exception = ex

@property
Expand Down Expand Up @@ -449,7 +453,8 @@ def new_query(debug=False, id=None) -> 'GPUStatCollection':

def _decode(b: Union[str, bytes]) -> str:
if isinstance(b, bytes):
return b.decode('utf-8') # for python3, to unicode
return b.decode('utf-8')
assert isinstance(b, str)
return b

def get_gpu_info(handle: NVMLHandle) -> NvidiaGPUInfo:
Expand Down Expand Up @@ -664,9 +669,10 @@ def print_formatted(self, fp=sys.stdout, *,
t_color = Terminal(kind=TERM, force_styling=True)

# workaround of issue #32 (watch doesn't recognize sgr0 characters)
t_color._normal = '\x1b[0;10m'
# pylint: disable-next=protected-access
t_color._normal = '\x1b[0;10m' # type: ignore
elif no_color:
t_color = Terminal(force_styling=None)
t_color = Terminal(force_styling=None) # type: ignore
else:
t_color = Terminal() # auto, depending on isatty

Expand Down

0 comments on commit f1464ea

Please sign in to comment.