Skip to content

Commit f03d3dd

Browse files
gh-90172: add test for functools.singledispatch on Union types with None type (#92174)
Signed-off-by: prwatson <[email protected]> Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent ff3e9cd commit f03d3dd

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Lib/test/test_functools.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2792,6 +2792,49 @@ def _(arg: int | float):
27922792
self.assertEqual(f(1), "types.UnionType")
27932793
self.assertEqual(f(1.0), "types.UnionType")
27942794

2795+
def test_union_conflict(self):
2796+
@functools.singledispatch
2797+
def f(arg):
2798+
return "default"
2799+
2800+
@f.register
2801+
def _(arg: typing.Union[str, bytes]):
2802+
return "typing.Union"
2803+
2804+
@f.register
2805+
def _(arg: int | str):
2806+
return "types.UnionType"
2807+
2808+
self.assertEqual(f([]), "default")
2809+
self.assertEqual(f(""), "types.UnionType") # last one wins
2810+
self.assertEqual(f(b""), "typing.Union")
2811+
self.assertEqual(f(1), "types.UnionType")
2812+
2813+
def test_union_None(self):
2814+
@functools.singledispatch
2815+
def typing_union(arg):
2816+
return "default"
2817+
2818+
@typing_union.register
2819+
def _(arg: typing.Union[str, None]):
2820+
return "typing.Union"
2821+
2822+
self.assertEqual(typing_union(1), "default")
2823+
self.assertEqual(typing_union(""), "typing.Union")
2824+
self.assertEqual(typing_union(None), "typing.Union")
2825+
2826+
@functools.singledispatch
2827+
def types_union(arg):
2828+
return "default"
2829+
2830+
@types_union.register
2831+
def _(arg: int | None):
2832+
return "types.UnionType"
2833+
2834+
self.assertEqual(types_union(""), "default")
2835+
self.assertEqual(types_union(1), "types.UnionType")
2836+
self.assertEqual(types_union(None), "types.UnionType")
2837+
27952838
def test_register_genericalias(self):
27962839
@functools.singledispatch
27972840
def f(arg):

0 commit comments

Comments
 (0)