FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Vibe-coded singledispatch mod to support .__getitem__ as .dispatch by gvanrossum · Pull Request #159806 · python/cpython · GitHub

Repository navigation

Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
3 changes: 3 additions & 0 deletions Doc/library/functools.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
Expand Up @@ -641,6 +641,9 @@ The :mod:`!functools` module defines the following functions:
>>> fun.dispatch(dict) # note: default implementation
<function fun at 0x103fe0000>

Subscription also returns the implementation selected for a given type:
``fun[float]`` is equivalent to ``fun.dispatch(float)``.

To access all registered implementations, use the read-only ``registry``
attribute::

Expand Down
25 changes: 25 additions & 0 deletions Lib/functools.py
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
Expand Up @@ -898,6 +898,30 @@ def _find_impl(cls, registry):
match = t
return registry.get(match)

class _SingleDispatchCallable:
__slots__ = ('_wrapper', '__dict__', '__weakref__')

def __init__(self, wrapper):
self._wrapper = wrapper

# Preserve the wrapper frame name used by importlib.resources caller detection.
def wrapper(self, /, *args, **kwds):
return self._wrapper(*args, **kwds)

__call__ = wrapper

def __getitem__(self, cls):
return self.dispatch(cls)

def __get__(self, obj, cls=None):
if obj is None:
return self
return MethodType(self, obj)

def __reduce__(self):
return self.__qualname__


def singledispatch(func):
"""Single-dispatch generic function decorator.

Expand Down Expand Up @@ -1009,6 +1033,7 @@ def wrapper(*args, **kw):

funcname = getattr(func, '__name__', 'singledispatch function')
registry[object] = func
wrapper = _SingleDispatchCallable(wrapper)
wrapper.register = register
wrapper.dispatch = dispatch
wrapper.registry = MappingProxyType(registry)
Expand Down
48 changes: 48 additions & 0 deletions Lib/test/test_functools.py
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
Expand Up @@ -2305,7 +2305,55 @@ def cached_staticmeth(x, y):
return 3 * x + y


@functools.singledispatch
def _pickle_single_dispatch(obj):
return obj


class TestSingleDispatch(unittest.TestCase):
def test_subscription(self):
@functools.singledispatch
def g(obj):
return 'default'

@g.register(int)
def g_int(obj):
return 'integer'

@g.register(float)
def g_float(obj):
return 'float'

self.assertIs(g[int], g_int)
self.assertIs(g[float], g_float)
self.assertIs(g[bool], g_int)
self.assertIs(g[str], g.__wrapped__)
self.assertEqual(g[int](42), 'integer')
self.assertIs(g[int], g.dispatch(int))

@g.register(int)
def replacement(obj):
return 'replacement'

self.assertIs(g[int], replacement)

def test_descriptor(self):
class A:
@functools.singledispatch
def method(self, value):
return value

a = A()
self.assertEqual(a.method(42), 42)
self.assertIs(a.method.__func__, A.method)
self.assertIs(A.method[A], A.method.__wrapped__)

def test_pickle_generic_function(self):
for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
with self.subTest(protocol=protocol):
self.assertIs(pickle.loads(pickle.dumps(
_pickle_single_dispatch, protocol)), _pickle_single_dispatch)

def test_simple_overloads(self):
@functools.singledispatch
def g(obj):
Expand Down
Loading

Back | FazBrowse Home | New Git URL