@@ -850,7 +850,7 @@ def __str__(self):
850
850
def __add__ (self , other ):
851
851
return 0
852
852
853
- @runtime
853
+ @runtime_checkable
854
854
class HasCallProtocol (Protocol ):
855
855
__call__ : typing .Callable
856
856
@@ -1324,7 +1324,7 @@ class Coordinate(Protocol):
1324
1324
x : int
1325
1325
y : int
1326
1326
1327
- @runtime
1327
+ @runtime_checkable
1328
1328
class Point (Coordinate , Protocol ):
1329
1329
label : str
1330
1330
@@ -1339,11 +1339,11 @@ class XAxis(Protocol):
1339
1339
class YAxis (Protocol ):
1340
1340
y : int
1341
1341
1342
- @runtime
1342
+ @runtime_checkable
1343
1343
class Position (XAxis , YAxis , Protocol ):
1344
1344
pass
1345
1345
1346
- @runtime
1346
+ @runtime_checkable
1347
1347
class Proto (Protocol ):
1348
1348
attr : int
1349
1349
@@ -1367,9 +1367,11 @@ class NT(NamedTuple):
1367
1367
1368
1368
1369
1369
class ProtocolTests (BaseTestCase ):
1370
+ def test_runtime_alias (self ):
1371
+ self .assertIs (runtime , runtime_checkable )
1370
1372
1371
1373
def test_basic_protocol (self ):
1372
- @runtime
1374
+ @runtime_checkable
1373
1375
class P (Protocol ):
1374
1376
def meth (self ):
1375
1377
pass
@@ -1387,7 +1389,7 @@ def f():
1387
1389
self .assertNotIsInstance (f , P )
1388
1390
1389
1391
def test_everything_implements_empty_protocol (self ):
1390
- @runtime
1392
+ @runtime_checkable
1391
1393
class Empty (Protocol ): pass
1392
1394
class C : pass
1393
1395
def f ():
@@ -1437,7 +1439,7 @@ class CG(PG[T]): pass
1437
1439
self .assertIsInstance (CG [int ](), CG )
1438
1440
1439
1441
def test_cannot_instantiate_abstract (self ):
1440
- @runtime
1442
+ @runtime_checkable
1441
1443
class P (Protocol ):
1442
1444
@abc .abstractmethod
1443
1445
def ameth (self ) -> int :
@@ -1455,7 +1457,7 @@ def test_subprotocols_extending(self):
1455
1457
class P1 (Protocol ):
1456
1458
def meth1 (self ):
1457
1459
pass
1458
- @runtime
1460
+ @runtime_checkable
1459
1461
class P2 (P1 , Protocol ):
1460
1462
def meth2 (self ):
1461
1463
pass
@@ -1484,7 +1486,7 @@ def meth1(self):
1484
1486
class P2 (Protocol ):
1485
1487
def meth2 (self ):
1486
1488
pass
1487
- @runtime
1489
+ @runtime_checkable
1488
1490
class P (P1 , P2 , Protocol ):
1489
1491
pass
1490
1492
class C :
@@ -1507,10 +1509,10 @@ def meth2(self):
1507
1509
1508
1510
def test_protocols_issubclass (self ):
1509
1511
T = TypeVar ('T' )
1510
- @runtime
1512
+ @runtime_checkable
1511
1513
class P (Protocol ):
1512
1514
def x (self ): ...
1513
- @runtime
1515
+ @runtime_checkable
1514
1516
class PG (Protocol [T ]):
1515
1517
def x (self ): ...
1516
1518
class BadP (Protocol ):
@@ -1538,7 +1540,7 @@ def x(self): ...
1538
1540
def test_protocols_issubclass_non_callable (self ):
1539
1541
class C :
1540
1542
x = 1
1541
- @runtime
1543
+ @runtime_checkable
1542
1544
class PNonCall (Protocol ):
1543
1545
x = 1
1544
1546
with self .assertRaises (TypeError ):
@@ -1560,10 +1562,10 @@ class D(PNonCall): ...
1560
1562
1561
1563
def test_protocols_isinstance (self ):
1562
1564
T = TypeVar ('T' )
1563
- @runtime
1565
+ @runtime_checkable
1564
1566
class P (Protocol ):
1565
1567
def meth (x ): ...
1566
- @runtime
1568
+ @runtime_checkable
1567
1569
class PG (Protocol [T ]):
1568
1570
def meth (x ): ...
1569
1571
class BadP (Protocol ):
@@ -1616,10 +1618,10 @@ class Bad: pass
1616
1618
1617
1619
def test_protocols_isinstance_init (self ):
1618
1620
T = TypeVar ('T' )
1619
- @runtime
1621
+ @runtime_checkable
1620
1622
class P (Protocol ):
1621
1623
x = 1
1622
- @runtime
1624
+ @runtime_checkable
1623
1625
class PG (Protocol [T ]):
1624
1626
x = 1
1625
1627
class C :
@@ -1629,7 +1631,7 @@ def __init__(self, x):
1629
1631
self .assertIsInstance (C (1 ), PG )
1630
1632
1631
1633
def test_protocols_support_register (self ):
1632
- @runtime
1634
+ @runtime_checkable
1633
1635
class P (Protocol ):
1634
1636
x = 1
1635
1637
class PM (Protocol ):
@@ -1642,7 +1644,7 @@ class C: pass
1642
1644
self .assertIsInstance (C (), D )
1643
1645
1644
1646
def test_none_on_non_callable_doesnt_block_implementation (self ):
1645
- @runtime
1647
+ @runtime_checkable
1646
1648
class P (Protocol ):
1647
1649
x = 1
1648
1650
class A :
@@ -1656,7 +1658,7 @@ def __init__(self):
1656
1658
self .assertIsInstance (C (), P )
1657
1659
1658
1660
def test_none_on_callable_blocks_implementation (self ):
1659
- @runtime
1661
+ @runtime_checkable
1660
1662
class P (Protocol ):
1661
1663
def x (self ): ...
1662
1664
class A :
@@ -1672,7 +1674,7 @@ def __init__(self):
1672
1674
def test_non_protocol_subclasses (self ):
1673
1675
class P (Protocol ):
1674
1676
x = 1
1675
- @runtime
1677
+ @runtime_checkable
1676
1678
class PR (Protocol ):
1677
1679
def meth (self ): pass
1678
1680
class NonP (P ):
@@ -1705,7 +1707,7 @@ def __subclasshook__(cls, other):
1705
1707
self .assertNotIsSubclass (BadClass , C )
1706
1708
1707
1709
def test_issubclass_fails_correctly (self ):
1708
- @runtime
1710
+ @runtime_checkable
1709
1711
class P (Protocol ):
1710
1712
x = 1
1711
1713
class C : pass
@@ -1715,7 +1717,7 @@ class C: pass
1715
1717
def test_defining_generic_protocols (self ):
1716
1718
T = TypeVar ('T' )
1717
1719
S = TypeVar ('S' )
1718
- @runtime
1720
+ @runtime_checkable
1719
1721
class PR (Protocol [T , S ]):
1720
1722
def meth (self ): pass
1721
1723
class P (PR [int , T ], Protocol [T ]):
@@ -1739,7 +1741,7 @@ class C(PR[int, T]): pass
1739
1741
def test_defining_generic_protocols_old_style (self ):
1740
1742
T = TypeVar ('T' )
1741
1743
S = TypeVar ('S' )
1742
- @runtime
1744
+ @runtime_checkable
1743
1745
class PR (Protocol , Generic [T , S ]):
1744
1746
def meth (self ): pass
1745
1747
class P (PR [int , str ], Protocol ):
@@ -1756,7 +1758,7 @@ class P1(Protocol, Generic[T]):
1756
1758
def bar (self , x : T ) -> str : ...
1757
1759
class P2 (Generic [T ], Protocol ):
1758
1760
def bar (self , x : T ) -> str : ...
1759
- @runtime
1761
+ @runtime_checkable
1760
1762
class PSub (P1 [str ], Protocol ):
1761
1763
x = 1
1762
1764
class Test :
@@ -1813,7 +1815,7 @@ class P(Protocol[T]): pass
1813
1815
self .assertIs (P [int ].__origin__ , P )
1814
1816
1815
1817
def test_generic_protocols_special_from_protocol (self ):
1816
- @runtime
1818
+ @runtime_checkable
1817
1819
class PR (Protocol ):
1818
1820
x = 1
1819
1821
class P (Protocol ):
@@ -1841,17 +1843,17 @@ def meth(self):
1841
1843
1842
1844
def test_no_runtime_deco_on_nominal (self ):
1843
1845
with self .assertRaises (TypeError ):
1844
- @runtime
1846
+ @runtime_checkable
1845
1847
class C : pass
1846
1848
class Proto (Protocol ):
1847
1849
x = 1
1848
1850
with self .assertRaises (TypeError ):
1849
- @runtime
1851
+ @runtime_checkable
1850
1852
class Concrete (Proto ):
1851
1853
pass
1852
1854
1853
1855
def test_none_treated_correctly (self ):
1854
- @runtime
1856
+ @runtime_checkable
1855
1857
class P (Protocol ):
1856
1858
x : int = None
1857
1859
class B (object ): pass
@@ -1882,7 +1884,7 @@ def test_protocols_pickleable(self):
1882
1884
global P , CP # pickle wants to reference the class by name
1883
1885
T = TypeVar ('T' )
1884
1886
1885
- @runtime
1887
+ @runtime_checkable
1886
1888
class P (Protocol [T ]):
1887
1889
x = 1
1888
1890
class CP (P [int ]):
0 commit comments