FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-103171: Forward-port new tests for runtime-checkable protocols decorated with `@final` by AlexWaygood · Pull Request #105473 · python/cpython · GitHub

/ cpython Public
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
65 changes: 65 additions & 0 deletions Lib/test/test_typing.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -3800,6 +3800,71 @@ class Foo(typing.Sized, Protocol): pass
# before any isinstance() checks against Sized
self.assertNotIsInstance(1, typing.Sized)

def test_empty_protocol_decorated_with_final(self):
@final
@runtime_checkable
class EmptyProtocol(Protocol): ...

self.assertIsSubclass(object, EmptyProtocol)
self.assertIsInstance(object(), EmptyProtocol)

def test_protocol_decorated_with_final_callable_members(self):
@final
@runtime_checkable
class ProtocolWithMethod(Protocol):
def startswith(self, string: str) -> bool: ...

self.assertIsSubclass(str, ProtocolWithMethod)
self.assertNotIsSubclass(int, ProtocolWithMethod)
self.assertIsInstance('foo', ProtocolWithMethod)
self.assertNotIsInstance(42, ProtocolWithMethod)

def test_protocol_decorated_with_final_noncallable_members(self):
@final
@runtime_checkable
class ProtocolWithNonCallableMember(Protocol):
x: int

class Foo:
x = 42

only_callable_members_please = (
r"Protocols with non-method members don't support issubclass()"
)

with self.assertRaisesRegex(TypeError, only_callable_members_please):
issubclass(Foo, ProtocolWithNonCallableMember)

with self.assertRaisesRegex(TypeError, only_callable_members_please):
issubclass(int, ProtocolWithNonCallableMember)

self.assertIsInstance(Foo(), ProtocolWithNonCallableMember)
self.assertNotIsInstance(42, ProtocolWithNonCallableMember)

def test_protocol_decorated_with_final_mixed_members(self):
@final
@runtime_checkable
class ProtocolWithMixedMembers(Protocol):
x: int
def method(self) -> None: ...

class Foo:
x = 42
def method(self) -> None: ...

only_callable_members_please = (
r"Protocols with non-method members don't support issubclass()"
)

with self.assertRaisesRegex(TypeError, only_callable_members_please):
issubclass(Foo, ProtocolWithMixedMembers)

with self.assertRaisesRegex(TypeError, only_callable_members_please):
issubclass(int, ProtocolWithMixedMembers)

self.assertIsInstance(Foo(), ProtocolWithMixedMembers)
self.assertNotIsInstance(42, ProtocolWithMixedMembers)


class GenericTests(BaseTestCase):

Expand Down

Back | FazBrowse Home | New Git URL