`singledispatchmethod` inspected signature incorrectly includes `self`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug report
Bug description:
The result of singledispatchmethod.__get__ is wrapped to imitate the underlying function, instead of the result of func.__get__. This leads to an incorrect signature being reported by inspect.signature, which includes the leading self argument, thus misleading introspection tools as to the real arguments the method accepts.
import inspect
from functools import singledispatchmethod
class A:
@singledispatchmethod
def a(self, arg):
pass
sig = inspect.signature(A().a)
# <Signature (self, arg)> but we expect <Signature (arg)>
sig.bind(None) # should be OK because `A().a(None)` is OK. Instead:
# TypeError: missing a required argument: 'arg'
# because the first argument is erroneously bound to `self`.
One way to fix this might be to edit https://github.com/python/cpython/blob/6258844c27e3b5a43816e7c559089a5fe0a47123/Lib/functools.py#L979 to read instead
update_wrapper(_method, self.func.__get__(obj, cls))
CPython versions tested on:
3.10, 3.12
Operating systems tested on:
Linux, Windows
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Read Lib/functools.py around line 979, focusing on how singledispatchmethod.get wraps the bound method. Run the provided inspect.signature and sig.bind reproduction; done means the bound signature omits self, accepts the method's actual argument, and the relevant tests pass.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 68/100