Skip to content

Commit 0fc655d

Browse files
authoredApr 10, 2023
Runtime-checkable protocol tests: Use @runtime_checkable, not @runtime (#134)
1 parent ad8a08b commit 0fc655d

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed
 

‎src/test_typing_extensions.py

+31-29
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ def __str__(self):
850850
def __add__(self, other):
851851
return 0
852852

853-
@runtime
853+
@runtime_checkable
854854
class HasCallProtocol(Protocol):
855855
__call__: typing.Callable
856856

@@ -1324,7 +1324,7 @@ class Coordinate(Protocol):
13241324
x: int
13251325
y: int
13261326

1327-
@runtime
1327+
@runtime_checkable
13281328
class Point(Coordinate, Protocol):
13291329
label: str
13301330

@@ -1339,11 +1339,11 @@ class XAxis(Protocol):
13391339
class YAxis(Protocol):
13401340
y: int
13411341

1342-
@runtime
1342+
@runtime_checkable
13431343
class Position(XAxis, YAxis, Protocol):
13441344
pass
13451345

1346-
@runtime
1346+
@runtime_checkable
13471347
class Proto(Protocol):
13481348
attr: int
13491349

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

13681368

13691369
class ProtocolTests(BaseTestCase):
1370+
def test_runtime_alias(self):
1371+
self.assertIs(runtime, runtime_checkable)
13701372

13711373
def test_basic_protocol(self):
1372-
@runtime
1374+
@runtime_checkable
13731375
class P(Protocol):
13741376
def meth(self):
13751377
pass
@@ -1387,7 +1389,7 @@ def f():
13871389
self.assertNotIsInstance(f, P)
13881390

13891391
def test_everything_implements_empty_protocol(self):
1390-
@runtime
1392+
@runtime_checkable
13911393
class Empty(Protocol): pass
13921394
class C: pass
13931395
def f():
@@ -1437,7 +1439,7 @@ class CG(PG[T]): pass
14371439
self.assertIsInstance(CG[int](), CG)
14381440

14391441
def test_cannot_instantiate_abstract(self):
1440-
@runtime
1442+
@runtime_checkable
14411443
class P(Protocol):
14421444
@abc.abstractmethod
14431445
def ameth(self) -> int:
@@ -1455,7 +1457,7 @@ def test_subprotocols_extending(self):
14551457
class P1(Protocol):
14561458
def meth1(self):
14571459
pass
1458-
@runtime
1460+
@runtime_checkable
14591461
class P2(P1, Protocol):
14601462
def meth2(self):
14611463
pass
@@ -1484,7 +1486,7 @@ def meth1(self):
14841486
class P2(Protocol):
14851487
def meth2(self):
14861488
pass
1487-
@runtime
1489+
@runtime_checkable
14881490
class P(P1, P2, Protocol):
14891491
pass
14901492
class C:
@@ -1507,10 +1509,10 @@ def meth2(self):
15071509

15081510
def test_protocols_issubclass(self):
15091511
T = TypeVar('T')
1510-
@runtime
1512+
@runtime_checkable
15111513
class P(Protocol):
15121514
def x(self): ...
1513-
@runtime
1515+
@runtime_checkable
15141516
class PG(Protocol[T]):
15151517
def x(self): ...
15161518
class BadP(Protocol):
@@ -1538,7 +1540,7 @@ def x(self): ...
15381540
def test_protocols_issubclass_non_callable(self):
15391541
class C:
15401542
x = 1
1541-
@runtime
1543+
@runtime_checkable
15421544
class PNonCall(Protocol):
15431545
x = 1
15441546
with self.assertRaises(TypeError):
@@ -1560,10 +1562,10 @@ class D(PNonCall): ...
15601562

15611563
def test_protocols_isinstance(self):
15621564
T = TypeVar('T')
1563-
@runtime
1565+
@runtime_checkable
15641566
class P(Protocol):
15651567
def meth(x): ...
1566-
@runtime
1568+
@runtime_checkable
15671569
class PG(Protocol[T]):
15681570
def meth(x): ...
15691571
class BadP(Protocol):
@@ -1616,10 +1618,10 @@ class Bad: pass
16161618

16171619
def test_protocols_isinstance_init(self):
16181620
T = TypeVar('T')
1619-
@runtime
1621+
@runtime_checkable
16201622
class P(Protocol):
16211623
x = 1
1622-
@runtime
1624+
@runtime_checkable
16231625
class PG(Protocol[T]):
16241626
x = 1
16251627
class C:
@@ -1629,7 +1631,7 @@ def __init__(self, x):
16291631
self.assertIsInstance(C(1), PG)
16301632

16311633
def test_protocols_support_register(self):
1632-
@runtime
1634+
@runtime_checkable
16331635
class P(Protocol):
16341636
x = 1
16351637
class PM(Protocol):
@@ -1642,7 +1644,7 @@ class C: pass
16421644
self.assertIsInstance(C(), D)
16431645

16441646
def test_none_on_non_callable_doesnt_block_implementation(self):
1645-
@runtime
1647+
@runtime_checkable
16461648
class P(Protocol):
16471649
x = 1
16481650
class A:
@@ -1656,7 +1658,7 @@ def __init__(self):
16561658
self.assertIsInstance(C(), P)
16571659

16581660
def test_none_on_callable_blocks_implementation(self):
1659-
@runtime
1661+
@runtime_checkable
16601662
class P(Protocol):
16611663
def x(self): ...
16621664
class A:
@@ -1672,7 +1674,7 @@ def __init__(self):
16721674
def test_non_protocol_subclasses(self):
16731675
class P(Protocol):
16741676
x = 1
1675-
@runtime
1677+
@runtime_checkable
16761678
class PR(Protocol):
16771679
def meth(self): pass
16781680
class NonP(P):
@@ -1705,7 +1707,7 @@ def __subclasshook__(cls, other):
17051707
self.assertNotIsSubclass(BadClass, C)
17061708

17071709
def test_issubclass_fails_correctly(self):
1708-
@runtime
1710+
@runtime_checkable
17091711
class P(Protocol):
17101712
x = 1
17111713
class C: pass
@@ -1715,7 +1717,7 @@ class C: pass
17151717
def test_defining_generic_protocols(self):
17161718
T = TypeVar('T')
17171719
S = TypeVar('S')
1718-
@runtime
1720+
@runtime_checkable
17191721
class PR(Protocol[T, S]):
17201722
def meth(self): pass
17211723
class P(PR[int, T], Protocol[T]):
@@ -1739,7 +1741,7 @@ class C(PR[int, T]): pass
17391741
def test_defining_generic_protocols_old_style(self):
17401742
T = TypeVar('T')
17411743
S = TypeVar('S')
1742-
@runtime
1744+
@runtime_checkable
17431745
class PR(Protocol, Generic[T, S]):
17441746
def meth(self): pass
17451747
class P(PR[int, str], Protocol):
@@ -1756,7 +1758,7 @@ class P1(Protocol, Generic[T]):
17561758
def bar(self, x: T) -> str: ...
17571759
class P2(Generic[T], Protocol):
17581760
def bar(self, x: T) -> str: ...
1759-
@runtime
1761+
@runtime_checkable
17601762
class PSub(P1[str], Protocol):
17611763
x = 1
17621764
class Test:
@@ -1813,7 +1815,7 @@ class P(Protocol[T]): pass
18131815
self.assertIs(P[int].__origin__, P)
18141816

18151817
def test_generic_protocols_special_from_protocol(self):
1816-
@runtime
1818+
@runtime_checkable
18171819
class PR(Protocol):
18181820
x = 1
18191821
class P(Protocol):
@@ -1841,17 +1843,17 @@ def meth(self):
18411843

18421844
def test_no_runtime_deco_on_nominal(self):
18431845
with self.assertRaises(TypeError):
1844-
@runtime
1846+
@runtime_checkable
18451847
class C: pass
18461848
class Proto(Protocol):
18471849
x = 1
18481850
with self.assertRaises(TypeError):
1849-
@runtime
1851+
@runtime_checkable
18501852
class Concrete(Proto):
18511853
pass
18521854

18531855
def test_none_treated_correctly(self):
1854-
@runtime
1856+
@runtime_checkable
18551857
class P(Protocol):
18561858
x: int = None
18571859
class B(object): pass
@@ -1882,7 +1884,7 @@ def test_protocols_pickleable(self):
18821884
global P, CP # pickle wants to reference the class by name
18831885
T = TypeVar('T')
18841886

1885-
@runtime
1887+
@runtime_checkable
18861888
class P(Protocol[T]):
18871889
x = 1
18881890
class CP(P[int]):

0 commit comments

Comments
 (0)
Please sign in to comment.