Skip to content

Commit 8845352

Browse files
[psutil] Add cache_clear to psutil.process_iter typing (#14803)
1 parent 6a7f319 commit 8845352

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

stubs/psutil/@tests/stubtest_allowlist.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ psutil._pssunos
88

99
# Test utilities
1010
psutil.tests.*
11+
12+
# process_iter is enhanced with cache_clear method that's not detected by stubtest
13+
psutil.process_iter
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""Test cases for psutil.process_iter and its cache_clear method."""
2+
3+
from __future__ import annotations
4+
5+
import psutil
6+
7+
# Test that process_iter can be called as a function
8+
for proc in psutil.process_iter():
9+
break
10+
11+
# Test that process_iter has cache_clear method
12+
psutil.process_iter.cache_clear()
13+
14+
# Test that cache_clear is callable
15+
clear_method = psutil.process_iter.cache_clear
16+
clear_method()

stubs/psutil/psutil/__init__.pyi

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import sys
22
from _typeshed import Incomplete
33
from collections.abc import Callable, Iterable, Iterator
44
from contextlib import AbstractContextManager
5-
from typing import Any, Literal, overload
5+
from typing import Any, Literal, Protocol, overload, type_check_only
66
from typing_extensions import Self, TypeAlias, deprecated
77

88
from psutil._common import (
@@ -234,11 +234,18 @@ class Popen(Process):
234234
def __getattribute__(self, name: str) -> Any: ...
235235
def __dir__(self) -> list[str]: ...
236236

237+
@type_check_only
238+
class _ProcessIterCallable(Protocol):
239+
def __call__(
240+
self, attrs: list[str] | tuple[str, ...] | set[str] | frozenset[str] | None = None, ad_value=None
241+
) -> Iterator[Process]: ...
242+
def cache_clear(self) -> None: ...
243+
237244
def pids() -> list[int]: ...
238245
def pid_exists(pid: int) -> bool: ...
239-
def process_iter(
240-
attrs: list[str] | tuple[str, ...] | set[str] | frozenset[str] | None = None, ad_value=None
241-
) -> Iterator[Process]: ...
246+
247+
process_iter: _ProcessIterCallable
248+
242249
def wait_procs(
243250
procs: Iterable[Process], timeout: float | None = None, callback: Callable[[Process], object] | None = None
244251
) -> tuple[list[Process], list[Process]]: ...

0 commit comments

Comments
 (0)