◐ Shell
clean mode source ↗

Backport some recent `Protocol` fixes from 3.12 by AlexWaygood · Pull Request #161 · python/typing_extensions

exec(textwrap.dedent( """ def test_pep695_generic_protocol_callable_members(self): @runtime_checkable class Foo[T](Protocol): def meth(self, x: T) -> None: ...
class Bar[T]: def meth(self, x: T) -> None: ...
self.assertIsInstance(Bar(), Foo) self.assertIsSubclass(Bar, Foo)
@runtime_checkable class SupportsTrunc[T](Protocol): def __trunc__(self) -> T: ...
self.assertIsInstance(0.0, SupportsTrunc) self.assertIsSubclass(float, SupportsTrunc)
def test_no_weird_caching_with_issubclass_after_isinstance_pep695(self): @runtime_checkable class Spam[T](Protocol): x: T
class Eggs[T]: def __init__(self, x: T) -> None: self.x = x
self.assertIsInstance(Eggs(42), Spam)
# gh-104555: If we didn't override ABCMeta.__subclasscheck__ in _ProtocolMeta, # TypeError wouldn't be raised here, # as the cached result of the isinstance() check immediately above # would mean the issubclass() call would short-circuit # before we got to the "raise TypeError" line with self.assertRaises(TypeError): issubclass(Eggs, Spam)