Skip to content

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.
@github-actions
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.

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

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