[Python-checkins] cpython (3.5): Fix issue #24635.

guido.van.rossum python-checkins at python.org
Fri Sep 4 21:57:23 CEST 2015


https://hg.python.org/cpython/rev/438dde69871d
changeset:   97676:438dde69871d
branch:      3.5
parent:      97659:07e04c34bab5
user:        Guido van Rossum <guido at dropbox.com>
date:        Fri Sep 04 12:15:54 2015 -0700
summary:
  Fix issue #24635.

files:
  Lib/test/test_typing.py |  17 ++++++++++++++---
  Lib/typing.py           |  15 ++++++++++-----
  Misc/NEWS               |   3 +++
  3 files changed, 27 insertions(+), 8 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
@@ -436,12 +436,14 @@
             c()
 
     def test_callable_instance_works(self):
-        f = lambda: None
+        def f():
+            pass
         assert isinstance(f, Callable)
         assert not isinstance(None, Callable)
 
     def test_callable_instance_type_error(self):
-        f = lambda: None
+        def f():
+            pass
         with self.assertRaises(TypeError):
             assert isinstance(f, Callable[[], None])
         with self.assertRaises(TypeError):
@@ -674,7 +676,9 @@
         T = TypeVar('T')
 
         class Node(Generic[T]):
-            def __init__(self, label: T, left: 'Node[T]' = None, right: 'Node[T]' = None):
+            def __init__(self, label: T,
+                         left: 'Node[T]' = None,
+                         right: 'Node[T]' = None):
                 self.label = label  # type: T
                 self.left = left  # type: Optional[Node[T]]
                 self.right = right  # type: Optional[Node[T]]
@@ -934,8 +938,15 @@
 
     def test_iterable(self):
         assert isinstance([], typing.Iterable)
+        # Due to ABC caching, the second time takes a separate code
+        # path and could fail.  So call this a few times.
+        assert isinstance([], typing.Iterable)
+        assert isinstance([], typing.Iterable)
         assert isinstance([], typing.Iterable[int])
         assert not isinstance(42, typing.Iterable)
+        # Just in case, also test issubclass() a few times.
+        assert issubclass(list, typing.Iterable)
+        assert issubclass(list, typing.Iterable)
 
     def test_iterator(self):
         it = iter([])
diff --git a/Lib/typing.py b/Lib/typing.py
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -1,7 +1,3 @@
-# TODO:
-# - Generic[T, T] is invalid
-# - Look for TODO below
-
 # TODO nits:
 # Get rid of asserts that are the caller's fault.
 # Docstrings (e.g. ABCs).
@@ -963,7 +959,8 @@
                     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.")
+                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" %
@@ -987,6 +984,14 @@
                               origin=self,
                               extra=self.__extra__)
 
+    def __instancecheck__(self, instance):
+        # Since we extend ABC.__subclasscheck__ and
+        # ABC.__instancecheck__ inlines the cache checking done by the
+        # latter, we must extend __instancecheck__ too. For simplicity
+        # we just skip the cache check -- instance checks for generic
+        # classes are supposed to be rare anyways.
+        return self.__subclasscheck__(instance.__class__)
+
     def __subclasscheck__(self, cls):
         if cls is Any:
             return True
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -15,6 +15,9 @@
 Library
 -------
 
+- Issue #24635: Fixed a bug in typing.py where isinstance([], typing.Iterable)
+  would return True once, then False on subsequent calls.
+
 - Issue #24989: Fixed buffer overread in BytesIO.readline() if a position is
   set beyond size.  Based on patch by John Leitch.
 

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


More information about the Python-checkins mailing list