Skip to content

Update operator from 3.13.5 #5935

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 5 commits into from
Jul 11, 2025
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
10 changes: 5 additions & 5 deletions Lib/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -257,7 +257,7 @@ def func(obj):
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):
Expand All @@ -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):
Expand All @@ -288,7 +288,7 @@ def func(obj):
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):
Expand All @@ -315,7 +315,7 @@ def __init__(self, name, /, *args, **kwargs):
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):
Expand Down
88 changes: 88 additions & 0 deletions Lib/test/test_operator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import unittest
import inspect
import pickle
import sys
from decimal import Decimal
from fractions import Fraction

from test import support
from test.support import import_helper
Expand Down Expand Up @@ -508,6 +511,44 @@ def __getitem__(self, other): return 5 # so that C is a sequence
self.assertEqual(operator.ixor (c, 5), "ixor")
self.assertEqual(operator.iconcat (c, c), "iadd")

def test_iconcat_without_getitem(self):
operator = self.module

msg = "'int' object can't be concatenated"
with self.assertRaisesRegex(TypeError, msg):
operator.iconcat(1, 0.5)

def test_index(self):
operator = self.module
class X:
def __index__(self):
return 1

self.assertEqual(operator.index(X()), 1)
self.assertEqual(operator.index(0), 0)
self.assertEqual(operator.index(1), 1)
self.assertEqual(operator.index(2), 2)
with self.assertRaises((AttributeError, TypeError)):
operator.index(1.5)
with self.assertRaises((AttributeError, TypeError)):
operator.index(Fraction(3, 7))
with self.assertRaises((AttributeError, TypeError)):
operator.index(Decimal(1))
with self.assertRaises((AttributeError, TypeError)):
operator.index(None)

def test_not_(self):
operator = self.module
class C:
def __bool__(self):
raise SyntaxError
self.assertRaises(TypeError, operator.not_)
self.assertRaises(SyntaxError, operator.not_, C())
self.assertFalse(operator.not_(5))
self.assertFalse(operator.not_([0]))
self.assertTrue(operator.not_(0))
self.assertTrue(operator.not_([]))

def test_length_hint(self):
operator = self.module
class X(object):
Expand All @@ -533,6 +574,13 @@ def __length_hint__(self):
with self.assertRaises(LookupError):
operator.length_hint(X(LookupError))

class Y: pass

msg = "'str' object cannot be interpreted as an integer"
with self.assertRaisesRegex(TypeError, msg):
operator.length_hint(X(2), "abc")
self.assertEqual(operator.length_hint(Y(), 10), 10)

def test_call(self):
operator = self.module

Expand All @@ -555,13 +603,53 @@ def test_dunder_is_original(self):
if dunder:
self.assertIs(dunder, orig)

@support.requires_docstrings
def test_attrgetter_signature(self):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried to mark those signature tests with unittest.expectedFailure but then the test fails on "UNEXPECTED SUCCESS", very strange.

I have tried to mess with defining __text_signature__ but I'm guessing that I'm doing something wrong because it completely ignores it. I would appreciate any suggestion that you have:)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it is a base class of multiple other tests

operator = self.module
sig = inspect.signature(operator.attrgetter)
self.assertEqual(str(sig), '(attr, /, *attrs)')
sig = inspect.signature(operator.attrgetter('x', 'z', 'y'))
self.assertEqual(str(sig), '(obj, /)')

@support.requires_docstrings
def test_itemgetter_signature(self):
operator = self.module
sig = inspect.signature(operator.itemgetter)
self.assertEqual(str(sig), '(item, /, *items)')
sig = inspect.signature(operator.itemgetter(2, 3, 5))
self.assertEqual(str(sig), '(obj, /)')

@support.requires_docstrings
def test_methodcaller_signature(self):
operator = self.module
sig = inspect.signature(operator.methodcaller)
self.assertEqual(str(sig), '(name, /, *args, **kwargs)')
sig = inspect.signature(operator.methodcaller('foo', 2, y=3))
self.assertEqual(str(sig), '(obj, /)')


class PyOperatorTestCase(OperatorTestCase, unittest.TestCase):
module = py_operator

@unittest.skipUnless(c_operator, 'requires _operator')
class COperatorTestCase(OperatorTestCase, unittest.TestCase):
module = c_operator

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_attrgetter_signature(self):
super().test_attrgetter_signature()

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_itemgetter_signature(self):
super().test_itemgetter_signature()

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_methodcaller_signature(self):
super().test_methodcaller_signature()


class OperatorPickleTestCase:
def copy(self, obj, proto):
Expand Down
9 changes: 5 additions & 4 deletions vm/src/stdlib/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ mod _operator {
.map(|v| {
if !v.fast_isinstance(vm.ctx.types.int_type) {
return Err(vm.new_type_error(format!(
"'{}' type cannot be interpreted as an integer",
"'{}' object cannot be interpreted as an integer",
v.class().name()
)));
}
Expand Down Expand Up @@ -253,9 +253,10 @@ mod _operator {
if !a.class().has_attr(identifier!(vm, __getitem__))
|| a.fast_isinstance(vm.ctx.types.dict_type)
{
return Err(
vm.new_type_error(format!("{} object can't be concatenated", a.class().name()))
);
return Err(vm.new_type_error(format!(
"'{}' object can't be concatenated",
a.class().name()
)));
}
vm._iadd(&a, &b)
}
Expand Down
Loading