Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions stdlib/@tests/test_cases/builtins/check_isinstance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from typing import Any, cast
from typing_extensions import TypeIs, assert_type

o = cast(Any, "")
assert_type(isinstance(o, int), TypeIs[int])

Check failure on line 5 in stdlib/@tests/test_cases/builtins/check_isinstance.py

View workflow job for this annotation

GitHub Actions / pyright: regression tests (Linux, 3.13)

"assert_type" mismatch: expected "TypeIs[int]" but received "TypeIs[int]" (reportAssertTypeFailure)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

These tests are failing and they do not prove that the revised stubs help with reasonable usage patterns. The issue talks about patterns like .filter(lambda x: isinstance(x, int) where presumably there's some TypeIs on the .filter(), that's what we need to prove is actually improved by this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Understood. I will continue to work on the tests later.

assert_type(isinstance(o, object), TypeIs[object])

Check failure on line 6 in stdlib/@tests/test_cases/builtins/check_isinstance.py

View workflow job for this annotation

GitHub Actions / pyright: regression tests (Linux, 3.13)

"assert_type" mismatch: expected "TypeIs[object]" but received "TypeIs[object]" (reportAssertTypeFailure)
assert_type(isinstance(o, str), TypeIs[str])

Check failure on line 7 in stdlib/@tests/test_cases/builtins/check_isinstance.py

View workflow job for this annotation

GitHub Actions / pyright: regression tests (Linux, 3.13)

"assert_type" mismatch: expected "TypeIs[str]" but received "TypeIs[str]" (reportAssertTypeFailure)
assert_type(isinstance(o, (bytes, memoryview)), TypeIs[bytes | memoryview[int]])

Check failure on line 8 in stdlib/@tests/test_cases/builtins/check_isinstance.py

View workflow job for this annotation

GitHub Actions / pyright: regression tests (Linux, 3.13)

"assert_type" mismatch: expected "TypeIs[bytes | memoryview[int]]" but received "TypeIs[bytes | memoryview[int]]" (reportAssertTypeFailure)
assert_type(isinstance(o, (set, type[dict])), TypeIs[set[Any] | type[dict[Any, Any]]])

Check failure on line 9 in stdlib/@tests/test_cases/builtins/check_isinstance.py

View workflow job for this annotation

GitHub Actions / pyright: regression tests (Linux, 3.13)

Second argument to "isinstance" must be a class or tuple of classes   Generic type with type arguments not allowed for instance or class checks (reportArgumentType)

Check failure on line 9 in stdlib/@tests/test_cases/builtins/check_isinstance.py

View workflow job for this annotation

GitHub Actions / pyright: regression tests (Linux, 3.13)

"assert_type" mismatch: expected "TypeIs[set[Any] | type[dict[Any, Any]]]" but received "TypeIs[set[Unknown] | type[dict[Unknown, Unknown]]]" (reportAssertTypeFailure)
20 changes: 20 additions & 0 deletions stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1882,7 +1882,27 @@ def iter(object: Callable[[], _T], sentinel: object, /) -> Iterator[_T]: ...

_ClassInfo: TypeAlias = type | types.UnionType | tuple[_ClassInfo, ...]

@overload
def isinstance(obj: object, class_or_tuple: type[_T] | tuple[type[_T]], /) -> TypeIs[_T]: ...
@overload
def isinstance(obj: object, class_or_tuple: tuple[type[_T], type[_T1]], /) -> TypeIs[_T | _T1]: ...
@overload
def isinstance(obj: object, class_or_tuple: tuple[type[_T], type[_T1], type[_T2]], /) -> TypeIs[_T | _T1 | _T2]: ...
@overload
def isinstance(
obj: object, class_or_tuple: tuple[type[_T], type[_T1], type[_T2], type[_T3]], /
) -> TypeIs[_T | _T1 | _T2 | _T3]: ...
@overload
def isinstance(
obj: object, class_or_tuple: tuple[type[_T], type[_T1], type[_T2], type[_T3], type[_T4]], /
) -> TypeIs[_T | _T1 | _T2 | _T3 | _T4]: ...
@overload
def isinstance(
obj: object, class_or_tuple: tuple[type[_T], type[_T1], type[_T2], type[_T3], type[_T4], type[_T5]], /
) -> TypeIs[_T | _T1 | _T2 | _T3 | _T4 | _T5]: ...
@overload
def isinstance(obj: object, class_or_tuple: _ClassInfo, /) -> bool: ...

def issubclass(cls: type, class_or_tuple: _ClassInfo, /) -> bool: ...
def len(obj: Sized, /) -> int: ...

Expand Down
Loading