5757 'DefaultDict' ,
5858 'List' ,
5959 'Set' ,
60+ 'FrozenSet' ,
6061 'NamedTuple' , # Not really a type.
6162 'Generator' ,
6263
@@ -160,12 +161,6 @@ def __new__(cls, arg):
160161 return self
161162
162163 def _eval_type (self , globalns , localns ):
163- if not isinstance (localns , dict ):
164- raise TypeError ('ForwardRef localns must be a dict -- got %r' %
165- (localns ,))
166- if not isinstance (globalns , dict ):
167- raise TypeError ('ForwardRef globalns must be a dict -- got %r' %
168- (globalns ,))
169164 if not self .__forward_evaluated__ :
170165 if globalns is None and localns is None :
171166 globalns = localns = {}
@@ -388,9 +383,10 @@ def longest(x: A, y: A) -> A:
388383 and issubclass(bytes, A) are true, and issubclass(int, A) is
389384 false. (TODO: Why is this needed? This may change. See #136.)
390385
391- Type variables may be marked covariant or contravariant by passing
392- covariant=True or contravariant=True. See PEP 484 for more
393- details. By default type variables are invariant.
386+ Type variables defined with covariant=True or contravariant=True
387+ can be used do declare covariant or contravariant generic types.
388+ See PEP 484 for more details. By default generic types are invariant
389+ in all type variables.
394390
395391 Type variables can be introspected. e.g.:
396392
@@ -405,7 +401,7 @@ def __new__(cls, name, *constraints, bound=None,
405401 covariant = False , contravariant = False ):
406402 self = super ().__new__ (cls , name , (Final ,), {}, _root = True )
407403 if covariant and contravariant :
408- raise ValueError ("Bivariant type variables are not supported." )
404+ raise ValueError ("Bivariant types are not supported." )
409405 self .__covariant__ = bool (covariant )
410406 self .__contravariant__ = bool (contravariant )
411407 if constraints and bound is not None :
@@ -782,7 +778,7 @@ def __new__(cls, name, bases, namespace, _root=False,
782778 return self
783779
784780 def _get_type_vars (self , tvars ):
785- if self .__args__ :
781+ if self .__args__ and self . __args__ is not Ellipsis :
786782 _get_type_vars (self .__args__ , tvars )
787783
788784 def _eval_type (self , globalns , localns ):
@@ -1044,7 +1040,7 @@ def __subclasscheck__(self, cls):
10441040 if cls is Any :
10451041 return True
10461042 if isinstance (cls , GenericMeta ):
1047- # For a class C(Generic[T]) where T is co-variant ,
1043+ # For a covariant class C(Generic[T]),
10481044 # C[X] is a subclass of C[Y] iff X is a subclass of Y.
10491045 origin = self .__origin__
10501046 if origin is not None and origin is cls .__origin__ :
@@ -1434,31 +1430,53 @@ class Container(Generic[T_co], extra=collections_abc.Container):
14341430 __slots__ = ()
14351431
14361432
1437- # Callable was defined earlier.
1433+ if hasattr (collections_abc , 'Collection' ):
1434+ class Collection (Sized , Iterable [T_co ], Container [T_co ],
1435+ extra = collections_abc .Collection ):
1436+ __slots__ = ()
14381437
1438+ __all__ .append ('Collection' )
14391439
1440- class AbstractSet (Sized , Iterable [T_co ], Container [T_co ],
1441- extra = collections_abc .Set ):
1442- pass
1440+
1441+ # Callable was defined earlier.
1442+
1443+ if hasattr (collections_abc , 'Collection' ):
1444+ class AbstractSet (Collection [T_co ],
1445+ extra = collections_abc .Set ):
1446+ pass
1447+ else :
1448+ class AbstractSet (Sized , Iterable [T_co ], Container [T_co ],
1449+ extra = collections_abc .Set ):
1450+ pass
14431451
14441452
14451453class MutableSet (AbstractSet [T ], extra = collections_abc .MutableSet ):
14461454 pass
14471455
14481456
1449- # NOTE: Only the value type is covariant.
1450- class Mapping (Sized , Iterable [KT ], Container [KT ], Generic [KT , VT_co ],
1451- extra = collections_abc .Mapping ):
1452- pass
1457+ # NOTE: It is only covariant in the value type.
1458+ if hasattr (collections_abc , 'Collection' ):
1459+ class Mapping (Collection [KT ], Generic [KT , VT_co ],
1460+ extra = collections_abc .Mapping ):
1461+ pass
1462+ else :
1463+ class Mapping (Sized , Iterable [KT ], Container [KT ], Generic [KT , VT_co ],
1464+ extra = collections_abc .Mapping ):
1465+ pass
14531466
14541467
14551468class MutableMapping (Mapping [KT , VT ], extra = collections_abc .MutableMapping ):
14561469 pass
14571470
14581471if hasattr (collections_abc , 'Reversible' ):
1459- class Sequence (Sized , Reversible [T_co ], Container [T_co ],
1460- extra = collections_abc .Sequence ):
1461- pass
1472+ if hasattr (collections_abc , 'Collection' ):
1473+ class Sequence (Reversible [T_co ], Collection [T_co ],
1474+ extra = collections_abc .Sequence ):
1475+ pass
1476+ else :
1477+ class Sequence (Sized , Reversible [T_co ], Container [T_co ],
1478+ extra = collections_abc .Sequence ):
1479+ pass
14621480else :
14631481 class Sequence (Sized , Iterable [T_co ], Container [T_co ],
14641482 extra = collections_abc .Sequence ):
@@ -1583,11 +1601,11 @@ def __new__(cls, *args, **kwds):
15831601
15841602
15851603# Internal type variable used for Type[].
1586- CT = TypeVar ('CT ' , covariant = True , bound = type )
1604+ CT_co = TypeVar ('CT_co ' , covariant = True , bound = type )
15871605
15881606
15891607# This is not a real generic class. Don't use outside annotations.
1590- class Type (type , Generic [CT ], extra = type ):
1608+ class Type (type , Generic [CT_co ], extra = type ):
15911609 """A special construct usable to annotate class objects.
15921610
15931611 For example, suppose we have the following classes::
0 commit comments