Skip to content

Conversation

@Gobot1234
Copy link
Contributor

No description provided.

@Gobot1234 Gobot1234 changed the title Add better types for predicate parameter Add better types for predicate parameter to inspect.getx functions Mar 16, 2023
@github-actions
Copy link
Contributor

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

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

I checked out this PR branch locally, and tried adding this test case to a new test_cases/stdlib/check_inspect.py file:

from __future__ import annotations

import inspect
from typing import Any, List, Tuple
from typing_extensions import TypeGuard, assert_type

def foo(obj: Any) -> TypeGuard[list[str]]:
    return isinstance(obj, list) and all(isinstance(item, str) for item in obj)

class Bar: ...
x = Bar()

assert_type(inspect.getmembers(x), List[Tuple[str, Any]])
assert_type(inspect.getmembers(x, None), List[Tuple[str, Any]])
assert_type(inspect.getmembers(x, foo), List[Tuple[str, List[str]]])

It seems to pass pyright, but the last assertion fails on mypy (mypy infers list[tuple[str, Any]] for all three). I guess this is just because pyright has better handling of generic TypeGuards in this situation?

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

I verified that this is a clear improvement for pyright users, and it doesn't lead to any regressions for mypy users (mypy users just won't get the same benefits right now). So I'm happy for this to go in.

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