| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -672,7 +672,7 @@ The :mod:`functools` module defines the following functions: | |||
| 672 | 672 | dispatch>` :term:`generic function`. | |
| 673 | 673 | ||
| 674 | 674 | To define a generic method, decorate it with the ``@singledispatchmethod`` | |
| 675 | - decorator. When defining a function using ``@singledispatchmethod``, note | ||
| 675 | + decorator. When defining a method using ``@singledispatchmethod``, note | ||
| 676 | 676 | that the dispatch happens on the type of the first non-*self* or non-*cls* | |
| 677 | 677 | argument:: | |
| 678 | 678 | ||
@@ -716,6 +716,9 @@ The :mod:`functools` module defines the following functions: | |||
| 716 | 716 | ||
| 717 | 717 | .. versionadded:: 3.8 | |
| 718 | 718 | ||
| 719 | + .. versionchanged:: next | ||
| 720 | + Added support of non-:term:`descriptor` callables. | ||
| 721 | + | ||
| 719 | 722 | ||
| 720 | 723 | .. function:: update_wrapper(wrapper, wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES) | |
| 721 | 724 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -498,6 +498,14 @@ difflib | |||
| 498 | 498 | (Contributed by Jiahao Li in :gh:`134580`.) | |
| 499 | 499 | ||
| 500 | 500 | ||
| 501 | + functools | ||
| 502 | + --------- | ||
| 503 | + | ||
| 504 | + * :func:`~functools.singledispatchmethod` now supports non-:term:`descriptor` | ||
| 505 | + callables. | ||
| 506 | + (Contributed by Serhiy Storchaka in :gh:`140873`.) | ||
| 507 | + | ||
| 508 | + | ||
| 501 | 509 | hashlib | |
| 502 | 510 | ------- | |
| 503 | 511 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1083,7 +1083,10 @@ def __call__(self, /, *args, **kwargs): | |||
| 1083 | 1083 | 'singledispatchmethod method') | |
| 1084 | 1084 | raise TypeError(f'{funcname} requires at least ' | |
| 1085 | 1085 | '1 positional argument') | |
| 1086 | - return self._dispatch(args[0].__class__).__get__(self._obj, self._cls)(*args, **kwargs) | ||
| 1086 | + method = self._dispatch(args[0].__class__) | ||
| 1087 | + if hasattr(method, "__get__"): | ||
| 1088 | + method = method.__get__(self._obj, self._cls) | ||
| 1089 | + return method(*args, **kwargs) | ||
| 1087 | 1090 | ||
| 1088 | 1091 | def __getattr__(self, name): | |
| 1089 | 1092 | # Resolve these attributes lazily to speed up creation of | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2785,7 +2785,7 @@ class Slot: | |||
| 2785 | 2785 | @functools.singledispatchmethod | |
| 2786 | 2786 | @classmethod | |
| 2787 | 2787 | def go(cls, item, arg): | |
| 2788 | - pass | ||
| 2788 | + return item - arg | ||
| 2789 | 2789 | ||
| 2790 | 2790 | @go.register | |
| 2791 | 2791 | @classmethod | |
@@ -2794,7 +2794,9 @@ def _(cls, item: int, arg): | |||
| 2794 | 2794 | ||
| 2795 | 2795 | s = Slot() | |
| 2796 | 2796 | self.assertEqual(s.go(1, 1), 2) | |
| 2797 | + self.assertEqual(s.go(1.5, 1), 0.5) | ||
| 2797 | 2798 | self.assertEqual(Slot.go(1, 1), 2) | |
| 2799 | + self.assertEqual(Slot.go(1.5, 1), 0.5) | ||
| 2798 | 2800 | ||
| 2799 | 2801 | def test_staticmethod_slotted_class(self): | |
| 2800 | 2802 | class A: | |
@@ -3485,6 +3487,37 @@ def _(item, arg: bytes) -> str: | |||
| 3485 | 3487 | self.assertEqual(str(Signature.from_callable(A.static_func)), | |
| 3486 | 3488 | '(item, arg: int) -> str') | |
| 3487 | 3489 | ||
| 3490 | + def test_method_non_descriptor(self): | ||
| 3491 | + class Callable: | ||
| 3492 | + def __init__(self, value): | ||
| 3493 | + self.value = value | ||
| 3494 | + def __call__(self, arg): | ||
| 3495 | + return self.value, arg | ||
| 3496 | + | ||
| 3497 | + class A: | ||
| 3498 | + t = functools.singledispatchmethod(Callable('general')) | ||
| 3499 | + t.register(int, Callable('special')) | ||
| 3500 | + | ||
| 3501 | + @functools.singledispatchmethod | ||
| 3502 | + def u(self, arg): | ||
| 3503 | + return 'general', arg | ||
| 3504 | + u.register(int, Callable('special')) | ||
| 3505 | + | ||
| 3506 | + v = functools.singledispatchmethod(Callable('general')) | ||
| 3507 | + @v.register(int) | ||
| 3508 | + def _(self, arg): | ||
| 3509 | + return 'special', arg | ||
| 3510 | + | ||
| 3511 | + a = A() | ||
| 3512 | + self.assertEqual(a.t(0), ('special', 0)) | ||
| 3513 | + self.assertEqual(a.t(2.5), ('general', 2.5)) | ||
| 3514 | + self.assertEqual(A.t(0), ('special', 0)) | ||
| 3515 | + self.assertEqual(A.t(2.5), ('general', 2.5)) | ||
| 3516 | + self.assertEqual(a.u(0), ('special', 0)) | ||
| 3517 | + self.assertEqual(a.u(2.5), ('general', 2.5)) | ||
| 3518 | + self.assertEqual(a.v(0), ('special', 0)) | ||
| 3519 | + self.assertEqual(a.v(2.5), ('general', 2.5)) | ||
| 3520 | + | ||
| 3488 | 3521 | ||
| 3489 | 3522 | class CachedCostItem: | |
| 3490 | 3523 | _cost = 1 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 1 | + Add support of non-:term:`descriptor` callables in | ||
| 2 | + :func:`functools.singledispatchmethod`. | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments