4141from typing_extensions import Doc , NoDefault
4242from _typed_dict_test_helper import Foo , FooGeneric , VeryAnnotated
4343
44+ NoneType = type (None )
45+
4446# Flags used to mark tests that only apply after a specific
4547# version of the typing module.
4648TYPING_3_9_0 = sys .version_info [:3 ] >= (3 , 9 , 0 )
@@ -1626,6 +1628,17 @@ async def g(): yield 0
16261628 self .assertNotIsInstance (type (g ), G )
16271629 self .assertNotIsInstance (g , G )
16281630
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 ))
1636+
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 ))
1641+
16291642
16301643class OtherABCTests (BaseTestCase ):
16311644
@@ -1638,6 +1651,12 @@ def manager():
16381651 self .assertIsInstance (cm , typing_extensions .ContextManager )
16391652 self .assertNotIsInstance (42 , typing_extensions .ContextManager )
16401653
1654+ def test_contextmanager_type_params (self ):
1655+ cm1 = typing_extensions .ContextManager [int ]
1656+ self .assertEqual (get_args (cm1 ), (int , typing .Optional [bool ]))
1657+ cm2 = typing_extensions .ContextManager [int , None ]
1658+ self .assertEqual (get_args (cm2 ), (int , NoneType ))
1659+
16411660 def test_async_contextmanager (self ):
16421661 class NotACM :
16431662 pass
@@ -1649,11 +1668,20 @@ def manager():
16491668
16501669 cm = manager ()
16511670 self .assertNotIsInstance (cm , typing_extensions .AsyncContextManager )
1652- self .assertEqual (typing_extensions .AsyncContextManager [int ].__args__ , (int ,))
1671+ self .assertEqual (
1672+ typing_extensions .AsyncContextManager [int ].__args__ ,
1673+ (int , typing .Optional [bool ])
1674+ )
16531675 with self .assertRaises (TypeError ):
16541676 isinstance (42 , typing_extensions .AsyncContextManager [int ])
16551677 with self .assertRaises (TypeError ):
1656- typing_extensions .AsyncContextManager [int , str ]
1678+ typing_extensions .AsyncContextManager [int , str , float ]
1679+
1680+ def test_asynccontextmanager_type_params (self ):
1681+ cm1 = typing_extensions .AsyncContextManager [int ]
1682+ self .assertEqual (get_args (cm1 ), (int , typing .Optional [bool ]))
1683+ cm2 = typing_extensions .AsyncContextManager [int , None ]
1684+ self .assertEqual (get_args (cm2 ), (int , NoneType ))
16571685
16581686
16591687class TypeTests (BaseTestCase ):
@@ -5533,28 +5561,25 @@ def test_all_names_in___all__(self):
55335561 self .assertLessEqual (exclude , actual_names )
55345562
55355563 def test_typing_extensions_defers_when_possible (self ):
5536- exclude = {
5537- 'dataclass_transform' ,
5538- 'overload' ,
5539- 'ParamSpec' ,
5540- 'TypeVar' ,
5541- 'TypeVarTuple' ,
5542- 'get_type_hints' ,
5543- }
5564+ exclude = set ()
55445565 if sys .version_info < (3 , 10 ):
55455566 exclude |= {'get_args' , 'get_origin' }
55465567 if sys .version_info < (3 , 10 , 1 ):
55475568 exclude |= {"Literal" }
55485569 if sys .version_info < (3 , 11 ):
5549- exclude |= {'final' , 'Any' , 'NewType' }
5570+ exclude |= {'final' , 'Any' , 'NewType' , 'overload' }
55505571 if sys .version_info < (3 , 12 ):
55515572 exclude |= {
55525573 'SupportsAbs' , 'SupportsBytes' ,
55535574 'SupportsComplex' , 'SupportsFloat' , 'SupportsIndex' , 'SupportsInt' ,
5554- 'SupportsRound' , 'Unpack' ,
5575+ 'SupportsRound' , 'Unpack' , 'dataclass_transform' ,
55555576 }
55565577 if sys .version_info < (3 , 13 ):
5557- exclude |= {'NamedTuple' , 'Protocol' , 'runtime_checkable' }
5578+ exclude |= {
5579+ 'NamedTuple' , 'Protocol' , 'runtime_checkable' , 'Generator' ,
5580+ 'AsyncGenerator' , 'ContextManager' , 'AsyncContextManager' ,
5581+ 'ParamSpec' , 'TypeVar' , 'TypeVarTuple' , 'get_type_hints' ,
5582+ }
55585583 if not typing_extensions ._PEP_728_IMPLEMENTED :
55595584 exclude |= {'TypedDict' , 'is_typeddict' }
55605585 for item in typing_extensions .__all__ :
0 commit comments