Skip to content

Commit fcc9ebc

Browse files
authored
[psutil] Use more strict pyright settings (#15109)
1 parent 0153c40 commit fcc9ebc

File tree

5 files changed

+66
-29
lines changed

5 files changed

+66
-29
lines changed

pyrightconfig.stricter.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
"stubs/pika",
7373
"stubs/pony",
7474
"stubs/protobuf",
75-
"stubs/psutil",
75+
"stubs/psutil/psutil/__init__.pyi",
7676
"stubs/psycopg2",
7777
"stubs/pyasn1",
7878
"stubs/pycurl",

stubs/psutil/psutil/__init__.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ class Process:
202202
def cpu_affinity(self, cpus: None = None) -> list[int]: ...
203203
@overload
204204
def cpu_affinity(self, cpus: list[int]) -> None: ...
205-
def memory_maps(self, grouped: bool = True): ...
205+
def memory_maps(self, grouped: bool = True) -> list[Incomplete]: ...
206206
if sys.platform == "linux":
207207
def rlimit(self, resource: int, limits: tuple[int, int] | None = ...) -> tuple[int, int]: ...
208208
def cpu_num(self) -> int: ...

stubs/psutil/psutil/_common.pyi

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import enum
22
import io
3+
import sys
34
import threading
45
from _typeshed import ConvertibleToFloat, FileDescriptorOrPath, Incomplete, StrOrBytesPath, SupportsWrite
6+
from collections import defaultdict
57
from collections.abc import Callable
68
from socket import AF_INET6 as AF_INET6, AddressFamily, SocketKind
79
from typing import BinaryIO, Final, NamedTuple, SupportsIndex, TypeVar, overload
@@ -213,9 +215,7 @@ class addr(NamedTuple):
213215

214216
conn_tmap: dict[str, tuple[list[AddressFamily], list[SocketKind]]]
215217

216-
class Error(Exception):
217-
msg: str
218-
def __init__(self, msg: str = ...) -> None: ...
218+
class Error(Exception): ...
219219

220220
class NoSuchProcess(Error):
221221
pid: int
@@ -242,6 +242,7 @@ class TimeoutExpired(Error):
242242

243243
_P = ParamSpec("_P")
244244
_R = TypeVar("_R")
245+
_T = TypeVar("_T")
245246

246247
def usage_percent(used: ConvertibleToFloat, total: float, round_: SupportsIndex | None = None) -> float: ...
247248

@@ -257,32 +258,64 @@ def parse_environ_block(data: str) -> dict[str, str]: ...
257258
def sockfam_to_enum(num: int) -> AddressFamily: ...
258259
def socktype_to_enum(num: int) -> SocketKind: ...
259260
@overload
260-
def conn_to_ntuple(fd: int, fam: int, type_: int, laddr, raddr, status: str, status_map, pid: int) -> sconn: ...
261+
def conn_to_ntuple(
262+
fd: int,
263+
fam: int,
264+
type_: int,
265+
laddr: addr | tuple[str, int] | tuple[()],
266+
raddr: addr | tuple[str, int] | tuple[()],
267+
status: int | str,
268+
status_map: dict[int, str] | dict[str, str],
269+
pid: int,
270+
) -> sconn: ...
261271
@overload
262-
def conn_to_ntuple(fd: int, fam: int, type_: int, laddr, raddr, status: str, status_map, pid: None = None) -> pconn: ...
272+
def conn_to_ntuple(
273+
fd: int,
274+
fam: int,
275+
type_: int,
276+
laddr: addr | tuple[str, int] | tuple[()],
277+
raddr: addr | tuple[str, int] | tuple[()],
278+
status: int | str,
279+
status_map: dict[int, str] | dict[str, str],
280+
pid: None = None,
281+
) -> pconn: ...
263282
def deprecated_method(replacement: str) -> Callable[[Callable[_P, _R]], Callable[_P, _R]]: ...
264283

265284
class _WrapNumbers:
266285
lock: threading.Lock
267-
cache: dict[Incomplete, Incomplete]
268-
reminders: dict[Incomplete, Incomplete]
269-
reminder_keys: dict[Incomplete, Incomplete]
286+
cache: dict[str, dict[str, tuple[int, ...]]]
287+
reminders: dict[str, defaultdict[Incomplete, int]]
288+
reminder_keys: dict[str, defaultdict[Incomplete, set[Incomplete]]]
270289
def __init__(self) -> None: ...
271-
def run(self, input_dict, name): ...
272-
def cache_clear(self, name=None) -> None: ...
273-
def cache_info(self) -> tuple[dict[Incomplete, Incomplete], dict[Incomplete, Incomplete], dict[Incomplete, Incomplete]]: ...
274-
275-
def wrap_numbers(input_dict, name: str): ...
290+
def run(self, input_dict: dict[str, tuple[int, ...]], name: str) -> dict[str, tuple[int, ...]]: ...
291+
def cache_clear(self, name: str | None = None) -> None: ...
292+
def cache_info(
293+
self,
294+
) -> tuple[
295+
dict[str, dict[str, tuple[int, ...]]],
296+
dict[str, defaultdict[Incomplete, int]],
297+
dict[str, defaultdict[Incomplete, set[Incomplete]]],
298+
]: ...
299+
300+
def wrap_numbers(input_dict: dict[str, tuple[int, ...]], name: str) -> dict[str, tuple[int, ...]]: ...
276301
def open_binary(fname: FileDescriptorOrPath) -> BinaryIO: ...
277302
def open_text(fname: FileDescriptorOrPath) -> io.TextIOWrapper: ...
278-
def cat(fname: FileDescriptorOrPath, fallback=..., _open=...): ...
279-
def bcat(fname: FileDescriptorOrPath, fallback=...): ...
303+
@overload
304+
def cat(fname: FileDescriptorOrPath, _open: Callable[[FileDescriptorOrPath], io.TextIOWrapper] = ...) -> str: ...
305+
@overload
306+
def cat(
307+
fname: FileDescriptorOrPath, fallback: _T = ..., _open: Callable[[FileDescriptorOrPath], io.TextIOWrapper] = ...
308+
) -> str | _T: ...
309+
@overload
310+
def bcat(fname: FileDescriptorOrPath) -> str: ...
311+
@overload
312+
def bcat(fname: FileDescriptorOrPath, fallback: _T = ...) -> str | _T: ...
280313
def bytes2human(n: int, format: str = "%(value).1f%(symbol)s") -> str: ...
281314
def get_procfs_path() -> str: ...
282315
def decode(s: bytes) -> str: ...
283-
def term_supports_colors(file: SupportsWrite[str] = ...) -> bool: ...
316+
def term_supports_colors(file: SupportsWrite[str] = sys.stdout) -> bool: ...
284317
def hilite(s: str, color: str | None = None, bold: bool = False) -> str: ...
285-
def print_color(s: str, color: str | None = None, bold: bool = False, file: SupportsWrite[str] = ...) -> None: ...
318+
def print_color(s: str, color: str | None = None, bold: bool = False, file: SupportsWrite[str] = sys.stdout) -> None: ...
286319
def debug(msg: str | Exception) -> None: ...
287320

288321
__all__ = [

stubs/psutil/psutil/_psosx.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ if sys.platform == "darwin":
108108
def num_ctx_switches(self) -> _common.pctxsw: ...
109109
def num_threads(self) -> int: ...
110110
def open_files(self) -> list[_common.popenfile]: ...
111-
def net_connections(self, kind: str = "inet"): ...
111+
def net_connections(self, kind: str = "inet") -> list[_common.pconn]: ...
112112
def num_fds(self) -> int: ...
113113
def wait(self, timeout: float | None = None) -> int | None: ...
114-
def nice_get(self): ...
115-
def nice_set(self, value): ...
114+
def nice_get(self) -> int: ...
115+
def nice_set(self, value: int) -> None: ...
116116
def status(self) -> str: ...
117117
def threads(self) -> list[_common.pthread]: ...

stubs/psutil/psutil/_pswindows.pyi

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import sys
33
if sys.platform == "win32":
44
import enum
55
from _typeshed import Incomplete
6-
from collections.abc import Iterable, Iterator
6+
from collections.abc import Callable, Iterable, Iterator
77
from signal import Signals
8-
from typing import Final, Literal, NamedTuple, TypedDict, overload, type_check_only
8+
from typing import Final, Literal, NamedTuple, TypedDict, TypeVar, overload, type_check_only
9+
from typing_extensions import ParamSpec
910

1011
from psutil import _psutil_windows
1112
from psutil._common import (
@@ -127,6 +128,9 @@ if sys.platform == "win32":
127128
other_count: int
128129
other_bytes: int
129130

131+
_P = ParamSpec("_P")
132+
_R = TypeVar("_R")
133+
130134
def convert_dos_path(s: str) -> str: ...
131135
def getpagesize() -> int: ...
132136
def virtual_memory() -> svmem: ...
@@ -186,11 +190,11 @@ if sys.platform == "win32":
186190

187191
def is_permission_err(exc: OSError) -> bool: ...
188192
@overload
189-
def convert_oserror(exc: PermissionError, pid=None, name=None) -> AccessDenied: ...
193+
def convert_oserror(exc: PermissionError, pid: int | None = None, name: str | None = None) -> AccessDenied: ...
190194
@overload
191-
def convert_oserror(exc: OSError, pid=None, name=None) -> AccessDenied | NoSuchProcess: ...
192-
def wrap_exceptions(fun): ...
193-
def retry_error_partial_copy(fun): ...
195+
def convert_oserror(exc: OSError, pid: int | None = None, name: str | None = None) -> AccessDenied | NoSuchProcess: ...
196+
def wrap_exceptions(fun: Callable[_P, _R]) -> Callable[_P, _R]: ...
197+
def retry_error_partial_copy(fun: Callable[_P, _R]) -> Callable[_P, _R]: ...
194198

195199
class Process:
196200
__slots__ = ["_cache", "_name", "_ppid", "pid"]
@@ -220,7 +224,7 @@ if sys.platform == "win32":
220224
def open_files(self) -> list[_common.popenfile]: ...
221225
def net_connections(self, kind: str = "inet") -> list[_common.pconn]: ...
222226
def nice_get(self) -> Priority: ...
223-
def nice_set(self, value) -> None: ...
227+
def nice_set(self, value: int) -> None: ...
224228
def ionice_get(self) -> IOPriority: ...
225229
def ionice_set(self, ioclass: int, value: None) -> None: ...
226230
def io_counters(self) -> pio: ...

0 commit comments

Comments
 (0)