bpo-35252: Remove FIXME from test_functools (GH-10551)
authorLysandros Nikolaou <lisandrosnik@gmail.com>
Sun, 19 May 2019 22:11:21 +0000 (00:11 +0200)
committerŁukasz Langa <lukasz@langa.pl>
Sun, 19 May 2019 22:11:20 +0000 (15:11 -0700)
Lib/functools.py
Lib/test/test_functools.py
Misc/NEWS.d/next/Library/2019-04-02-19-23-12.bpo-35252.VooTVv.rst [new file with mode: 0644]

index 28d9f6f75fdb8b9fc51492eeee6bd09512d1621f..c863341eec5f21575685cf825bfd51de2b9bbd4c 100644 (file)
@@ -861,9 +861,11 @@ def singledispatch(func):
             # only import typing if annotation parsing is necessary
             from typing import get_type_hints
             argname, cls = next(iter(get_type_hints(func).items()))
-            assert isinstance(cls, type), (
-                f"Invalid annotation for {argname!r}. {cls!r} is not a class."
-            )
+            if not isinstance(cls, type):
+                raise TypeError(
+                    f"Invalid annotation for {argname!r}. "
+                    f"{cls!r} is not a class."
+                )
         registry[cls] = func
         if cache_token is None and hasattr(cls, '__abstractmethods__'):
             cache_token = get_cache_token()
index 85c65d183260673b02d61c339b96c7499be8a420..b89d77967a0df2f57514099fe23a652a00ad8e0a 100644 (file)
@@ -2355,9 +2355,6 @@ class TestSingleDispatch(unittest.TestCase):
         ))
         self.assertTrue(str(exc.exception).endswith(msg_suffix))
 
-        # FIXME: The following will only work after PEP 560 is implemented.
-        return
-
         with self.assertRaises(TypeError) as exc:
             @i.register
             def _(arg: typing.Iterable[str]):
@@ -2366,10 +2363,12 @@ class TestSingleDispatch(unittest.TestCase):
                 # types from `typing`. Instead, annotate with regular types
                 # or ABCs.
                 return "I annotated with a generic collection"
-        self.assertTrue(str(exc.exception).startswith(msg_prefix +
-            "<function TestSingleDispatch.test_invalid_registrations.<locals>._"
+        self.assertTrue(str(exc.exception).startswith(
+            "Invalid annotation for 'arg'."
+        ))
+        self.assertTrue(str(exc.exception).endswith(
+            'typing.Iterable[str] is not a class.'
         ))
-        self.assertTrue(str(exc.exception).endswith(msg_suffix))
 
     def test_invalid_positional_argument(self):
         @functools.singledispatch
diff --git a/Misc/NEWS.d/next/Library/2019-04-02-19-23-12.bpo-35252.VooTVv.rst b/Misc/NEWS.d/next/Library/2019-04-02-19-23-12.bpo-35252.VooTVv.rst
new file mode 100644 (file)
index 0000000..c8f3e7d
--- /dev/null
@@ -0,0 +1 @@
+Throw a TypeError instead of an AssertionError when using an invalid type annotation with singledispatch.