]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-90172: add test for functools.singledispatch on Union types with None type (#92174)
authorThaddeus1499 <104600742+Thaddeus1499@users.noreply.github.com>
Tue, 3 May 2022 19:17:43 +0000 (15:17 -0400)
committerGitHub <noreply@github.com>
Tue, 3 May 2022 19:17:43 +0000 (13:17 -0600)
Signed-off-by: prwatson <prwatson@redhat.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Lib/test/test_functools.py

index e3c065615778f9445d1a8015b99b98376b2788ed..382e7dbffddf9d14f2e7fcb7b596dcbc114ce9b1 100644 (file)
@@ -2792,6 +2792,49 @@ class TestSingleDispatch(unittest.TestCase):
         self.assertEqual(f(1), "types.UnionType")
         self.assertEqual(f(1.0), "types.UnionType")
 
+    def test_union_conflict(self):
+        @functools.singledispatch
+        def f(arg):
+            return "default"
+
+        @f.register
+        def _(arg: typing.Union[str, bytes]):
+            return "typing.Union"
+
+        @f.register
+        def _(arg: int | str):
+            return "types.UnionType"
+
+        self.assertEqual(f([]), "default")
+        self.assertEqual(f(""), "types.UnionType")  # last one wins
+        self.assertEqual(f(b""), "typing.Union")
+        self.assertEqual(f(1), "types.UnionType")
+
+    def test_union_None(self):
+        @functools.singledispatch
+        def typing_union(arg):
+            return "default"
+
+        @typing_union.register
+        def _(arg: typing.Union[str, None]):
+            return "typing.Union"
+
+        self.assertEqual(typing_union(1), "default")
+        self.assertEqual(typing_union(""), "typing.Union")
+        self.assertEqual(typing_union(None), "typing.Union")
+
+        @functools.singledispatch
+        def types_union(arg):
+            return "default"
+
+        @types_union.register
+        def _(arg: int | None):
+            return "types.UnionType"
+
+        self.assertEqual(types_union(""), "default")
+        self.assertEqual(types_union(1), "types.UnionType")
+        self.assertEqual(types_union(None), "types.UnionType")
+
     def test_register_genericalias(self):
         @functools.singledispatch
         def f(arg):