Skip to content

psutil: Type ad_value as object #14106

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

psutil: Type ad_value as object #14106

wants to merge 1 commit into from

Conversation

vnmabus
Copy link

@vnmabus vnmabus commented May 20, 2025

After #14063, calls to psutil.process_iter and psutil.Process.as_dict are being reported by Pyright, as the ad_value parameter is untyped.

However, given that (from the documentation) this is just a placeholder value to use when an error is raised while fetching some attribute, I think that typing it as object is appropriate.

After python#14063, calls to `psutil.process_iter` and `psutil.Process.as_dict` are being reported by Pyright, as the `ad_value` parameter is untyped.

However, given that (from the documentation) this is just a placeholder value to use when an error is raised while fetching some attribute, I think that typing it as `object` is appropriate.
Copy link
Contributor

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

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

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

Considering that the supplied type influenced the result type (but we can't express that relation properly), Any is a bit more appropriate. Also, we need to document uses of Any, see suggestions below.

@@ -175,7 +175,7 @@ class Process:
info: dict[str, Any]
def oneshot(self) -> AbstractContextManager[None]: ...
def as_dict(
self, attrs: list[str] | tuple[str, ...] | set[str] | frozenset[str] | None = None, ad_value=None
self, attrs: list[str] | tuple[str, ...] | set[str] | frozenset[str] | None = None, ad_value: object = None
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
self, attrs: list[str] | tuple[str, ...] | set[str] | frozenset[str] | None = None, ad_value: object = None
self,
attrs: list[str] | tuple[str, ...] | set[str] | frozenset[str] | None = None,
ad_value: Any = None, # arbitrary value used as result in case of error

@@ -236,7 +236,7 @@ class Popen(Process):
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
attrs: list[str] | tuple[str, ...] | set[str] | frozenset[str] | None = None, ad_value: object = None
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
attrs: list[str] | tuple[str, ...] | set[str] | frozenset[str] | None = None, ad_value: object = None
attrs: list[str] | tuple[str, ...] | set[str] | frozenset[str] | None = None,
ad_value: Any = None, # arbitrary value used as result in case of error

@vnmabus
Copy link
Author

vnmabus commented May 20, 2025

Considering that the supplied type influenced the result type (but we can't express that relation properly)

Can't we? Wouldn't be possible to create a (fake) Process subclass that include the info field as a generic TypedDict and use overload to see if we have to return that subclass or the normal class?

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