diff options
author | Matti Picus <matti.picus@gmail.com> | 2022-12-01 15:08:45 +0200 |
---|---|---|
committer | Matti Picus <matti.picus@gmail.com> | 2022-12-01 15:08:45 +0200 |
commit | c5a1d0882e5f7225f8ba27b97b20ba0831c7440e (patch) | |
tree | e4c6bdce85c3cee0b7745f3e1355a90df73d5cca | |
parent | change candidate to final (diff) | |
download | pypy-c5a1d0882e5f7225f8ba27b97b20ba0831c7440e.tar.gz pypy-c5a1d0882e5f7225f8ba27b97b20ba0831c7440e.tar.bz2 pypy-c5a1d0882e5f7225f8ba27b97b20ba0831c7440e.zip |
remove redundnat lib_pypy/_functools.py and re-sync test_functools.py (issue 3861)
-rw-r--r-- | lib-python/3/test/test_functools.py | 177 | ||||
-rw-r--r-- | lib_pypy/_functools.py | 152 |
2 files changed, 0 insertions, 329 deletions
diff --git a/lib-python/3/test/test_functools.py b/lib-python/3/test/test_functools.py index 8c33c79e3f..9fd6d21e17 100644 --- a/lib-python/3/test/test_functools.py +++ b/lib-python/3/test/test_functools.py @@ -2565,183 +2565,6 @@ class TestSingleDispatch(unittest.TestCase): self.assertEqual(without_single_dispatch_foo, single_dispatch_foo) self.assertEqual(single_dispatch_foo, '5') - - self.assertEqual( - WithoutSingleDispatch.decorated_classmethod(5), - WithSingleDispatch.decorated_classmethod(5) - ) - - self.assertEqual(WithSingleDispatch.decorated_classmethod(5), '5') - - # Behavioural checks now follow - for method_name in ('cls_context_manager', 'decorated_classmethod'): - with self.subTest(method=method_name): - self.assertEqual( - getattr(WithSingleDispatch, method_name).__name__, - getattr(WithoutSingleDispatch, method_name).__name__ - ) - - self.assertEqual( - getattr(WithSingleDispatch(), method_name).__name__, - getattr(WithoutSingleDispatch(), method_name).__name__ - ) - - for meth in ( - WithSingleDispatch.cls_context_manager, - WithSingleDispatch().cls_context_manager, - WithSingleDispatch.decorated_classmethod, - WithSingleDispatch().decorated_classmethod - ): - with self.subTest(meth=meth): - self.assertEqual(meth.__doc__, 'My function docstring') - self.assertEqual(meth.__annotations__['arg'], int) - - self.assertEqual( - WithSingleDispatch.cls_context_manager.__name__, - 'cls_context_manager' - ) - self.assertEqual( - WithSingleDispatch().cls_context_manager.__name__, - 'cls_context_manager' - ) - self.assertEqual( - WithSingleDispatch.decorated_classmethod.__name__, - 'decorated_classmethod' - ) - self.assertEqual( - WithSingleDispatch().decorated_classmethod.__name__, - 'decorated_classmethod' - ) - - def test_staticmethod_type_ann_register(self): - class A: - @functools.singledispatchmethod - @staticmethod - def t(arg): - return arg - @t.register - @staticmethod - def _(arg: int): - return isinstance(arg, int) - @t.register - @staticmethod - def _(arg: str): - return isinstance(arg, str) - a = A() - - self.assertTrue(A.t(0)) - self.assertTrue(A.t('')) - self.assertEqual(A.t(0.0), 0.0) - - def test_classmethod_type_ann_register(self): - class A: - def __init__(self, arg): - self.arg = arg - - @functools.singledispatchmethod - @classmethod - def t(cls, arg): - return cls("base") - @t.register - @classmethod - def _(cls, arg: int): - return cls("int") - @t.register - @classmethod - def _(cls, arg: str): - return cls("str") - - self.assertEqual(A.t(0).arg, "int") - self.assertEqual(A.t('').arg, "str") - self.assertEqual(A.t(0.0).arg, "base") - - def test_method_wrapping_attributes(self): - class A: - @functools.singledispatchmethod - def func(self, arg: int) -> str: - """My function docstring""" - return str(arg) - @functools.singledispatchmethod - @classmethod - def cls_func(cls, arg: int) -> str: - """My function docstring""" - return str(arg) - @functools.singledispatchmethod - @staticmethod - def static_func(arg: int) -> str: - """My function docstring""" - return str(arg) - - for meth in ( - A.func, - A().func, - A.cls_func, - A().cls_func, - A.static_func, - A().static_func - ): - with self.subTest(meth=meth): - self.assertEqual(meth.__doc__, 'My function docstring') - self.assertEqual(meth.__annotations__['arg'], int) - - self.assertEqual(A.func.__name__, 'func') - self.assertEqual(A().func.__name__, 'func') - self.assertEqual(A.cls_func.__name__, 'cls_func') - self.assertEqual(A().cls_func.__name__, 'cls_func') - self.assertEqual(A.static_func.__name__, 'static_func') - self.assertEqual(A().static_func.__name__, 'static_func') - - def test_double_wrapped_methods(self): - def classmethod_friendly_decorator(func): - wrapped = func.__func__ - @classmethod - @functools.wraps(wrapped) - def wrapper(*args, **kwargs): - return wrapped(*args, **kwargs) - return wrapper - - class WithoutSingleDispatch: - @classmethod - @contextlib.contextmanager - def cls_context_manager(cls, arg: int) -> str: - try: - yield str(arg) - finally: - return 'Done' - - @classmethod_friendly_decorator - @classmethod - def decorated_classmethod(cls, arg: int) -> str: - return str(arg) - - class WithSingleDispatch: - @functools.singledispatchmethod - @classmethod - @contextlib.contextmanager - def cls_context_manager(cls, arg: int) -> str: - """My function docstring""" - try: - yield str(arg) - finally: - return 'Done' - - @functools.singledispatchmethod - @classmethod_friendly_decorator - @classmethod - def decorated_classmethod(cls, arg: int) -> str: - """My function docstring""" - return str(arg) - - # These are sanity checks - # to test the test itself is working as expected - with WithoutSingleDispatch.cls_context_manager(5) as foo: - without_single_dispatch_foo = foo - - with WithSingleDispatch.cls_context_manager(5) as foo: - single_dispatch_foo = foo - - self.assertEqual(without_single_dispatch_foo, single_dispatch_foo) - self.assertEqual(single_dispatch_foo, '5') self.assertEqual( WithoutSingleDispatch.decorated_classmethod(5), diff --git a/lib_pypy/_functools.py b/lib_pypy/_functools.py deleted file mode 100644 index bba43c1b02..0000000000 --- a/lib_pypy/_functools.py +++ /dev/null @@ -1,152 +0,0 @@ -""" Supplies the internal functions for functools.py in the standard library """ -try: from __pypy__ import builtinify -except ImportError: builtinify = lambda f: f - -try: from reprlib import recursive_repr as _recursive_repr -except ImportError: _recursive_repr = lambda: (lambda f: f) -from _pypy_generic_alias import GenericAlias - - -sentinel = object() - -@builtinify -def reduce(func, sequence, initial=sentinel): - """reduce(function, sequence[, initial]) -> value - -Apply a function of two arguments cumulatively to the items of a sequence, -from left to right, so as to reduce the sequence to a single value. -For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates -((((1+2)+3)+4)+5). If initial is present, it is placed before the items -of the sequence in the calculation, and serves as a default when the -sequence is empty.""" - iterator = iter(sequence) - if initial is sentinel: - try: - initial = next(iterator) - except StopIteration: - raise TypeError("reduce() of empty sequence with no initial value") - result = initial - for item in iterator: - result = func(result, item) - return result - - -class partial(object): - """ - partial(func, *args, **keywords) - new function with partial application - of the given arguments and keywords. - """ - - __slots__ = ('_func', '_args', '_keywords', '__dict__') - __module__ = 'functools' # instead of '_functools' - - def __init__(*args, **keywords): - if len(args) < 2: - raise TypeError('__init__() takes at least 2 arguments (%d given)' - % len(args)) - self, func, args = args[0], args[1], args[2:] - if not callable(func): - raise TypeError("the first argument must be callable") - if isinstance(func, partial): - args = func._args + args - tmpkw = func._keywords.copy() - tmpkw.update(keywords) - keywords = tmpkw - del tmpkw - func = func._func - self._func = func - self._args = args - self._keywords = keywords - - def __delattr__(self, key): - if key == '__dict__': - raise TypeError("a partial object's dictionary may not be deleted") - object.__delattr__(self, key) - - @property - def func(self): - return self._func - - @property - def args(self): - return self._args - - @property - def keywords(self): - return self._keywords - - def __call__(self, *fargs, **fkeywords): - if self._keywords: - fkeywords = dict(self._keywords, **fkeywords) - return self._func(*(self._args + fargs), **fkeywords) - - @_recursive_repr() - def __repr__(self): - cls = type(self) - if cls is partial: - name = 'functools.partial' - else: - name = cls.__name__ - tmp = [repr(self.func)] - for arg in self.args: - tmp.append(repr(arg)) - if self.keywords: - for k, v in self.keywords.items(): - tmp.append("{}={!r}".format(k, v)) - return "{}({})".format(name, ', '.join(tmp)) - - def __reduce__(self): - d = dict((k, v) for k, v in self.__dict__.items() if k not in - ('_func', '_args', '_keywords')) - if len(d) == 0: - d = None - return (type(self), (self._func,), - (self._func, self._args, self._keywords, d)) - - def __setstate__(self, state): - if not isinstance(state, tuple) or len(state) != 4: - raise TypeError("invalid partial state") - - func, args, keywords, d = state - - if (not callable(func) or not isinstance(args, tuple) or - (keywords is not None and not isinstance(keywords, dict))): - raise TypeError("invalid partial state") - - self._func = func - self._args = tuple(args) - - if keywords is None: - keywords = {} - elif type(keywords) is not dict: - keywords = dict(keywords) - self._keywords = keywords - - if d is None: - self.__dict__.clear() - else: - self.__dict__.update(d) - - def __class_getitem__(self, item): - return GenericAlias(self, item) - - -@builtinify -def cmp_to_key(mycmp): - """Convert a cmp= function into a key= function""" - class K(object): - __slots__ = ['obj'] - def __init__(self, obj): - self.obj = obj - def __lt__(self, other): - return mycmp(self.obj, other.obj) < 0 - def __gt__(self, other): - return mycmp(self.obj, other.obj) > 0 - def __eq__(self, other): - return mycmp(self.obj, other.obj) == 0 - def __le__(self, other): - return mycmp(self.obj, other.obj) <= 0 - def __ge__(self, other): - return mycmp(self.obj, other.obj) >= 0 - __hash__ = None - return K |