Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix typehints for psutil.cpu_* functions #12602

Merged
merged 2 commits into from
Aug 31, 2024
Merged

Fix typehints for psutil.cpu_* functions #12602

merged 2 commits into from
Aug 31, 2024

Conversation

alanhdu
Copy link
Contributor

@alanhdu alanhdu commented Aug 28, 2024

The previous typehints ommitted the return type (and used the wrong return type for psutil.cpu_times_percent) and did not capture the overload behavior of percpu=True.

@overload
def cpu_freq(percpu: Literal[True]) -> list[scpufreq]: ...
@overload
def cpu_times(percpu: Literal[False] = ...) -> pcputimes: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

This doesn't seem correct. According to stubs, the returned fields are:

class pcputimes(NamedTuple):
    user: float
    system: float
    children_user: float
    children_system: float

On my linux system, I get no children_user or children_system fields, and the type is scputimes, not pcputimes:

>>> psutil.cpu_times()
scputimes(user=521.89, nice=0.0, system=116.9, idle=5597.83, iowait=209.84, irq=0.0, softirq=11.94, steal=0.0, guest=0.0, guest_nice=0.0)
>>> type(psutil.cpu_times())
<class 'psutil._pslinux.scputimes'>

Currently scputimes is defined in various places, so presumably you need to import it from the right place depending on platform:

akuli@akuli-desktop:~/typeshed/stubs/psutil$ git grep scputimes
psutil/_psaix.pyi:class scputimes(NamedTuple):
psutil/_psbsd.pyi:class scputimes(NamedTuple):
psutil/_pslinux.pyi:def set_scputimes_ntuple(procfs_path) -> None: ...
psutil/_pslinux.pyi:scputimes: Any
psutil/_psosx.pyi:class scputimes(NamedTuple):
psutil/_pssunos.pyi:class scputimes(NamedTuple):
psutil/_pswindows.pyi:class scputimes(NamedTuple):

The scputimes: Any looks wrong. It is generated dynamically at runtime and needs better stubs. (Just include all possible fields and perhaps add a comment.)

Alternatively, if you don't want to bother with scputimes, you could just use Incomplete instead of pcputimes in all these methods for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right -- on my machine it was a psutil._pslinux.scputimes as well. It looks like pcputimes is returned for psutil.Process(...).cpu_times().

This comment has been minimized.

Use an `@overload` to correctly type the `percpu` argument and
use the right return types
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Copy link
Collaborator

@Akuli Akuli left a comment

Choose a reason for hiding this comment

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

Thanks, looks good!

@Akuli Akuli merged commit 23d867e into python:main Aug 31, 2024
48 checks passed
max-muoto pushed a commit to max-muoto/typeshed that referenced this pull request Sep 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants