[Python-checkins] cpython (merge 3.5 -> default): Issue #23973: Update typing.py from GitHub repo. (Merge from 3.5.)

guido.van.rossum python-checkins at python.org
Wed Aug 5 12:27:39 CEST 2015


https://hg.python.org/cpython/rev/c9a6ce666ff2
changeset:   97267:c9a6ce666ff2
parent:      97263:643b332465d7
parent:      97266:f142b7c7a8e3
user:        Guido van Rossum <guido at python.org>
date:        Wed Aug 05 12:13:11 2015 +0200
summary:
  Issue #23973: Update typing.py from GitHub repo. (Merge from 3.5.)

files:
  Lib/test/test_typing.py |  24 +++++++++++
  Lib/typing.py           |  59 +++++++++++++++++++++++-----
  Misc/NEWS               |   2 +
  3 files changed, 74 insertions(+), 11 deletions(-)


diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -459,6 +459,14 @@
         ctv = Callable[..., str]
         self.assertEqual(repr(ctv), 'typing.Callable[..., str]')
 
+    def test_callable_with_ellipsis(self):
+
+        def foo(a: Callable[..., T]):
+            pass
+
+        self.assertEqual(get_type_hints(foo, globals(), locals()),
+                         {'a': Callable[..., T]})
+
 
 XK = TypeVar('XK', str, bytes)
 XV = TypeVar('XV')
@@ -555,6 +563,14 @@
         with self.assertRaises(TypeError):
             Y[str, bytes]
 
+    def test_init(self):
+        T = TypeVar('T')
+        S = TypeVar('S')
+        with self.assertRaises(TypeError):
+            Generic[T, T]
+        with self.assertRaises(TypeError):
+            Generic[T, S, T]
+
     def test_repr(self):
         self.assertEqual(repr(SimpleMapping),
                          __name__ + '.' + 'SimpleMapping[~XK, ~XV]')
@@ -801,6 +817,14 @@
         self.assertEqual(get_type_hints(foo, globals(), locals()),
                          {'a': Callable[[T], T]})
 
+    def test_callable_with_ellipsis_forward(self):
+
+        def foo(a: 'Callable[..., T]'):
+            pass
+
+        self.assertEqual(get_type_hints(foo, globals(), locals()),
+                         {'a': Callable[..., T]})
+
     def test_syntax_error(self):
 
         with self.assertRaises(SyntaxError):
diff --git a/Lib/typing.py b/Lib/typing.py
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -128,6 +128,8 @@
 class Final:
     """Mix-in class to prevent instantiation."""
 
+    __slots__ = ()
+
     def __new__(self, *args, **kwds):
         raise TypeError("Cannot instantiate %r" % self.__class__)
 
@@ -204,6 +206,8 @@
     False.
     """
 
+    __slots__ = ('name', 'type_var', 'impl_type', 'type_checker')
+
     def __new__(cls, *args, **kwds):
         """Constructor.
 
@@ -341,6 +345,8 @@
     - As a special case, Any and object are subclasses of each other.
     """
 
+    __slots__ = ()
+
 
 class TypeVar(TypingMeta, metaclass=TypingMeta, _root=True):
     """Type variable.
@@ -635,6 +641,8 @@
     Optional[X] is equivalent to Union[X, type(None)].
     """
 
+    __slots__ = ()
+
 
 class TupleMeta(TypingMeta):
     """Metaclass for Tuple."""
@@ -734,6 +742,8 @@
     To specify a variable-length tuple of homogeneous type, use Sequence[T].
     """
 
+    __slots__ = ()
+
 
 class CallableMeta(TypingMeta):
     """Metaclass for Callable."""
@@ -767,7 +777,10 @@
     def _eval_type(self, globalns, localns):
         if self.__args__ is None and self.__result__ is None:
             return self
-        args = [_eval_type(t, globalns, localns) for t in self.__args__]
+        if self.__args__ is Ellipsis:
+            args = self.__args__
+        else:
+            args = [_eval_type(t, globalns, localns) for t in self.__args__]
         result = _eval_type(self.__result__, globalns, localns)
         if args == self.__args__ and result == self.__result__:
             return self
@@ -837,6 +850,8 @@
     such function types are rarely used as callback types.
     """
 
+    __slots__ = ()
+
 
 def _gorg(a):
     """Return the farthest origin of a generic class."""
@@ -947,6 +962,8 @@
                 if not isinstance(p, TypeVar):
                     raise TypeError("Initial parameters must be "
                                     "type variables; got %s" % p)
+            if len(set(params)) != len(params):
+                raise TypeError("All type variables in Generic[...] must be distinct.")
         else:
             if len(params) != len(self.__parameters__):
                 raise TypeError("Cannot change parameter count from %d to %d" %
@@ -1039,6 +1056,8 @@
           # Same body as above.
     """
 
