8000 gh-127750: Restore inspect and pydoc support of singledispatchmethod by serhiy-storchaka · Pull Request #130309 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-127750: Restore inspect and pydoc support of singledispatchmethod #130309

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,10 @@ def __getattr__(self, name):
raise AttributeError
return getattr(self._unbound.func, name)

@property
def __wrapped__(self):
return self._unbound.func

@property
def register(self):
return self._unbound.register
Expand Down
3 changes: 2 additions & 1 deletion Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,8 @@ def isroutine(object):
or isfunction(object)
or ismethod(object)
or ismethoddescriptor(object)
or ismethodwrapper(object))
or ismethodwrapper(object)
or isinstance(object, functools._singledispatchmethod_get))

def isabstract(object):
"""Return true if the object is an abstract base class (ABC)."""
Expand Down
52 changes: 52 additions & 0 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3312,6 +3312,58 @@ def t(self, arg):
support.gc_collect()
self.assertIsNone(r())

def test_signatures(self):
@functools.singledispatch
def func(item, arg: int) -> str:
return str(item)
@func.register
def _(item: int, arg: bytes) -> str:
return str(item)

self.assertEqual(str(Signature.from_callable(func)),
'(item, arg: int) -> str')

def test_method_signatures(self):
class A:
def m(self, item, arg: int) -> str:
return str(item)
@classmethod
def cm(cls, item, arg: int) -> str:
return str(item)
@functools.singledispatchmethod
def func(self, item, arg: int) -> str:
return str(item)
@func.register
def _(self, item, arg: bytes) -> str:
return str(item)

@functools.singledispatchmethod
@classmethod
def cls_func(cls, item, arg: int) -> str:
return str(arg)
@func.register
@classmethod
def _(cls, item, arg: bytes) -> str:
return str(item)

@functools.singledispatchmethod
@staticmethod
def static_func(item, arg: int) -> str:
return str(arg)
@func.register
@staticmethod
def _(item, arg: bytes) -> str:
return str(item)

self.assertEqual(str(Signature.from_callable(A.func)),
'(self, item, arg: int) -> str')
self.assertEqual(str(Signature.from_callable(A().func)),
'(self, item, arg: int) -> str')
self.assertEqual(str(Signature.from_callable(A.cls_func)),
'(cls, item, arg: int) -> str')
self.assertEqual(str(Signature.from_callable(A.static_func)),
'(item, arg: int) -> str')


class CachedCostItem:
_cost = 1
Expand Down
21 changes: 21 additions & 0 deletions Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,27 @@ def test_isroutine(self):
# partial
self.assertTrue(inspect.isroutine(functools.partial(mod.spam)))

def test_isroutine_singledispatch(self):
self.assertTrue(inspect.isroutine(functools.singledispatch(mod.spam)))

class A:
@functools.singledispatchmethod
def method(self, arg):
pass
@functools.singledispatchmethod
@classmethod
def class_method(cls, arg):
pass
@functools.singledispatchmethod
@staticmethod
def static_method(arg):
pass

self.assertTrue(inspect.isroutine(A.method))
self.assertTrue(inspect.isroutine(A().method))
self.assertTrue(inspect.isroutine(A.static_method))
self.assertTrue(inspect.isroutine(A.class_method))

def test_isclass(self):
self.istest(inspect.isclass, 'mod.StupidGit')
self.assertTrue(inspect.isclass(list))
Expand Down
Loading
0