Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion stubs/psutil/psutil/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,13 @@ class Process:
@overload
def cpu_affinity(self, cpus: list[int]) -> None: ...

def memory_maps(self, grouped: bool = True) -> list[Incomplete]: ...
@overload
def memory_maps(self, grouped: Literal[True] = True) -> list[_ntp.pmmap_grouped]: ...
@overload
def memory_maps(self, grouped: Literal[False]) -> list[_ntp.pmmap_ext]: ...
@overload
def memory_maps(self, grouped: bool) -> list[_ntp.pmmap_grouped] | list[_ntp.pmmap_ext]: ...
Comment on lines +184 to +185

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need the third "fallback" overload, as the other overloads already cover all possible cases.

Suggested change
@overload
def memory_maps(self, grouped: bool) -> list[_ntp.pmmap_grouped] | list[_ntp.pmmap_ext]: ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that, and mypy needs the third overload. With only the two Literal overloads, passing a plain bool variable fails:

p = psutil.Process()
flag: bool = True
p.memory_maps(flag)
error: No overload variant of "memory_maps" of "Process" matches argument type "bool"  [call-overload]
note: Possible overload variants:
note:     def memory_maps(self, grouped: Literal[True] = ...) -> list[pmmap_grouped]
note:     def memory_maps(self, grouped: Literal[False]) -> list[pmmap_ext]

Pyright expands bool into Literal[True] | Literal[False] during overload resolution, but mypy does not, so the literal overloads alone do not cover every call. With the fallback restored, the same call resolves to the union and all four call shapes check out.

This matches what typeshed already does elsewhere for the same situation, for example ssl.SSLObject.getpeercert and _winapi.ConnectNamedPipe, which both keep a trailing bool overload after the two literal ones.

Happy to drop it if you would rather have the stricter signature, but that would be a small regression for callers who pass a computed flag.


if sys.platform == "linux":
def rlimit(self, resource: int, limits: tuple[int, int] | None = None) -> tuple[int, int]: ...
def cpu_num(self) -> int: ...
Expand Down
Loading