mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
bpo-46014: Add ability to use typing.Union with singledispatch (GH-30017)
This commit is contained in:
parent
810c1769f1
commit
3cb357a2e6
3 changed files with 60 additions and 7 deletions
|
@ -2684,6 +2684,17 @@ class TestSingleDispatch(unittest.TestCase):
|
|||
'typing.Iterable[str] is not a class.'
|
||||
))
|
||||
|
||||
with self.assertRaises(TypeError) as exc:
|
||||
@i.register
|
||||
def _(arg: typing.Union[int, typing.Iterable[str]]):
|
||||
return "Invalid Union"
|
||||
self.assertTrue(str(exc.exception).startswith(
|
||||
"Invalid annotation for 'arg'."
|
||||
))
|
||||
self.assertTrue(str(exc.exception).endswith(
|
||||
'typing.Union[int, typing.Iterable[str]] not all arguments are classes.'
|
||||
))
|
||||
|
||||
def test_invalid_positional_argument(self):
|
||||
@functools.singledispatch
|
||||
def f(*args):
|
||||
|
@ -2692,6 +2703,25 @@ class TestSingleDispatch(unittest.TestCase):
|
|||
with self.assertRaisesRegex(TypeError, msg):
|
||||
f()
|
||||
|
||||
def test_union(self):
|
||||
@functools.singledispatch
|
||||
def f(arg):
|
||||
return "default"
|
||||
|
||||
@f.register
|
||||
def _(arg: typing.Union[str, bytes]):
|
||||
return "typing.Union"
|
||||
|
||||
@f.register
|
||||
def _(arg: int | float):
|
||||
return "types.UnionType"
|
||||
|
||||
self.assertEqual(f([]), "default")
|
||||
self.assertEqual(f(""), "typing.Union")
|
||||
self.assertEqual(f(b""), "typing.Union")
|
||||
self.assertEqual(f(1), "types.UnionType")
|
||||
self.assertEqual(f(1.0), "types.UnionType")
|
||||
|
||||
|
||||
class CachedCostItem:
|
||||
_cost = 1
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue