Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime-checkable protocol tests: Use @runtime_checkable, not @runtime #134

Merged
Merged
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
60 changes: 31 additions & 29 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ def __str__(self):
def __add__(self, other):
return 0

@runtime
@runtime_checkable
class HasCallProtocol(Protocol):
__call__: typing.Callable

Expand Down Expand Up @@ -1324,7 +1324,7 @@ class Coordinate(Protocol):
x: int
y: int

@runtime
@runtime_checkable
class Point(Coordinate, Protocol):
label: str

Expand All @@ -1339,11 +1339,11 @@ class XAxis(Protocol):
class YAxis(Protocol):
y: int

@runtime
@runtime_checkable
class Position(XAxis, YAxis, Protocol):
pass

@runtime
@runtime_checkable
class Proto(Protocol):
attr: int

Expand All @@ -1367,9 +1367,11 @@ class NT(NamedTuple):


class ProtocolTests(BaseTestCase):
def test_runtime_alias(self):
self.assertIs(runtime, runtime_checkable)

def test_basic_protocol(self):
@runtime
@runtime_checkable
class P(Protocol):
def meth(self):
pass
Expand All @@ -1387,7 +1389,7 @@ def f():
self.assertNotIsInstance(f, P)

def test_everything_implements_empty_protocol(self):
@runtime
@runtime_checkable
class Empty(Protocol): pass
class C: pass
def f():
Expand Down Expand Up @@ -1437,7 +1439,7 @@ class CG(PG[T]): pass
self.assertIsInstance(CG[int](), CG)

def test_cannot_instantiate_abstract(self):
@runtime
@runtime_checkable
class P(Protocol):
@abc.abstractmethod
def ameth(self) -> int:
Expand All @@ -1455,7 +1457,7 @@ def test_subprotocols_extending(self):
class P1(Protocol):
def meth1(self):
pass
@runtime
@runtime_checkable
class P2(P1, Protocol):
def meth2(self):
pass
Expand Down Expand Up @@ -1484,7 +1486,7 @@ def meth1(self):
class P2(Protocol):
def meth2(self):
pass
@runtime
@runtime_checkable
class P(P1, P2, Protocol):
pass
class C:
Expand All @@ -1507,10 +1509,10 @@ def meth2(self):

def test_protocols_issubclass(self):
T = TypeVar('T')
@runtime
@runtime_checkable
class P(Protocol):
def x(self): ...
@runtime
@runtime_checkable
class PG(Protocol[T]):
def x(self): ...
class BadP(Protocol):
Expand Down Expand Up @@ -1538,7 +1540,7 @@ def x(self): ...
def test_protocols_issubclass_non_callable(self):
class C:
x = 1
@runtime
@runtime_checkable
class PNonCall(Protocol):
x = 1
with self.assertRaises(TypeError):
Expand All @@ -1560,10 +1562,10 @@ class D(PNonCall): ...

def test_protocols_isinstance(self):
T = TypeVar('T')
@runtime
@runtime_checkable
class P(Protocol):
def meth(x): ...
@runtime
@runtime_checkable
class PG(Protocol[T]):
def meth(x): ...
class BadP(Protocol):
Expand Down Expand Up @@ -1616,10 +1618,10 @@ class Bad: pass

def test_protocols_isinstance_init(self):
T = TypeVar('T')
@runtime
@runtime_checkable
class P(Protocol):
x = 1
@runtime
@runtime_checkable
class PG(Protocol[T]):
x = 1
class C:
Expand All @@ -1629,7 +1631,7 @@ def __init__(self, x):
self.assertIsInstance(C(1), PG)

def test_protocols_support_register(self):
@runtime
@runtime_checkable
class P(Protocol):
x = 1
class PM(Protocol):
Expand All @@ -1642,7 +1644,7 @@ class C: pass
self.assertIsInstance(C(), D)

def test_none_on_non_callable_doesnt_block_implementation(self):
@runtime
@runtime_checkable
class P(Protocol):
x = 1
class A:
Expand All @@ -1656,7 +1658,7 @@ def __init__(self):
self.assertIsInstance(C(), P)

def test_none_on_callable_blocks_implementation(self):
@runtime
@runtime_checkable
class P(Protocol):
def x(self): ...
class A:
Expand All @@ -1672,7 +1674,7 @@ def __init__(self):
def test_non_protocol_subclasses(self):
class P(Protocol):
x = 1
@runtime
@runtime_checkable
class PR(Protocol):
def meth(self): pass
class NonP(P):
Expand Down Expand Up @@ -1705,7 +1707,7 @@ def __subclasshook__(cls, other):
self.assertNotIsSubclass(BadClass, C)

def test_issubclass_fails_correctly(self):
@runtime
@runtime_checkable
class P(Protocol):
x = 1
class C: pass
Expand All @@ -1715,7 +1717,7 @@ class C: pass
def test_defining_generic_protocols(self):
T = TypeVar('T')
S = TypeVar('S')
@runtime
@runtime_checkable
class PR(Protocol[T, S]):
def meth(self): pass
class P(PR[int, T], Protocol[T]):
Expand All @@ -1739,7 +1741,7 @@ class C(PR[int, T]): pass
def test_defining_generic_protocols_old_style(self):
T = TypeVar('T')
S = TypeVar('S')
@runtime
@runtime_checkable
class PR(Protocol, Generic[T, S]):
def meth(self): pass
class P(PR[int, str], Protocol):
Expand All @@ -1756,7 +1758,7 @@ class P1(Protocol, Generic[T]):
def bar(self, x: T) -> str: ...
class P2(Generic[T], Protocol):
def bar(self, x: T) -> str: ...
@runtime
@runtime_checkable
class PSub(P1[str], Protocol):
x = 1
class Test:
Expand Down Expand Up @@ -1813,7 +1815,7 @@ class P(Protocol[T]): pass
self.assertIs(P[int].__origin__, P)

def test_generic_protocols_special_from_protocol(self):
@runtime
@runtime_checkable
class PR(Protocol):
x = 1
class P(Protocol):
Expand Down Expand Up @@ -1841,17 +1843,17 @@ def meth(self):

def test_no_runtime_deco_on_nominal(self):
with self.assertRaises(TypeError):
@runtime
@runtime_checkable
class C: pass
class Proto(Protocol):
x = 1
with self.assertRaises(TypeError):
@runtime
@runtime_checkable
class Concrete(Proto):
pass

def test_none_treated_correctly(self):
@runtime
@runtime_checkable
class P(Protocol):
x: int = None
class B(object): pass
Expand Down Expand Up @@ -1882,7 +1884,7 @@ def test_protocols_pickleable(self):
global P, CP # pickle wants to reference the class by name
T = TypeVar('T')

@runtime
@runtime_checkable
class P(Protocol[T]):
x = 1
class CP(P[int]):
Expand Down