+    __slots__ = ()
+
     def __new__(cls, *args, **kwds):
         next_in_mro = object
         # Look for the last occurrence of Generic or Generic[...].
@@ -1205,6 +1224,7 @@
                         attr != '__abstractmethods__' and
                         attr != '_is_protocol' and
                         attr != '__dict__' and
+                        attr != '__slots__' and
                         attr != '_get_protocol_attrs' and
                         attr != '__parameters__' and
                         attr != '__origin__' and
@@ -1222,6 +1242,8 @@
     such as Hashable).
     """
 
+    __slots__ = ()
+
     _is_protocol = True
 
 
@@ -1232,14 +1254,15 @@
 
 
 class Iterable(Generic[T_co], extra=collections_abc.Iterable):
-    pass
+    __slots__ = ()
 
 
 class Iterator(Iterable[T_co], extra=collections_abc.Iterator):
-    pass
+    __slots__ = ()
 
 
 class SupportsInt(_Protocol):
+    __slots__ = ()
 
     @abstractmethod
     def __int__(self) -> int:
@@ -1247,6 +1270,7 @@
 
 
 class SupportsFloat(_Protocol):
+    __slots__ = ()
 
     @abstractmethod
     def __float__(self) -> float:
@@ -1254,6 +1278,7 @@
 
 
 class SupportsComplex(_Protocol):
+    __slots__ = ()
 
     @abstractmethod
     def __complex__(self) -> complex:
@@ -1261,30 +1286,34 @@
 
 
 class SupportsBytes(_Protocol):
+    __slots__ = ()
 
     @abstractmethod
     def __bytes__(self) -> bytes:
         pass
 
 
-class SupportsAbs(_Protocol[T]):
+class SupportsAbs(_Protocol[T_co]):
+    __slots__ = ()
 
     @abstractmethod
-    def __abs__(self) -> T:
+    def __abs__(self) -> T_co:
         pass
 
 
-class SupportsRound(_Protocol[T]):
+class SupportsRound(_Protocol[T_co]):
+    __slots__ = ()
 
     @abstractmethod
-    def __round__(self, ndigits: int = 0) -> T:
+    def __round__(self, ndigits: int = 0) -> T_co:
         pass
 
 
-class Reversible(_Protocol[T]):
+class Reversible(_Protocol[T_co]):
+    __slots__ = ()
 
     @abstractmethod
-    def __reversed__(self) -> 'Iterator[T]':
+    def __reversed__(self) -> 'Iterator[T_co]':
         pass
 
 
@@ -1292,7 +1321,7 @@
 
 
 class Container(Generic[T_co], extra=collections_abc.Container):
-    pass
+    __slots__ = ()
 
 
 # Callable was defined earlier.
@@ -1308,7 +1337,7 @@
 
 
 # NOTE: Only the value type is covariant.
-class Mapping(Sized, Iterable[KT], Container[KT], Generic[KT, VT_co],
+class Mapping(Sized, Iterable[KT], Container[KT], Generic[VT_co],
               extra=collections_abc.Mapping):
     pass
 
@@ -1366,6 +1395,7 @@
 
 
 class FrozenSet(frozenset, AbstractSet[T_co], metaclass=_FrozenSetMeta):
+    __slots__ = ()
 
     def __new__(cls, *args, **kwds):
         if _geqv(cls, FrozenSet):
@@ -1413,6 +1443,7 @@
 
 class Generator(Iterator[T_co], Generic[T_co, T_contra, V_co],
                 extra=_G_base):
+    __slots__ = ()
 
     def __new__(cls, *args, **kwds):
         if _geqv(cls, Generator):
@@ -1456,6 +1487,8 @@
     way to track the other distinctions in the type system.
     """
 
+    __slots__ = ()
+
     @abstractproperty
     def mode(self) -> str:
         pass
@@ -1540,6 +1573,8 @@
 class BinaryIO(IO[bytes]):
     """Typed version of the return of open() in binary mode."""
 
+    __slots__ = ()
+
     @abstractmethod
     def write(self, s: Union[bytes, bytearray]) -> int:
         pass
@@ -1552,6 +1587,8 @@
 class TextIO(IO[str]):
     """Typed version of the return of open() in text mode."""
 
+    __slots__ = ()
+
     @abstractproperty
     def buffer(self) -> BinaryIO:
         pass
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -13,6 +13,8 @@
 Library
 -------
 
+- Issue #23973: Update typing.py from GitHub repo.
+
 - Issue #23888: Handle fractional time in cookie expiry. Patch by ssh.
 
 - Issue #23652: Make it possible to compile the select module against the

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list