@@ -97,8 +97,8 @@ def _qualname(x):
97
97
98
98
99
99
def _trim_name (nm ):
100
- if nm . startswith ( '_' ) and nm not in ( '_TypeAlias' ,
101
- '_ForwardRef' , '_TypingBase' , '_FinalTypingBase' ) :
100
+ whitelist = ( '_TypeAlias' , '_ForwardRef' , '_TypingBase' , '_FinalTypingBase' )
101
+ if nm . startswith ( '_' ) and nm not in whitelist :
102
102
nm = nm [1 :]
103
103
return nm
104
104
@@ -355,13 +355,17 @@ def _type_check(arg, msg):
355
355
return type (None )
356
356
if isinstance (arg , str ):
357
357
arg = _ForwardRef (arg )
358
- if (isinstance (arg , _TypingBase ) and type (arg ).__name__ == '_ClassVar' or
359
- not isinstance (arg , (type , _TypingBase )) and not callable (arg )):
358
+ if (
359
+ isinstance (arg , _TypingBase ) and type (arg ).__name__ == '_ClassVar' or
360
+ not isinstance (arg , (type , _TypingBase )) and not callable (arg )
361
+ ):
360
362
raise TypeError (msg + " Got %.100r." % (arg ,))
361
363
# Bare Union etc. are not valid as type arguments
362
- if (type (arg ).__name__ in ('_Union' , '_Optional' )
363
- and not getattr (arg , '__origin__' , None )
364
- or isinstance (arg , TypingMeta ) and _gorg (arg ) in (Generic , _Protocol )):
364
+ if (
365
+ type (arg ).__name__ in ('_Union' , '_Optional' ) and
366
+ not getattr (arg , '__origin__' , None ) or
367
+ isinstance (arg , TypingMeta ) and _gorg (arg ) in (Generic , _Protocol )
368
+ ):
365
369
raise TypeError ("Plain %s is not valid as type argument" % arg )
366
370
return arg
367
371
@@ -455,7 +459,7 @@ def longest(x: A, y: A) -> A:
455
459
'__covariant__' , '__contravariant__' )
456
460
457
461
def __init__ (self , name , * constraints , bound = None ,
458
- covariant = False , contravariant = False ):
462
+ covariant = False , contravariant = False ):
459
463
super ().__init__ (name , * constraints , bound = bound ,
460
464
covariant = covariant , contravariant = contravariant )
461
465
self .__name__ = name
@@ -563,7 +567,7 @@ def _subs_tree(cls, tvars=None, args=None):
563
567
# ... then continue replacing down the origin chain.
564
568
for ocls in orig_chain :
565
569
new_tree_args = []
566
- for i , arg in enumerate ( ocls .__args__ ) :
570
+ for arg in ocls .__args__ :
567
571
new_tree_args .append (_replace_arg (arg , ocls .__parameters__ , tree_args ))
568
572
tree_args = new_tree_args
569
573
return tree_args
@@ -631,6 +635,7 @@ def _tp_cache(func):
631
635
632
636
cached = functools .lru_cache ()(func )
633
637
_cleanups .append (cached .cache_clear )
638
+
634
639
@functools .wraps (func )
635
640
def inner (* args , ** kwds ):
636
641
try :
@@ -840,7 +845,7 @@ def _next_in_mro(cls):
840
845
# Look for the last occurrence of Generic or Generic[...].
841
846
for i , c in enumerate (cls .__mro__ [:- 1 ]):
842
847
if isinstance (c , GenericMeta ) and _gorg (c ) is Generic :
843
- next_in_mro = cls .__mro__ [i + 1 ]
848
+ next_in_mro = cls .__mro__ [i + 1 ]
844
849
return next_in_mro
845
850
846
851
@@ -849,8 +854,10 @@ def _valid_for_check(cls):
849
854
if cls is Generic :
850
855
raise TypeError ("Class %r cannot be used with class "
851
856
"or instance checks" % cls )
852
- if (cls .__origin__ is not None and
853
- sys ._getframe (3 ).f_globals ['__name__' ] not in ['abc' , 'functools' ]):
857
+ if (
858
+ cls .__origin__ is not None and
859
+ sys ._getframe (3 ).f_globals ['__name__' ] not in ['abc' , 'functools' ]
860
+ ):
854
861
raise TypeError ("Parameterized generics cannot be used with class "
855
862
"or instance checks" )
856
863
@@ -986,9 +993,12 @@ def __new__(cls, name, bases, namespace,
986
993
# This allows unparameterized generic collections to be used
987
994
# with issubclass() and isinstance() in the same way as their
988
995
# collections.abc counterparts (e.g., isinstance([], Iterable)).
989
- if ('__subclasshook__' not in namespace and extra # allow overriding
990
- or hasattr (self .__subclasshook__ , '__name__' ) and
991
- self .__subclasshook__ .__name__ == '__extrahook__' ):
996
+ if (
997
+ # allow overriding
998
+ '__subclasshook__' not in namespace and extra or
999
+ hasattr (self .__subclasshook__ , '__name__' ) and
1000
+ self .__subclasshook__ .__name__ == '__extrahook__'
1001
+ ):
992
1002
self .__subclasshook__ = _make_subclasshook (self )
993
1003
if isinstance (extra , abc .ABCMeta ):
994
1004
self ._abc_registry = extra ._abc_registry
@@ -1192,13 +1202,13 @@ def __getitem__(self, parameters):
1192
1202
return super ().__getitem__ (parameters )
1193
1203
1194
1204
def __instancecheck__ (self , obj ):
1195
- if self .__args__ == None :
1205
+ if self .__args__ is None :
1196
1206
return isinstance (obj , tuple )
1197
1207
raise TypeError ("Parameterized Tuple cannot be used "
1198
1208
"with isinstance()." )
1199
1209
1200
1210
def __subclasscheck__ (self , cls ):
1201
- if self .__args__ == None :
1211
+ if self .__args__ is None :
1202
1212
return issubclass (cls , tuple )
1203
1213
raise TypeError ("Parameterized Tuple cannot be used "
1204
1214
"with issubclass()." )
@@ -1252,7 +1262,7 @@ def __getitem__(self, parameters):
1252
1262
with hashable arguments to improve speed.
1253
1263
"""
1254
1264
1255
- if self .__origin__ is not None or not _geqv (self , Callable ):
1265
+ if self .__origin__ is not None or not _geqv (self , Callable ):
1256
1266
return super ().__getitem__ (parameters )
1257
1267
if not isinstance (parameters , tuple ) or len (parameters ) != 2 :
1258
1268
raise TypeError ("Callable must be used as "
@@ -1280,7 +1290,7 @@ def __getitem_inner__(self, parameters):
1280
1290
return super ().__getitem__ (parameters )
1281
1291
1282
1292
1283
- class Callable (extra = collections_abc .Callable , metaclass = CallableMeta ):
1293
+ class Callable (extra = collections_abc .Callable , metaclass = CallableMeta ):
1284
1294
"""Callable type; Callable[[int], str] is a function of (int) -> str.
1285
1295
1286
1296
The subscription syntax must always be used with exactly two
@@ -1442,10 +1452,12 @@ def get_type_hints(obj, globalns=None, localns=None):
1442
1452
hints = getattr (obj , '__annotations__' , None )
1443
1453
if hints is None :
1444
1454
# Return empty annotations for something that _could_ have them.
1445
- if (isinstance (obj , types .FunctionType ) or
1455
+ if (
1456
+ isinstance (obj , types .FunctionType ) or
1446
1457
isinstance (obj , types .BuiltinFunctionType ) or
1447
1458
isinstance (obj , types .MethodType ) or
1448
- isinstance (obj , types .ModuleType )):
1459
+ isinstance (obj , types .ModuleType )
1460
+ ):
1449
1461
return {}
1450
1462
else :
1451
1463
raise TypeError ('{!r} is not a module, class, method, '
@@ -1485,7 +1497,7 @@ def no_type_check(arg):
1485
1497
no_type_check (obj )
1486
1498
try :
1487
1499
arg .__no_type_check__ = True
1488
- except TypeError : # built-in classes
1500
+ except TypeError : # built-in classes
1489
1501
pass
1490
1502
return arg
1491
1503
@@ -1771,14 +1783,15 @@ class Mapping(Sized, Iterable[KT], Container[KT], Generic[KT, VT_co],
1771
1783
class MutableMapping (Mapping [KT , VT ], extra = collections_abc .MutableMapping ):
1772
1784
__slots__ = ()
1773
1785
1786
+
1774
1787
if hasattr (collections_abc , 'Reversible' ):
1775
1788
if hasattr (collections_abc , 'Collection' ):
1776
1789
class Sequence (Reversible [T_co ], Collection [T_co ],
1777
- extra = collections_abc .Sequence ):
1790
+ extra = collections_abc .Sequence ):
1778
1791
__slots__ = ()
1779
1792
else :
1780
1793
class Sequence (Sized , Reversible [T_co ], Container [T_co ],
1781
- extra = collections_abc .Sequence ):
1794
+ extra = collections_abc .Sequence ):
1782
1795
__slots__ = ()
1783
1796
else :
1784
1797
class Sequence (Sized , Iterable [T_co ], Container [T_co ],
@@ -1804,6 +1817,7 @@ def __new__(cls, *args, **kwds):
1804
1817
"use list() instead" )
1805
1818
return _generic_new (list , cls , * args , ** kwds )
1806
1819
1820
+
1807
1821
class Deque (collections .deque , MutableSequence [T ], extra = collections .deque ):
1808
1822
1809
1823
__slots__ = ()
@@ -1814,6 +1828,7 @@ def __new__(cls, *args, **kwds):
1814
1828
"use deque() instead" )
1815
1829
return _generic_new (collections .deque , cls , * args , ** kwds )
1816
1830
1831
+
1817
1832
class Set (set , MutableSet [T ], extra = set ):
1818
1833
1819
1834
__slots__ = ()
@@ -1871,6 +1886,7 @@ def __new__(cls, *args, **kwds):
1871
1886
"use dict() instead" )
1872
1887
return _generic_new (dict , cls , * args , ** kwds )
1873
1888
1889
+
1874
1890
class DefaultDict (collections .defaultdict , MutableMapping [KT , VT ],
1875
1891
extra = collections .defaultdict ):
1876
1892
@@ -1882,6 +1898,7 @@ def __new__(cls, *args, **kwds):
1882
1898
"use collections.defaultdict() instead" )
1883
1899
return _generic_new (collections .defaultdict , cls , * args , ** kwds )
1884
1900
1901
+
1885
1902
# Determine what base class to use for Generator.
1886
1903
if hasattr (collections_abc , 'Generator' ):
1887
1904
# Sufficiently recent versions of 3.5 have a Generator ABC.
@@ -1901,6 +1918,7 @@ def __new__(cls, *args, **kwds):
1901
1918
"create a subclass instead" )
1902
1919
return _generic_new (_G_base , cls , * args , ** kwds )
1903
1920
1921
+
1904
1922
if hasattr (collections_abc , 'AsyncGenerator' ):
1905
1923
class AsyncGenerator (AsyncIterator [T_co ], Generic [T_co , T_contra ],
1906
1924
extra = collections_abc .AsyncGenerator ):
@@ -1976,14 +1994,15 @@ def __new__(cls, typename, bases, ns):
1976
1994
defaults .append (default_value )
1977
1995
defaults_dict [field_name ] = default_value
1978
1996
elif defaults :
1979
- raise TypeError ("Non-default namedtuple field {field_name} cannot follow default "
1980
- " field(s) {default_names}"
1997
+ raise TypeError ("Non-default namedtuple field {field_name} cannot "
1998
+ "follow default field(s) {default_names}"
1981
1999
.format (field_name = field_name ,
1982
2000
default_names = ', ' .join (defaults_dict .keys ())))
1983
2001
nm_tpl .__new__ .__defaults__ = tuple (defaults )
1984
2002
nm_tpl ._field_defaults = defaults_dict
1985
2003
return nm_tpl
1986
2004
2005
+
1987
2006
class NamedTuple (metaclass = NamedTupleMeta ):
1988
2007
"""Typed version of namedtuple.
1989
2008
@@ -2207,6 +2226,7 @@ class io:
2207
2226
TextIO = TextIO
2208
2227
BinaryIO = BinaryIO
2209
2228
2229
+
2210
2230
io .__name__ = __name__ + '.io'
2211
2231
sys .modules [io .__name__ ] = io
2212
2232
@@ -2224,5 +2244,6 @@ class re:
2224
2244
Pattern = Pattern
2225
2245
Match = Match
2226
2246
2247
+
2227
2248
re .__name__ = __name__ + '.re'
2228
2249
sys .modules [re .__name__ ] = re
0 commit comments