Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-90172: add test for functools.singledispatch on Union types with None type #92174

Merged
merged 4 commits into from
May 3, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Feedback from Serhiy
  • Loading branch information
JelleZijlstra committed May 3, 2022
commit f587878b7a1cb5259d96705a37e89eb56c416f26
34 changes: 21 additions & 13 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2792,40 +2792,48 @@ def _(arg: int | float):
self.assertEqual(f(1), "types.UnionType")
self.assertEqual(f(1.0), "types.UnionType")

def test_union_None(self):
def test_union_conflict(self):
@functools.singledispatch
def f(arg):
return "default"

@f.register
def _(arg: typing.Union[str, None]):
def _(arg: typing.Union[str, bytes]):
return "typing.Union"

@f.register
def _(arg: int):
def _(arg: int | str):
return "types.UnionType"

self.assertEqual(f([]), "default")
self.assertEqual(f(""), "typing.Union")
self.assertEqual(f(None), "typing.Union")
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 f(arg):
def typing_union(arg):
return "default"

@f.register
def _(arg: str):
@typing_union.register
def _(arg: typing.Union[str, None]):
return "typing.Union"

@f.register
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"

JelleZijlstra marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(f([]), "default")
self.assertEqual(f(""), "typing.Union")
self.assertEqual(f(1), "types.UnionType")
self.assertEqual(f(None), "types.UnionType")
self.assertEqual(types_union(""), "default")
self.assertEqual(types_union(1), "types.UnionType")
self.assertEqual(types_union(None), "types.UnionType")

JelleZijlstra marked this conversation as resolved.
Show resolved Hide resolved
def test_register_genericalias(self):
@functools.singledispatch
Expand Down