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 from17 commits
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
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -358,6 +358,18 @@ The :mod:`functools` module defines the following functions: | ||
| >>> basetwo('10010') | ||
| 18 | ||
| If ``Placeholder`` sentinels are present in *args*, they will be filled first | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page.
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| when :func:`partial` is called. This allows custom selection of positional arguments | ||
| to be pre-filled when constructing :ref:`partial object<partial-objects>`. | ||
| If ``Placeholder`` sentinels are used, all of them must be filled at call time.: | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page.
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| >>> 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, 'world!') | ||
| >>> say_to_world('Hello') | ||
| Hello world! | ||
| .. versionchanged:: 3.14 | ||
| Support for ``Placeholder`` in *args* | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page.
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| .. class:: partialmethod(func, /, *args, **keywords) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -273,6 +273,11 @@ def reduce(function, sequence, initial=_initial_missing): | ||
| ### partial() argument application | ||
| ################################################################################ | ||
| class Placeholder: | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| """placeholder for partial arguments""" | ||
| # Purely functional, no descriptor behaviour | ||
| class partial: | ||
| """New function with partial application of the given arguments | ||
| @@ -285,8 +290,33 @@ def __new__(cls, func, /, *args, **keywords): | ||
| if not callable(func): | ||
| raise TypeError("the first argument must be callable") | ||
| np = 0 | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| nargs = len(args) | ||
| if args: | ||
| while nargs and args[nargs-1] is Placeholder: | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| nargs -= 1 | ||
| args = args[:nargs] | ||
| np = args.count(Placeholder) | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| if isinstance(func, partial): | ||
| pargs = func.args | ||
| pnp = func.np | ||
| if pnp and args: | ||
| all_args = list(pargs) | ||
| nargs = len(args) | ||
| j, pos = 0, 0 | ||
| end = nargs if nargs < pnp else pnp | ||
| while j < end: | ||
| pos = all_args.index(Placeholder, pos) | ||
| all_args[pos] = args[j] | ||
| j += 1 | ||
| pos += 1 | ||
| if pnp < nargs: | ||
| all_args.extend(args[pnp:]) | ||
| np += pnp - end | ||
| args = tuple(all_args) | ||
| else: | ||
| np += pnp | ||
| args = func.args + args | ||
| keywords = {**func.keywords, **keywords} | ||
| func = func.func | ||
| @@ -295,11 +325,27 @@ def __new__(cls, func, /, *args, **keywords): | ||
| self.func = func | ||
| self.args = args | ||
| self.keywords = keywords | ||
| self.np = np | ||
| return self | ||
| def __call__(self, /, *args, **keywords): | ||
| if not hasattr(self, 'np'): | ||
| self.np = self.args.count(Placeholder) | ||
| if np := self.np: | ||
| if len(args) < np: | ||
| raise TypeError("unfilled placeholders in 'partial' call") | ||
| f_args = list(self.args) | ||
| j, pos = 0, 0 | ||
| while j < np: | ||
| pos = f_args.index(Placeholder, pos) | ||
| f_args[pos] = args[j] | ||
| j += 1 | ||
| pos += 1 | ||
| keywords = {**self.keywords, **keywords} | ||
| return self.func(*f_args, *args[np:], **keywords) | ||
| else: | ||
| keywords = {**self.keywords, **keywords} | ||
| return self.func(*self.args, *args, **keywords) | ||
| @recursive_repr() | ||
| def __repr__(self): | ||
| @@ -340,7 +386,7 @@ def __setstate__(self, state): | ||
| self.keywords = kwds | ||
| try: | ||
| from _functools import partial, Placeholder | ||
| except ImportError: | ||
| pass | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -210,6 +210,43 @@ def foo(bar): | ||
| p2.new_attr = 'spam' | ||
| self.assertEqual(p2.new_attr, 'spam') | ||
| def test_placeholders_trailing_trim(self): | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| PH = self.module.Placeholder | ||
| for args in [(PH,), (PH, 0), (0, PH), (0, PH, 1, PH, PH, PH)]: | ||
| expected, n = tuple(args), len(args) | ||
| while n and args[n-1] is PH: | ||
| n -= 1 | ||
| expected = expected[:n] | ||
| p = self.partial(capture, *args) | ||
| self.assertTrue(p.args == expected) | ||
| def test_placeholders(self): | ||
| PH = self.module.Placeholder | ||
| # 1 Placeholder | ||
| args = (PH, 0) | ||
| p = self.partial(capture, *args) | ||
| got, empty = p('x') | ||
| self.assertTrue(('x', 0) == got and empty == {}) | ||
serhiy-storchaka marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| # 2 Placeholders | ||
| args = (PH, 0, PH, 1) | ||
| p = self.partial(capture, *args) | ||
| with self.assertRaises(TypeError): | ||
| got, empty = p('x') | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| got, empty = p('x', 'y') | ||
| expected = ('x', 0, 'y', 1) | ||
| self.assertTrue(expected == got and empty == {}) | ||
| ||
| def test_placeholders_optimization(self): | ||
| PH = self.module.Placeholder | ||
| p = self.partial(capture, PH, 0) | ||
| p2 = self.partial(p, PH, 1, 2, 3) | ||
| expected = (PH, 0, 1, 2, 3) | ||
| self.assertTrue(expected == p2.args) | ||
serhiy-storchaka marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| p3 = self.partial(p2, -1, 4) | ||
| got, empty = p3(5) | ||
| expected = (-1, 0, 1, 2, 3, 4, 5) | ||
| self.assertTrue(expected == got and empty == {}) | ||
| def test_repr(self): | ||
| args = (object(), object()) | ||
| args_repr = ', '.join(repr(a) for a in args) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| ``partial`` of ``functools`` module now supports placeholders for positional | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| arguments. | ||
Uh oh!
There was an error while loading.Please reload this page.