-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
GH-100623: Implement singledispatch on type/class arguments #100624
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
Changes from 9 commits
d2d09c1
31e1ae6
b874773
78d25ce
1ec0ad4
7da2946
b5171c5
9761b56
eb23d05
8a648bc
2183609
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2905,6 +2905,88 @@ def _(arg: typing.List[float] | bytes): | |
| self.assertEqual(f(""), "default") | ||
| self.assertEqual(f(b""), "default") | ||
|
|
||
| def test_type_argument(self): | ||
| @functools.singledispatch | ||
| def f(arg): | ||
| return "default" | ||
|
|
||
| @f.register | ||
| def _(arg: type[int]): | ||
| return "type[int]" | ||
|
|
||
| @f.register | ||
| def _(arg: typing.Type[float]): | ||
| return "type[float]" | ||
|
|
||
| @f.register(type[str]) | ||
| def _(arg): | ||
| return "type[str]" | ||
|
|
||
| @f.register(typing.Type[bytes]) | ||
| def _(arg): | ||
| return "type[bytes]" | ||
|
|
||
| self.assertEqual(f(int), "type[int]") | ||
| self.assertEqual(f(float), "type[float]") | ||
| self.assertEqual(f(str), "type[str]") | ||
| self.assertEqual(f(bytes), "type[bytes]") | ||
| self.assertEqual(f(2), "default") | ||
|
|
||
| def test_type_argument_mro(self): | ||
| class A: | ||
| pass | ||
|
|
||
| class B(A): | ||
| pass | ||
|
|
||
| class C: | ||
| pass | ||
|
|
||
| @functools.singledispatch | ||
| def f(arg): | ||
| return "default" | ||
|
|
||
| @f.register | ||
| def _(arg: type[A]): | ||
| return "type[A]" | ||
|
|
||
| @f.register | ||
| def _(arg: B): | ||
| return "B" | ||
|
|
||
| self.assertEqual(f(B), "type[A]") | ||
| self.assertEqual(f(C), "default") | ||
|
|
||
| def test_type_argument_unions(self): | ||
| @functools.singledispatch | ||
| def f(arg): | ||
| return "default" | ||
|
|
||
| @f.register | ||
| def _(arg: type[list|dict]): | ||
| return "type[list|dict]" | ||
|
|
||
| @f.register | ||
| def _(arg: type[set]|typing.Type[type(None)]): | ||
| return "type[set]|type[NoneType]" | ||
|
|
||
| self.assertEqual(f(list), "type[list|dict]") | ||
| self.assertEqual(f(type(None)), "type[set]|type[NoneType]") | ||
|
Comment on lines
+2965
to
+2974
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering if treating E.g. if we have a single-dispatch function with an implementation for So maybe unions of OTOH, it's unlikely that introducing support for dispatching to |
||
|
|
||
| def test_type_argument_invalid_types(self): | ||
| @functools.singledispatch | ||
| def f(arg): | ||
| return "default" | ||
|
|
||
| with self.assertRaisesRegex(TypeError, "Invalid annotation for 'arg'"): | ||
| @f.register | ||
| def _(arg: type[2]): | ||
| pass | ||
|
|
||
| with self.assertRaisesRegex(TypeError, "Invalid annotation for 'arg'"): | ||
| @f.register | ||
| def _(arg: typing.Type[int]|type[3]): | ||
| pass | ||
|
|
||
| class CachedCostItem: | ||
| _cost = 1 | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.