@@ -1590,12 +1590,92 @@ class MyCounter(typing_extensions.Counter[int]):
15901590 self .assertIsInstance (d , collections .Counter )
15911591 self .assertIsInstance (d , typing_extensions .Counter )
15921592
1593- def test_async_generator (self ):
1594- async def f ():
1593+
1594+ # These are a separate TestCase class,
1595+ # as (unlike most collections.abc aliases in typing_extensions),
1596+ # these are reimplemented on Python <=3.12 so that we can provide
1597+ # default values for the second and third parameters
1598+ class GeneratorTests (BaseTestCase ):
1599+
1600+ def test_generator_basics (self ):
1601+ def foo ():
15951602 yield 42
1603+ g = foo ()
1604+
1605+ self .assertIsInstance (g , typing_extensions .Generator )
1606+ self .assertNotIsInstance (foo , typing_extensions .Generator )
1607+ self .assertIsSubclass (type (g ), typing_extensions .Generator )
1608+ self .assertNotIsSubclass (type (foo ), typing_extensions .Generator )
1609+
1610+ parameterized = typing_extensions .Generator [int , str , None ]
1611+ with self .assertRaises (TypeError ):
1612+ isinstance (g , parameterized )
1613+ with self .assertRaises (TypeError ):
1614+ issubclass (type (g ), parameterized )
1615+
1616+ def test_generator_default (self ):
1617+ g1 = typing_extensions .Generator [int ]
1618+ g2 = typing_extensions .Generator [int , None , None ]
1619+ self .assertEqual (get_args (g1 ), (int , type (None ), type (None )))
1620+ self .assertEqual (get_args (g1 ), get_args (g2 ))
1621+
1622+ g3 = typing_extensions .Generator [int , float ]
1623+ g4 = typing_extensions .Generator [int , float , None ]
1624+ self .assertEqual (get_args (g3 ), (int , float , type (None )))
1625+ self .assertEqual (get_args (g3 ), get_args (g4 ))
1626+
1627+ def test_no_generator_instantiation (self ):
1628+ with self .assertRaises (TypeError ):
1629+ typing_extensions .Generator ()
1630+ with self .assertRaises (TypeError ):
1631+ typing_extensions .Generator [T , T , T ]()
1632+ with self .assertRaises (TypeError ):
1633+ typing_extensions .Generator [int , int , int ]()
1634+
1635+ def test_subclassing_generator (self ):
1636+ class G (typing_extensions .Generator [int , int , None ]):
1637+ def send (self , value ):
1638+ pass
1639+ def throw (self , typ , val = None , tb = None ):
1640+ pass
15961641
1642+ def g (): yield 0
1643+
1644+ self .assertIsSubclass (G , typing_extensions .Generator )
1645+ self .assertIsSubclass (G , typing_extensions .Iterable )
1646+ self .assertIsSubclass (G , collections .abc .Generator )
1647+ self .assertIsSubclass (G , collections .abc .Iterable )
1648+ self .assertNotIsSubclass (type (g ), G )
1649+
1650+ instance = G ()
1651+ self .assertIsInstance (instance , typing_extensions .Generator )
1652+ self .assertIsInstance (instance , typing_extensions .Iterable )
1653+ self .assertIsInstance (instance , collections .abc .Generator )
1654+ self .assertIsInstance (instance , collections .abc .Iterable )
1655+ self .assertNotIsInstance (type (g ), G )
1656+ self .assertNotIsInstance (g , G )
1657+
1658+ def test_async_generator_basics (self ):
1659+ async def f ():
1660+ yield 42
15971661 g = f ()
1662+
1663+ self .assertIsInstance (g , typing_extensions .AsyncGenerator )
15981664 self .assertIsSubclass (type (g ), typing_extensions .AsyncGenerator )
1665+ self .assertNotIsInstance (f , typing_extensions .AsyncGenerator )
1666+ self .assertNotIsSubclass (type (f ), typing_extensions .AsyncGenerator )
1667+
1668+ parameterized = typing_extensions .AsyncGenerator [int , str ]
1669+ with self .assertRaises (TypeError ):
1670+ isinstance (g , parameterized )
1671+ with self .assertRaises (TypeError ):
1672+ issubclass (type (g ), parameterized )
1673+
1674+ def test_async_generator_default (self ):
1675+ ag1 = typing_extensions .AsyncGenerator [int ]
1676+ ag2 = typing_extensions .AsyncGenerator [int , None ]
1677+ self .assertEqual (get_args (ag1 ), (int , type (None )))
1678+ self .assertEqual (get_args (ag1 ), get_args (ag2 ))
15991679
16001680 def test_no_async_generator_instantiation (self ):
16011681 with self .assertRaises (TypeError ):
@@ -1628,16 +1708,67 @@ async def g(): yield 0
16281708 self .assertNotIsInstance (type (g ), G )
16291709 self .assertNotIsInstance (g , G )
16301710
1631- def test_generator_default (self ):
1632- g1 = typing_extensions .Generator [int ]
1633- g2 = typing_extensions .Generator [int , None , None ]
1634- self .assertEqual (get_args (g1 ), (int , type (None ), type (None )))
1635- self .assertEqual (get_args (g1 ), get_args (g2 ))
1711+ def test_subclassing_subclasshook (self ):
16361712
1637- g3 = typing_extensions .Generator [int , float ]
1638- g4 = typing_extensions .Generator [int , float , None ]
1639- self .assertEqual (get_args (g3 ), (int , float , type (None )))
1640- self .assertEqual (get_args (g3 ), get_args (g4 ))
1713+ class Base (typing_extensions .Generator ):
1714+ @classmethod
1715+ def __subclasshook__ (cls , other ):
1716+ if other .__name__ == 'Foo' :
1717+ return True
1718+ else :
1719+ return False
1720+
1721+ class C (Base ): ...
1722+ class Foo : ...
1723+ class Bar : ...
1724+ self .assertIsSubclass (Foo , Base )
1725+ self .assertIsSubclass (Foo , C )
1726+ self .assertNotIsSubclass (Bar , C )
1727+
1728+ def test_subclassing_register (self ):
1729+
1730+ class A (typing_extensions .Generator ): ...
1731+ class B (A ): ...
1732+
1733+ class C : ...
1734+ A .register (C )
1735+ self .assertIsSubclass (C , A )
1736+ self .assertNotIsSubclass (C , B )
1737+
1738+ class D : ...
1739+ B .register (D )
1740+ self .assertIsSubclass (D , A )
1741+ self .assertIsSubclass (D , B )
1742+
1743+ class M (): ...
1744+ collections .abc .Generator .register (M )
1745+ self .assertIsSubclass (M , typing_extensions .Generator )
1746+
1747+ def test_collections_as_base (self ):
1748+
1749+ class M (collections .abc .Generator ): ...
1750+ self .assertIsSubclass (M , typing_extensions .Generator )
1751+ self .assertIsSubclass (M , typing_extensions .Iterable )
1752+
1753+ class S (collections .abc .AsyncGenerator ): ...
1754+ self .assertIsSubclass (S , typing_extensions .AsyncGenerator )
1755+ self .assertIsSubclass (S , typing_extensions .AsyncIterator )
1756+
1757+ class A (collections .abc .Generator , metaclass = abc .ABCMeta ): ...
1758+ class B : ...
1759+ A .register (B )
1760+ self .assertIsSubclass (B , typing_extensions .Generator )
1761+
1762+ @skipIf (sys .version_info < (3 , 10 ), "PEP 604 has yet to be" )
1763+ def test_or_and_ror (self ):
1764+ self .assertEqual (
1765+ typing_extensions .Generator | typing_extensions .AsyncGenerator ,
1766+ Union [typing_extensions .Generator , typing_extensions .AsyncGenerator ]
1767+ )
1768+ self .assertEqual (
1769+ typing_extensions .Generator | typing .Deque ,
1770+ Union [typing_extensions .Generator , typing .Deque ]
1771+ )
16411772
16421773
16431774class OtherABCTests (BaseTestCase ):
0 commit comments