diff --git a/stubs/psutil/@tests/stubtest_allowlist.txt b/stubs/psutil/@tests/stubtest_allowlist.txt index ec9f30c85992..de51b72de935 100644 --- a/stubs/psutil/@tests/stubtest_allowlist.txt +++ b/stubs/psutil/@tests/stubtest_allowlist.txt @@ -8,3 +8,6 @@ psutil._pssunos # Test utilities psutil.tests.* + +# process_iter is enhanced with cache_clear method that's not detected by stubtest +psutil.process_iter diff --git a/stubs/psutil/@tests/test_cases/check_process_iter.py b/stubs/psutil/@tests/test_cases/check_process_iter.py new file mode 100644 index 000000000000..b31fe9d7eefe --- /dev/null +++ b/stubs/psutil/@tests/test_cases/check_process_iter.py @@ -0,0 +1,16 @@ +"""Test cases for psutil.process_iter and its cache_clear method.""" + +from __future__ import annotations + +import psutil + +# Test that process_iter can be called as a function +for proc in psutil.process_iter(): + break + +# Test that process_iter has cache_clear method +psutil.process_iter.cache_clear() + +# Test that cache_clear is callable +clear_method = psutil.process_iter.cache_clear +clear_method() diff --git a/stubs/psutil/psutil/__init__.pyi b/stubs/psutil/psutil/__init__.pyi index 5c6778fa3fba..817a93127d29 100644 --- a/stubs/psutil/psutil/__init__.pyi +++ b/stubs/psutil/psutil/__init__.pyi @@ -2,7 +2,7 @@ import sys from _typeshed import Incomplete from collections.abc import Callable, Iterable, Iterator from contextlib import AbstractContextManager -from typing import Any, Literal, overload +from typing import Any, Literal, Protocol, overload, type_check_only from typing_extensions import Self, TypeAlias, deprecated from psutil._common import ( @@ -234,11 +234,18 @@ class Popen(Process): def __getattribute__(self, name: str) -> Any: ... def __dir__(self) -> list[str]: ... +@type_check_only +class _ProcessIterCallable(Protocol): + def __call__( + self, attrs: list[str] | tuple[str, ...] | set[str] | frozenset[str] | None = None, ad_value=None + ) -> Iterator[Process]: ... + def cache_clear(self) -> None: ... + def pids() -> list[int]: ... def pid_exists(pid: int) -> bool: ... -def process_iter( - attrs: list[str] | tuple[str, ...] | set[str] | frozenset[str] | None = None, ad_value=None -) -> Iterator[Process]: ... + +process_iter: _ProcessIterCallable + def wait_procs( procs: Iterable[Process], timeout: float | None = None, callback: Callable[[Process], object] | None = None ) -> tuple[list[Process], list[Process]]: ...