-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
Open
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
Basically, the isinstance-check is ignoring that I manually specified that the class must have __class_getitem__.
from types import GenericAlias
from typing import runtime_checkable, Protocol, Iterator, TypeVar
T_co = TypeVar("T_co", covariant=True)
@runtime_checkable
class GenericIterable(Protocol[T_co]):
def __class_getitem__(cls, item: type) -> GenericAlias: ...
def __iter__(self) -> Iterator[T_co]: ...
x = ["a", "b", "c"]
assert hasattr(x, "__class_getitem__") # ✅
assert isinstance(x, GenericIterable) # ✅
y = "abc"
assert not hasattr(y, "__class_getitem__") # ✅
assert not isinstance(y, GenericIterable) # ❌I was considering this Protocol as a partial workaround for the Iterable[str] vs str issue python/typing#256.
CPython versions tested on:
3.12
Operating systems tested on:
Linux
Linked PRs
Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error