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

Set Process.info to None by default #2260

Closed
wants to merge 2 commits into from

Conversation

sobolevn
Copy link

Refs python/typeshed#10195

Summary

  • OS: any
  • Bug fix: yes
  • Type: core
  • Fixes: None

Description

Right now .info attribute is only set during process_iter, which is not quite correct.
This results in AttributeError when using regular Process:

>>> import psutil
>>> psutil.Process(1)
psutil.Process(pid=1, name='launchd', status='running', started='2023-05-27 09:31:50')
>>> psutil.Process(1).info
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Process' object has no attribute 'info'

I propose using None default for cases when it is not created using process_iter.

This way it will simplify typing of Process for us.

@giampaolo
Copy link
Owner

giampaolo commented May 29, 2023

Hmm... debatable.
I think I'm more keen on agreeing with you: I suppose it's less surprising for the user if info attribute is always set. I wonder if it's more correct for it to be an empty dict {} rather than None though. Uhmm...

@sobolevn
Copy link
Author

Oh, whatever you like, I am not a user of this feature, so I cannot really decide.

@emcek
Copy link

emcek commented May 29, 2023

Hmm... debatable. I think I'm more keen on agreeing with you: I suppose it's less surprising for the user if info attribute is always set. I wonder if it's more correct for it to be an empty dict {} rather than None though. Uhmm...

Yes, I think empty dict is fine!

@ringohoffman
Copy link

ringohoffman commented Jun 6, 2023

I came here to raise a similar issue. This example from the docs raises type errors since info is not defined in the __init__:

https://psutil.readthedocs.io/en/latest/#psutil.process_iter:

import psutil
for proc in psutil.process_iter(['pid', 'name', 'username']):
    print(proc.info)  # Cannot access member "info" for type "Process"; Member "info" is unknown

We would need to update the type stubs accordingly: https://github.com/python/typeshed/blob/e347af1d59ed46478a788ed60fd02096fce26edd/stubs/psutil/psutil/__init__.pyi#L158

@sobolevn
Copy link
Author

sobolevn commented Jun 7, 2023

Oh, closing in favor of #2267
Sorry, I haven't seen your PR! 👍

@sobolevn sobolevn closed this Jun 7, 2023
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.

None yet

4 participants