Skip to content

Commit f1d1a68

Browse files
committed
[psutil] Annotate few common attributes
1 parent 1dad9a5 commit f1d1a68

File tree

3 files changed

+141
-117
lines changed

3 files changed

+141
-117
lines changed

stubs/psutil/psutil/_common.pyi

Lines changed: 90 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,70 @@
11
import enum
2-
from _typeshed import Incomplete, StrOrBytesPath, SupportsWrite
2+
import io
3+
import threading
4+
from _typeshed import ConvertibleToFloat, FileDescriptorOrPath, Incomplete, StrOrBytesPath, SupportsWrite
35
from collections.abc import Callable
46
from socket import AF_INET6 as AF_INET6, AddressFamily, SocketKind
5-
from typing import Literal, NamedTuple, TypeVar, overload
6-
7-
POSIX: bool
8-
WINDOWS: bool
9-
LINUX: bool
10-
MACOS: bool
11-
OSX: bool
12-
FREEBSD: bool
13-
OPENBSD: bool
14-
NETBSD: bool
15-
BSD: bool
16-
SUNOS: bool
17-
AIX: bool
18-
19-
STATUS_RUNNING: Literal["running"]
20-
STATUS_SLEEPING: Literal["sleeping"]
21-
STATUS_DISK_SLEEP: Literal["disk-sleep"]
22-
STATUS_STOPPED: Literal["stopped"]
23-
STATUS_TRACING_STOP: Literal["tracing-stop"]
24-
STATUS_ZOMBIE: Literal["zombie"]
25-
STATUS_DEAD: Literal["dead"]
26-
STATUS_WAKE_KILL: Literal["wake-kill"]
27-
STATUS_WAKING: Literal["waking"]
28-
STATUS_IDLE: Literal["idle"]
29-
STATUS_LOCKED: Literal["locked"]
30-
STATUS_WAITING: Literal["waiting"]
31-
STATUS_SUSPENDED: Literal["suspended"]
32-
STATUS_PARKED: Literal["parked"]
33-
34-
CONN_ESTABLISHED: str
35-
CONN_SYN_SENT: str
36-
CONN_SYN_RECV: str
37-
CONN_FIN_WAIT1: str
38-
CONN_FIN_WAIT2: str
39-
CONN_TIME_WAIT: str
40-
CONN_CLOSE: str
41-
CONN_CLOSE_WAIT: str
42-
CONN_LAST_ACK: str
43-
CONN_LISTEN: str
44-
CONN_CLOSING: str
45-
CONN_NONE: str
46-
NIC_DUPLEX_FULL: int
47-
NIC_DUPLEX_HALF: int
48-
NIC_DUPLEX_UNKNOWN: int
7+
from typing import BinaryIO, Final, NamedTuple, SupportsIndex, TypeVar, overload
8+
from typing_extensions import ParamSpec
9+
10+
POSIX: Final[bool]
11+
WINDOWS: Final[bool]
12+
LINUX: Final[bool]
13+
MACOS: Final[bool]
14+
OSX: Final[bool]
15+
FREEBSD: Final[bool]
16+
OPENBSD: Final[bool]
17+
NETBSD: Final[bool]
18+
BSD: Final[bool]
19+
SUNOS: Final[bool]
20+
AIX: Final[bool]
21+
22+
STATUS_RUNNING: Final = "running"
23+
STATUS_SLEEPING: Final = "sleeping"
24+
STATUS_DISK_SLEEP: Final = "disk-sleep"
25+
STATUS_STOPPED: Final = "stopped"
26+
STATUS_TRACING_STOP: Final = "tracing-stop"
27+
STATUS_ZOMBIE: Final = "zombie"
28+
STATUS_DEAD: Final = "dead"
29+
STATUS_WAKE_KILL: Final = "wake-kill"
30+
STATUS_WAKING: Final = "waking"
31+
STATUS_IDLE: Final = "idle"
32+
STATUS_LOCKED: Final = "locked"
33+
STATUS_WAITING: Final = "waiting"
34+
STATUS_SUSPENDED: Final = "suspended"
35+
STATUS_PARKED: Final = "parked"
36+
37+
CONN_ESTABLISHED: Final = "ESTABLISHED"
38+
CONN_SYN_SENT: Final = "SYN_SENT"
39+
CONN_SYN_RECV: Final = "SYN_RECV"
40+
CONN_FIN_WAIT1: Final = "FIN_WAIT1"
41+
CONN_FIN_WAIT2: Final = "FIN_WAIT2"
42+
CONN_TIME_WAIT: Final = "TIME_WAIT"
43+
CONN_CLOSE: Final = "CLOSE"
44+
CONN_CLOSE_WAIT: Final = "CLOSE_WAIT"
45+
CONN_LAST_ACK: Final = "LAST_ACK"
46+
CONN_LISTEN: Final = "LISTEN"
47+
CONN_CLOSING: Final = "CLOSING"
48+
CONN_NONE: Final = "NONE"
4949

5050
class NicDuplex(enum.IntEnum):
5151
NIC_DUPLEX_FULL = 2
5252
NIC_DUPLEX_HALF = 1
5353
NIC_DUPLEX_UNKNOWN = 0
5454

55-
POWER_TIME_UNKNOWN: int
56-
POWER_TIME_UNLIMITED: int
55+
NIC_DUPLEX_FULL: Final = NicDuplex.NIC_DUPLEX_FULL
56+
NIC_DUPLEX_HALF: Final = NicDuplex.NIC_DUPLEX_HALF
57+
NIC_DUPLEX_UNKNOWN: Final = NicDuplex.NIC_DUPLEX_UNKNOWN
5758

5859
class BatteryTime(enum.IntEnum):
5960
POWER_TIME_UNKNOWN = -1
6061
POWER_TIME_UNLIMITED = -2
6162

62-
ENCODING: str
63-
ENCODING_ERRS: str
63+
POWER_TIME_UNKNOWN: Final = BatteryTime.POWER_TIME_UNKNOWN
64+
POWER_TIME_UNLIMITED: Final = BatteryTime.POWER_TIME_UNLIMITED
65+
66+
ENCODING: Final[str]
67+
ENCODING_ERRS: Final[str]
6468

6569
class sswap(NamedTuple):
6670
total: int
@@ -210,44 +214,42 @@ class addr(NamedTuple):
210214
conn_tmap: dict[str, tuple[list[AddressFamily], list[SocketKind]]]
211215

212216
class Error(Exception):
213-
__module__: str
214-
msg: Incomplete
217+
msg: str
215218
def __init__(self, msg: str = ...) -> None: ...
216219

217220
class NoSuchProcess(Error):
218-
__module__: str
219-
pid: Incomplete
220-
name: Incomplete
221-
msg: Incomplete
222-
def __init__(self, pid, name=None, msg=None) -> None: ...
221+
pid: int
222+
name: str | None
223+
msg: str
224+
def __init__(self, pid: int, name: str | None = None, msg: str | None = None) -> None: ...
223225

224226
class ZombieProcess(NoSuchProcess):
225-
__module__: str
226-
pid: Incomplete
227-
ppid: Incomplete
228-
name: Incomplete
229-
msg: Incomplete
230-
def __init__(self, pid, name=None, ppid=None, msg=None) -> None: ...
227+
ppid: int | None
228+
def __init__(self, pid: int, name: str | None = None, ppid: int | None = None, msg: str | None = None) -> None: ...
231229

232230
class AccessDenied(Error):
233-
__module__: str
234-
pid: Incomplete
235-
name: Incomplete
236-
msg: Incomplete
237-
def __init__(self, pid=None, name=None, msg=None) -> None: ...
231+
pid: int | None
232+
name: str | None
233+
msg: str
234+
def __init__(self, pid: int | None = None, name: str | None = None, msg: str | None = None) -> None: ...
238235

