Skip to content

Commit 61dd90e

Browse files
[psutil] Add cache_clear to psutil.process_iter typing
Signed-off-by: Emmanuel Ferdman <[email protected]>
1 parent c7d0fd9 commit 61dd90e

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 (original behavior)
8+
for proc in psutil.process_iter():
9+
break
10+
11+
for proc in psutil.process_iter(attrs=["pid", "name"]):
12+
break
13+
14+
for proc in psutil.process_iter(attrs=["pid"], ad_value="N/A"):
15+
break
16+
17+
# Test that process_iter has cache_clear method (new behavior)
18+
psutil.process_iter.cache_clear()
19+
20+
# Test that cache_clear is callable
21+
clear_method = psutil.process_iter.cache_clear
22+
clear_method()
23+
24+
# Test type annotations work correctly
25+
from collections.abc import Iterator
26+
from typing import Any
27+
28+
processes: Iterator[psutil.Process] = psutil.process_iter()
29+
processes_with_attrs: Iterator[psutil.Process] = psutil.process_iter(attrs=["pid"])
30+
31+
# Test that cache_clear returns None
32+
psutil.process_iter.cache_clear() # Returns None, don't assign

stubs/psutil/psutil/__init__.pyi

Lines changed: 10 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
66
from typing_extensions import Self, TypeAlias, deprecated
77

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

237+
class _ProcessIterCallable(Protocol):
238+
def __call__(
239+
self, attrs: list[str] | tuple[str, ...] | set[str] | frozenset[str] | None = None, ad_value=None
240+
) -> Iterator[Process]: ...
241+
def cache_clear(self) -> None: ...
242+
237243
def pids() -> list[int]: ...
238244
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]: ...
245+
246+
process_iter: _ProcessIterCallable
247+
242248
def wait_procs(
243249
procs: Iterable[Process], timeout: float | None = None, callback: Callable[[Process], object] | None = None
244250
) -> tuple[list[Process], list[Process]]: ...

0 commit comments

Comments
 (0)