gh-118285: Fix signatures of operator.{attrgetter,itemgetter,methodcaller} instances (GH-118316)

* Allow to specify the signature of custom callable instances of extension
  type by the __text_signature__ attribute.
* Specify signatures of operator.attrgetter, operator.itemgetter, and
  operator.methodcaller instances.
This commit is contained in:
Serhiy Storchaka 2024-04-29 19:30:48 +03:00 committed by GitHub
parent 51c70de998
commit 444ac0b7a6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 76 additions and 5 deletions

View file

@ -239,7 +239,7 @@ class attrgetter:
"""
__slots__ = ('_attrs', '_call')
def __init__(self, attr, *attrs):
def __init__(self, attr, /, *attrs):
if not attrs:
if not isinstance(attr, str):
raise TypeError('attribute name must be a string')
@ -257,7 +257,7 @@ class attrgetter:
return tuple(getter(obj) for getter in getters)
self._call = func
def __call__(self, obj):
def __call__(self, obj, /):
return self._call(obj)
def __repr__(self):
@ -276,7 +276,7 @@ class itemgetter:
"""
__slots__ = ('_items', '_call')
def __init__(self, item, *items):
def __init__(self, item, /, *items):
if not items:
self._items = (item,)
def func(obj):
@ -288,7 +288,7 @@ class itemgetter:
return tuple(obj[i] for i in items)
self._call = func
def __call__(self, obj):
def __call__(self, obj, /):
return self._call(obj)
def __repr__(self):
@ -315,7 +315,7 @@ class methodcaller:
self._args = args
self._kwargs = kwargs
def __call__(self, obj):
def __call__(self, obj, /):
return getattr(obj, self._name)(*self._args, **self._kwargs)
def __repr__(self):