2121
2222from typing import Any , NoReturn , Never , assert_never
2323from typing import overload , get_overloads , clear_overloads
24- from typing import TypeVar , TypeVarTuple , Unpack , AnyStr
24+ from typing import TypeVar , TypeVarTuple , Unpack
2525from typing import T , KT , VT # Not in __all__.
2626from typing import Union , Optional , Literal
2727from typing import Tuple , List , Dict , MutableMapping
@@ -489,7 +489,6 @@ def test_repr(self):
489489 self .assertEqual (repr (T ), '~T' )
490490 self .assertEqual (repr (KT ), '~KT' )
491491 self .assertEqual (repr (VT ), '~VT' )
492- self .assertEqual (repr (AnyStr ), '~AnyStr' )
493492 T_co = TypeVar ('T_co' , covariant = True )
494493 self .assertEqual (repr (T_co ), '+T_co' )
495494 T_contra = TypeVar ('T_contra' , contravariant = True )
@@ -6388,13 +6387,15 @@ def on_bottom(self, a: int) -> int:
63886387class CastTests (BaseTestCase ):
63896388
63906389 def test_basics (self ):
6390+ T = TypeVar ("T" )
6391+
63916392 self .assertEqual (cast (int , 42 ), 42 )
63926393 self .assertEqual (cast (float , 42 ), 42 )
63936394 self .assertIs (type (cast (float , 42 )), int )
63946395 self .assertEqual (cast (Any , 42 ), 42 )
63956396 self .assertEqual (cast (list , 42 ), 42 )
63966397 self .assertEqual (cast (Union [str , float ], 42 ), 42 )
6397- self .assertEqual (cast (AnyStr , 42 ), 42 )
6398+ self .assertEqual (cast (T , 42 ), 42 )
63986399 self .assertEqual (cast (None , 42 ), 42 )
63996400
64006401 def test_errors (self ):
@@ -6407,9 +6408,10 @@ class AssertTypeTests(BaseTestCase):
64076408
64086409 def test_basics (self ):
64096410 arg = 42
6411+ T = TypeVar ("T" )
64106412 self .assertIs (assert_type (arg , int ), arg )
64116413 self .assertIs (assert_type (arg , str | float ), arg )
6412- self .assertIs (assert_type (arg , AnyStr ), arg )
6414+ self .assertIs (assert_type (arg , T ), arg )
64136415 self .assertIs (assert_type (arg , None ), arg )
64146416
64156417 def test_errors (self ):
@@ -9637,12 +9639,15 @@ def test_no_isinstance(self):
96379639class IOTests (BaseTestCase ):
96389640
96399641 def test_io (self ):
9640-
9641- def stuff (a : IO ) -> AnyStr :
9642+ def stuff (a : IO ):
96429643 return a .readline ()
96439644
96449645 a = stuff .__annotations__ ['a' ]
9645- self .assertEqual (a .__parameters__ , (AnyStr ,))
9646+ self .assertEqual (len (a .__parameters__ ), 1 )
9647+ any_str = a .__parameters__ [0 ]
9648+ self .assertEqual (repr (any_str ), 'AnyStr' )
9649+ self .assertEqual (any_str .__bound__ , None )
9650+ self .assertEqual (any_str .__constraints__ , (bytes , str ))
96469651
96479652 def test_textio (self ):
96489653
@@ -11285,13 +11290,22 @@ def test_all_exported_names(self):
1128511290 # there's a few types and metaclasses that aren't exported
1128611291 not k .endswith (('Meta' , '_contra' , '_co' )) and
1128711292 not k .upper () == k and
11288- k not in {"ByteString" } and
11293+ k not in {"ByteString" , "AnyStr" } and
1128911294 # but export all other things that have __module__ == 'typing'
1129011295 getattr (v , '__module__' , None ) == typing .__name__
1129111296 )
1129211297 }
1129311298 self .assertSetEqual (computed_all , actual_all )
1129411299
11300+ def test_any_str_deprecated (self ):
11301+ # gh-105578
11302+ fresh = import_helper .import_fresh_module ('typing' )
11303+
11304+ with self .assertWarnsRegex (DeprecationWarning , r'typing\.AnyStr' ):
11305+ fresh .AnyStr
11306+
11307+ self .assertEqual (repr (fresh .AnyStr ), '~AnyStr' )
11308+
1129511309
1129611310class TypeIterationTests (BaseTestCase ):
1129711311 _UNITERABLE_TYPES = (
0 commit comments