239236
class TimeoutExpired(Error):
240-
__module__: str
241-
seconds: Incomplete
242-
pid: Incomplete
243-
name: Incomplete
244-
def __init__(self, seconds, pid=None, name=None) -> None: ...
237+
seconds: float
238+
pid: int | None
239+
name: str | None
240+
msg: str
241+
def __init__(self, seconds: float, pid: int | None = None, name: str | None = None) -> None: ...
242+
243+
_P = ParamSpec("_P")
244+
_R = TypeVar("_R")
245+
246+
def usage_percent(used: ConvertibleToFloat, total: float, round_: SupportsIndex | None = None) -> float: ...
245247

246-
_Func = TypeVar("_Func", bound=Callable[..., Incomplete])
248+
# returned function has `cache_clear()` attribute:
249+
def memoize(fun: Callable[_P, _R]) -> Callable[_P, _R]: ...
247250

248-
def usage_percent(used, total, round_: int | None = None) -> float: ...
249-
def memoize(fun: _Func) -> _Func: ...
250-
def memoize_when_activated(fun: _Func) -> _Func: ...
251+
# returned function has `cache_activate(proc)` and `cache_deactivate(proc)` attributes:
252+
def memoize_when_activated(fun: Callable[_P, _R]) -> Callable[_P, _R]: ...
251253
def isfile_strict(path: StrOrBytesPath) -> bool: ...
252254
def path_exists_strict(path: StrOrBytesPath) -> bool: ...
253255
def supports_ipv6() -> bool: ...
@@ -258,29 +260,30 @@ def socktype_to_enum(num: int) -> SocketKind: ...
258260
def conn_to_ntuple(fd: int, fam: int, type_: int, laddr, raddr, status: str, status_map, pid: int) -> sconn: ...
259261
@overload
260262
def conn_to_ntuple(fd: int, fam: int, type_: int, laddr, raddr, status: str, status_map, pid: None = None) -> pconn: ...
261-
def deprecated_method(replacement: str) -> Callable[[_Func], _Func]: ...
263+
def deprecated_method(replacement: str) -> Callable[[Callable[_P, _R]], Callable[_P, _R]]: ...
262264

263265
class _WrapNumbers:
264-
lock: Incomplete
265-
cache: Incomplete
266-
reminders: Incomplete
267-
reminder_keys: Incomplete
266+
lock: threading.Lock
267+
cache: dict[Incomplete, Incomplete]
268+
reminders: dict[Incomplete, Incomplete]
269+
reminder_keys: dict[Incomplete, Incomplete]
268270
def __init__(self) -> None: ...
269271
def run(self, input_dict, name): ...
270272
def cache_clear(self, name=None) -> None: ...
271-
def cache_info(self): ...
273+
def cache_info(self) -> tuple[dict[Incomplete, Incomplete], dict[Incomplete, Incomplete], dict[Incomplete, Incomplete]]: ...
272274

273275
def wrap_numbers(input_dict, name: str): ...
274-
def open_binary(fname): ...
275-
def open_text(fname): ...
276-
def cat(fname, fallback=..., _open=...): ...
277-
def bcat(fname, fallback=...): ...
276+
def open_binary(fname: FileDescriptorOrPath) -> BinaryIO: ...
277+
def open_text(fname: FileDescriptorOrPath) -> io.TextIOWrapper: ...
278+
def cat(fname: FileDescriptorOrPath, fallback=..., _open=...): ...
279+
def bcat(fname: FileDescriptorOrPath, fallback=...): ...
278280
def bytes2human(n: int, format: str = "%(value).1f%(symbol)s") -> str: ...
279281
def get_procfs_path() -> str: ...
282+
def decode(s: bytes) -> str: ...
280283
def term_supports_colors(file: SupportsWrite[str] = ...) -> bool: ...
281284
def hilite(s: str, color: str | None = None, bold: bool = False) -> str: ...
282285
def print_color(s: str, color: str | None = None, bold: bool = False, file: SupportsWrite[str] = ...) -> None: ...
283-
def debug(msg) -> None: ...
286+
def debug(msg: str | Exception) -> None: ...
284287

