Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
gh-119127: functools.partial placeholders#119827
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
ee7333c8bcc462c67c9b4680d9009591ff5067e9388af20b3607a0b1f55801e58941453722e07a79c2af12aaa7292c767b496a9d238d9c11707b95714b38ca32bca198576493a3fd2d608529936fea348caec6e8115b8c53f5f00b202c9292c16d38400ff558ccc38fe7c82c7c9b7ef3e59d7117bfc5917957a978aaee6afe8e0ad00dd80ed352cfa9038ed549b8c71bc1fdbd30672211185510266b4fadd58a125971fbb9033650d31e5d1a3d39b09e4c5df16f12f882dd600f9cb653d255524404044e800217b38ee45011f47db3c872bdfd16189a6c6ef21c8d73ea8bd3ae70e47ed2eacf5ef78d8d30a8640e6e3d28266c305d14bf68cee642d58d6c28e8744bcbb8964704881ae6c3ad7d95e5d484File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -348,7 +348,7 @@ The :mod:`functools` module defines the following functions: | ||
| The :func:`partial` is used for partial function application which "freezes" | ||
| some portion of a function's arguments and/or keywords resulting in a new object | ||
| with a simplified signature. For example, :func:`partial` can be used to create | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| a callable that behaves like the :func:`int` function where the *base* argument | ||
| defaults to two: | ||
| @@ -362,7 +362,7 @@ The :mod:`functools` module defines the following functions: | ||
| when :func:`partial` is called. This allows custom selection of positional arguments | ||
| to be pre-filled when constructing a :ref:`partial object <partial-objects>`. | ||
| If :data:`!Placeholder` sentinels arepresent, all of them must be filled at call time: | ||
| >>> from functools import partial, Placeholder | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page.
picnixz marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| >>> say_to_world = partial(print, Placeholder, Placeholder, "world!") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -301,9 +301,9 @@ def __reduce__(self): | ||
| Placeholder = _PlaceholderType() | ||
| def _partial_prepare_merger(args): | ||
| if not args: | ||
| return 0, None | ||
| nargs = len(args) | ||
| order = [] | ||
| j = nargs | ||
| for i, a in enumerate(args): | ||
| @@ -324,9 +324,9 @@ def _partial_new(cls, func, /, *args, **keywords): | ||
| else: | ||
| base_cls = partialmethod | ||
| # func could be a descriptor like classmethod which isn't callable | ||
| if not callable(func) and not hasattr(func, "__get__"): | ||
| raise TypeError(f"the first argument {func!r} must be a callable " | ||
| "or a descriptor") | ||
| if args and args[-1] is Placeholder: | ||
| raise TypeError("trailing Placeholders are not allowed") | ||
dg-pb marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| if isinstance(func, base_cls): | ||
| @@ -459,7 +459,7 @@ def _method(cls_or_self, /, *args, **keywords): | ||
| args = args[phcount:] | ||
| except IndexError: | ||
| raise TypeError("missing positional arguments " | ||
| "in 'partialmethod' call; expected " | ||
| f"at least {phcount}, got {len(args)}") | ||
| else: | ||
| pto_args = self.args | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -364,14 +364,19 @@ def test_setstate(self): | ||
| f = self.partial(signature) | ||
| f.__setstate__((capture, (PH, 1), dict(a=10), dict(attr=[]))) | ||
| self.assertEqual(signature(f), (capture, (PH, 1), dict(a=10), dict(attr=[]))) | ||
| with self.assertRaises(TypeError) as cm: | ||
| self.assertEqual(f(), (PH, 1), dict(a=10)) | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| expected = ("missing positional arguments in 'partial' call; " | ||
| "expected at least 1, got 0") | ||
| self.assertEqual(cm.exception.args[0], expected) | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| self.assertEqual(f(2), ((2, 1), dict(a=10))) | ||
| #Trailing Placeholder error | ||
| f = self.partial(signature) | ||
| with self.assertRaises(TypeError) as cm: | ||
| f.__setstate__((capture, (1, PH), dict(a=10), dict(attr=[]))) | ||
| expected = "trailing Placeholders are not allowed" | ||
| self.assertEqual(cm.exception.args[0], expected) | ||
| def test_setstate_errors(self): | ||
| f = self.partial(signature) | ||
| @@ -556,10 +561,16 @@ class TestPartialPySubclass(TestPartialPy): | ||
| partial = PyPartialSubclass | ||
| def test_subclass_optimization(self): | ||
| # `partial` input to `partial` subclass | ||
| p = py_functools.partial(min, 2) | ||
dg-pb marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| p2 = self.partial(p, 1) | ||
| self.assertIs(p2.func, min) | ||
| self.assertEqual(p2(0), 0) | ||
| # `partial` subclass input to `partial` subclass | ||
| p = self.partial(min, 2) | ||
| p2 = self.partial(p, 1) | ||
| self.assertIs(p2.func, min) | ||
| self.assertEqual(p2(0), 0) | ||
| class TestPartialMethod(unittest.TestCase): | ||
| @@ -702,10 +713,16 @@ def f(a, b, /): | ||
| def test_subclass_optimization(self): | ||
| class PartialMethodSubclass(functools.partialmethod): | ||
| pass | ||
| # `partialmethod` input to `partialmethod` subclass | ||
| p = functools.partialmethod(min, 2) | ||
| p2 = PartialMethodSubclass(p, 1) | ||
| self.assertIs(p2.func, min) | ||
| self.assertEqual(p2.__get__(0)(), 0) | ||
| # `partialmethod` subclass input to `partialmethod` subclass | ||
| p = PartialMethodSubclass(min, 2) | ||
| p2 = PartialMethodSubclass(p, 1) | ||
| self.assertIs(p2.func, min) | ||
| self.assertEqual(p2.__get__(0)(), 0) | ||
| class TestUpdateWrapper(unittest.TestCase): | ||