-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
bpo-32380: Create functools.singledispatchmethod #6306
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dc9d2f8
bpo-32380: Create functools.singledispatchmethod
emmatyping ecf7489
Fix doc reference
emmatyping 5bdcbbc
Add type annotation test and callable test
emmatyping a5d3f28
Use type annotations in example for docs
emmatyping 4c6a4d4
Reformat error message
emmatyping c45b3e3
Rewrite opening to docs, add nested example
emmatyping 99d5efc
Use cls for classmethods
emmatyping 1a4fb85
Actually test the parameters are of the correct type
emmatyping File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2147,6 +2147,124 @@ def __eq__(self, other): | |
return self.arg == other | ||
self.assertEqual(i("str"), "str") | ||
|
||
def test_method_register(self): | ||
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 don't see tests that show that registration of arbitrary callables from outside of the class definition also works. Something like: class C:
@singledispatchmethod
def g(self, arg):
...
@C.g.register(int)
def _(self, arg):
... |
||
class A: | ||
@functools.singledispatchmethod | ||
def t(self, arg): | ||
self.arg = "base" | ||
@t.register(int) | ||
def _(self, arg): | ||
self.arg = "int" | ||
@t.register(str) | ||
def _(self, arg): | ||
self.arg = "str" | ||
a = A() | ||
|
||
a.t(0) | ||
self.assertEqual(a.arg, "int") | ||
aa = A() | ||
self.assertFalse(hasattr(aa, 'arg')) | ||
a.t('') | ||
self.assertEqual(a.arg, "str") | ||
aa = A() | ||
self.assertFalse(hasattr(aa, 'arg')) | ||
a.t(0.0) | ||
self.assertEqual(a.arg, "base") | ||
aa = A() | ||
self.assertFalse(hasattr(aa, 'arg')) | ||
|
||
def test_staticmethod_register(self): | ||
class A: | ||
@functools.singledispatchmethod | ||
@staticmethod | ||
def t(arg): | ||
return arg | ||
@t.register(int) | ||
@staticmethod | ||
def _(arg): | ||
return isinstance(arg, int) | ||
@t.register(str) | ||
@staticmethod | ||
def _(arg): | ||
return isinstance(arg, str) | ||
a = A() | ||
|
||
self.assertTrue(A.t(0)) | ||
self.assertTrue(A.t('')) | ||
self.assertEqual(A.t(0.0), 0.0) | ||
|
||
def test_classmethod_register(self): | ||
class A: | ||
def __init__(self, arg): | ||
self.arg = arg | ||
|
||
@functools.singledispatchmethod | ||
@classmethod | ||
def t(cls, arg): | ||
return cls("base") | ||
@t.register(int) | ||
@classmethod | ||
def _(cls, arg): | ||
return cls("int") | ||
@t.register(str) | ||
@classmethod | ||
def _(cls, arg): | ||
return cls("str") | ||
|
||
self.assertEqual(A.t(0).arg, "int") | ||
self.assertEqual(A.t('').arg, "str") | ||
self.assertEqual(A.t(0.0).arg, "base") | ||
|
||
def test_callable_register(self): | ||
class A: | ||
def __init__(self, arg): | ||
self.arg = arg | ||
|
||
@functools.singledispatchmethod | ||
@classmethod | ||
def t(cls, arg): | ||
return cls("base") | ||
|
||
@A.t.register(int) | ||
@classmethod | ||
def _(cls, arg): | ||
return cls("int") | ||
@A.t.register(str) | ||
@classmethod | ||
def _(cls, arg): | ||
return cls("str") | ||
|
||
self.assertEqual(A.t(0).arg, "int") | ||
self.assertEqual(A.t('').arg, "str") | ||
self.assertEqual(A.t(0.0).arg, "base") | ||
|
||
def test_abstractmethod_register(self): | ||
class Abstract(abc.ABCMeta): | ||
|
||
@functools.singledispatchmethod | ||
@abc.abstractmethod | ||
def add(self, x, y): | ||
pass | ||
|
||
self.assertTrue(Abstract.add.__isabstractmethod__) | ||
|
||
def test_type_ann_register(self): | ||
class A: | ||
@functools.singledispatchmethod | ||
def t(self, arg): | ||
return "base" | ||
@t.register | ||
def _(self, arg: int): | ||
return "int" | ||
@t.register | ||
def _(self, arg: str): | ||
return "str" | ||
a = A() | ||
|
||
self.assertEqual(a.t(0), "int") | ||
self.assertEqual(a.t(''), "str") | ||
self.assertEqual(a.t(0.0), "base") | ||
|
||
def test_invalid_registrations(self): | ||
msg_prefix = "Invalid first argument to `register()`: " | ||
msg_suffix = ( | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Library/2018-03-29-03-09-22.bpo-32380.NhuGig.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Create functools.singledispatchmethod to support generic single dispatch on | ||
descriptors and methods. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having an example stacked with
@classmethod
would be good here. It's unfortunate that the order of application will need to be different from that when stacking@abstractmethod
(see https://docs.python.org/3/library/abc.html#abc.abstractmethod), but we need "register" to be accessible on the resulting descriptor. So something like:Given the
@classmethod
example, you can then note that the same principle applies for@staticmethod
and other decorators - to be useful,@singledispatchmethod
typically needs to be the outermost decorator so that theregister
method is accessible.