285288
__all__ = [
286289
# OS constants

stubs/psutil/psutil/_psposix.pyi

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
1-
def pid_exists(pid): ...
2-
def wait_pid(pid, timeout=None, proc_name=None, _waitpid=..., _timer=..., _min=..., _sleep=..., _pid_exists=...): ...
3-
def disk_usage(path): ...
4-
def get_terminal_map(): ...
1+
import sys
2+
from _typeshed import FileDescriptorOrPath, StrOrBytesPath
3+
4+
from ._common import sdiskusage
5+
6+
def pid_exists(pid: int) -> bool: ...
7+
def wait_pid(
8+
pid: int,
9+
timeout: float | None = None,
10+
proc_name: str | None = None,
11+
_waitpid=...,
12+
_timer=...,
13+
_min=...,
14+
_sleep=...,
15+
_pid_exists=...,
16+
): ...
17+
18+
if sys.platform == "darwin":
19+
def disk_usage(path: StrOrBytesPath) -> sdiskusage: ...
20+
21+
else:
22+
def disk_usage(path: FileDescriptorOrPath) -> sdiskusage: ...
23+
24+
def get_terminal_map() -> dict[int, str]: ...
525

626
__all__ = ["pid_exists", "wait_pid", "disk_usage", "get_terminal_map"]
Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
import sys
2+
from typing import Final
23

34
if sys.platform == "linux":
4-
RLIMIT_AS: int
5-
RLIMIT_CORE: int
6-
RLIMIT_CPU: int
7-
RLIMIT_DATA: int
8-
RLIMIT_FSIZE: int
9-
RLIMIT_LOCKS: int
10-
RLIMIT_MEMLOCK: int
11-
RLIMIT_MSGQUEUE: int
12-
RLIMIT_NICE: int
13-
RLIMIT_NOFILE: int
14-
RLIMIT_NPROC: int
15-
RLIMIT_RSS: int
16-
RLIMIT_RTPRIO: int
17-
RLIMIT_RTTIME: int
18-
RLIMIT_SIGPENDING: int
19-
RLIMIT_STACK: int
20-
RLIM_INFINITY: int
5+
RLIMIT_AS: Final[int]
6+
RLIMIT_CORE: Final[int]
7+
RLIMIT_CPU: Final[int]
8+
RLIMIT_DATA: Final[int]
9+
RLIMIT_FSIZE: Final[int]
10+
RLIMIT_LOCKS: Final[int]
11+
RLIMIT_MEMLOCK: Final[int]
12+
RLIMIT_MSGQUEUE: Final[int]
13+
RLIMIT_NICE: Final[int]
14+
RLIMIT_NOFILE: Final[int]
15+
RLIMIT_NPROC: Final[int]
16+
RLIMIT_RSS: Final[int]
17+
RLIMIT_RTPRIO: Final[int]
18+
RLIMIT_RTTIME: Final[int]
19+
RLIMIT_SIGPENDING: Final[int]
20+
RLIMIT_STACK: Final[int]
21+
RLIM_INFINITY: Final[int]
2122

22-
def getpagesize(*args, **kwargs): ...
23-
def getpriority(*args, **kwargs): ...
24-
def net_if_addrs(*args, **kwargs): ...
25-
def net_if_flags(*args, **kwargs): ...
26-
def net_if_is_running(*args, **kwargs): ...
27-
def net_if_mtu(*args, **kwargs): ...
23+
def getpagesize() -> int: ...
24+
def getpriority(pid: int, /) -> int: ...
25+
def net_if_addrs() -> list[tuple[str, int, str | None, str | None, str | None, str | None]]: ...
26+
def net_if_flags(nic_name: str, /) -> list[str]: ...
27+
def net_if_is_running(nic_name: str, /) -> bool: ...
28+
def net_if_mtu(nic_name: str, /) -> int: ...
2829

2930
if sys.platform == "darwin":
30-
AF_LINK: int
31-
def net_if_duplex_speed(*args, **kwargs): ...
31+
AF_LINK: Final[int]
32+
def net_if_duplex_speed(nic_name: str, /) -> list[int]: ...
3233

33-
def setpriority(*args, **kwargs): ...
34+
def setpriority(pid: int, priority: int, /) -> None: ...

0 commit comments

Comments
 (0)