You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Fix inspect.signature() of operator.{attrgetter,itemgetter,methodcaller} instances #118285
On Python 3.12.3 and 3.11.9, it looks like the inspect.Signature object for operator's attrgetter, itemgetter, and methodcaller classes doesn't match their __call__ method signature:
changed the title [-]inspect signature of operator.{attrgetter,itemgetter,methodcaller} doesn't match __call__ definition[/-][+]Fix inspect.signature() of operator.{attrgetter,itemgetter,methodcaller} instances[/+]on Apr 25, 2024
The signatures of the getter/caller classes are the signature of their .__init__ methods, and inspect.signature returns these properly. The signature of callable instances should, it seems, match that of the .__call__ methods, but, as you tested, it does not. However, while the operator callables (functions and these 3 classes) are all initially defined in python, they are all replaced with C implementations, when possible, with try: from _operator import *. After this, itemgetter.__call__, for instance, is a 'wrapper_descriptor' and itemgetter(s).__call__ is a 'method_wrapper'. Perhaps the C code for the call methods was never revised to use argument-clinic.
Actually, the operator module was initially implemented in C. The Python implementation was added later, partially as a fallback for alternative Python implementations (although Jython, PyPy, and other already had their own implementations), partially as experiment (which failed in my opinion).
Now inspect.signature() returns <Signature (*args, **kwargs)> for custom callable objects implemented in C instead of raising a ValueError. It was perhaps not intentional change, but it may be too later to change this again, some code can already rely on this. At least not in 3.11 (which is in the security fixes mode) or 3.12.
In any case, I added a simple way to specify signature for callables implemented in C and implemented it for the operator callables. Whether make inspect.signature() raising a ValueError by default is an open question.
Activity
[-]inspect signature of operator.{attrgetter,itemgetter,methodcaller} doesn't match __call__ definition[/-][+]Fix inspect.signature() of operator.{attrgetter,itemgetter,methodcaller} instances[/+]terryjreedy commentedon Apr 26, 2024
The signatures of the getter/caller classes are the signature of their
.__init__methods, and inspect.signature returns these properly. The signature of callable instances should, it seems, match that of the.__call__methods, but, as you tested, it does not. However, while the operator callables (functions and these 3 classes) are all initially defined in python, they are all replaced with C implementations, when possible, withtry: from _operator import *. After this,itemgetter.__call__, for instance, is a 'wrapper_descriptor' anditemgetter(s).__call__is a 'method_wrapper'. Perhaps the C code for the call methods was never revised to use argument-clinic.pythongh-118285: Fix signatures of operator.{attrgetter,itemgetter,me…
gh-118285: Fix signatures of operator.{attrgetter,itemgetter,methodca…
serhiy-storchaka commentedon Apr 29, 2024
Actually, the
operatormodule was initially implemented in C. The Python implementation was added later, partially as a fallback for alternative Python implementations (although Jython, PyPy, and other already had their own implementations), partially as experiment (which failed in my opinion).Now
inspect.signature()returns<Signature (*args, **kwargs)>for custom callable objects implemented in C instead of raising a ValueError. It was perhaps not intentional change, but it may be too later to change this again, some code can already rely on this. At least not in 3.11 (which is in the security fixes mode) or 3.12.In any case, I added a simple way to specify signature for callables implemented in C and implemented it for the
operatorcallables. Whether makeinspect.signature()raising a ValueError by default is an open question.fix: Avoid using itemgetter with Pydantic
fix: Avoid using itemgetter with Pydantic