Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit efa798d

Browse files
committed
A new version of typing.py from https://github.com/python/typing.
1 parent 83f5a38 commit efa798d

3 files changed

Lines changed: 61 additions & 26 deletions

File tree

Lib/test/test_typing.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,10 @@ def foo(a: Callable[..., T]):
512512
self.assertEqual(get_type_hints(foo, globals(), locals()),
513513
{'a': Callable[..., T]})
514514

515+
def test_ellipsis_in_generic(self):
516+
# Shouldn't crash; see https://github.com/python/typing/issues/259
517+
typing.List[Callable[..., str]]
518+
515519

516520
XK = TypeVar('XK', str, bytes)
517521
XV = TypeVar('XV')
@@ -852,7 +856,7 @@ def test_covariance_tuple(self):
852856

853857
def test_covariance_sequence(self):
854858
# Check covariance for Sequence (which is just a generic class
855-
# for this purpose, but using a covariant type variable).
859+
# for this purpose, but using a type variable with covariant=True).
856860
self.assertIsSubclass(typing.Sequence[Manager],
857861
typing.Sequence[Employee])
858862
self.assertNotIsSubclass(typing.Sequence[Employee],
@@ -1185,6 +1189,13 @@ def test_container(self):
11851189
self.assertIsInstance([], typing.Container)
11861190
self.assertNotIsInstance(42, typing.Container)
11871191

1192+
def test_collection(self):
1193+
if hasattr(typing, 'Collection'):
1194+
self.assertIsInstance(tuple(), typing.Collection)
1195+
self.assertIsInstance(frozenset(), typing.Collection)
1196+
self.assertIsSubclass(dict, typing.Collection)
1197+
self.assertNotIsInstance(42, typing.Collection)
1198+
11881199
def test_abstractset(self):
11891200
self.assertIsInstance(set(), typing.AbstractSet)
11901201
self.assertNotIsInstance(42, typing.AbstractSet)

Lib/typing.py

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
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

14451453
class 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

14551468
class MutableMapping(Mapping[KT, VT], extra=collections_abc.MutableMapping):
14561469
pass
14571470

14581471
if 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
14621480
else:
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::

Misc/NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ Core and Builtins
5050
Library
5151
-------
5252

53+
- A new version of typing.py from https://github.com/python/typing:
54+
- Collection (only for 3.6) (Issue #27598)
55+
- Add FrozenSet to __all__ (upstream #261)
56+
- fix crash in _get_type_vars() (upstream #259)
57+
- Remove the dict constraint in ForwardRef._eval_type (upstream #252)
58+
5359
- Issue #27539: Fix unnormalised ``Fraction.__pow__`` result in the case
5460
of negative exponent and negative base.
5561

0 commit comments

Comments
 (0)