diff options
author | Thaddeus1499 <104600742+Thaddeus1499@users.noreply.github.com> | 2022-05-03 19:17:43 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-03 19:17:43 (GMT) |
commit | f03d3dd9afd5a2df8ffb6db80c0bb45f2d8909f5 (patch) | |
tree | 046f584b7bb333f57a9ddca757d5bd81ae79ad40 | |
parent | ff3e9cdf334737aeedbbec2e5a219084d27db9cd (diff) | |
download | cpython-f03d3dd9afd5a2df8ffb6db80c0bb45f2d8909f5.zip cpython-f03d3dd9afd5a2df8ffb6db80c0bb45f2d8909f5.tar.gz cpython-f03d3dd9afd5a2df8ffb6db80c0bb45f2d8909f5.tar.bz2 |
gh-90172: add test for functools.singledispatch on Union types with None type (#92174)
Signed-off-by: prwatson <prwatson@redhat.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
-rw-r--r-- | Lib/test/test_functools.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py index e3c0656..382e7db 100644 --- a/Lib/test/test_functools.py +++ b/Lib/test/test_functools.py @@ -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): |