cpython icon indicating copy to clipboard operation
cpython copied to clipboard

gh-121676: Raise a ``DeprecationWarning`` if the Python implementation of ``functools.reduce`` is called with a keyword args

Open Eclips4 opened this issue 1 year ago • 11 comments

  • Issue: gh-121676

Eclips4 avatar Jul 13 '24 10:07 Eclips4

ISTM that adding an argument restriction to an already published function is a breaking change.

rhettinger avatar Jul 15 '24 03:07 rhettinger

I see how @Eclips4 would conclude that the likelihood of users running the Python version of functools.reduce is low. However, I have to agree with Nikita and Raymond here. It is unknowable whether users had or had not depended on keyword arguments here, especially after the function existing for so long. A lot of code isn't open.

In general, it might be tempting to treat a case like this one as a special case and relax our usual standards, but I believe it's not worth doing: we're not fixing a crippling problem, and we're not unblocking users to perform a legitimate operation. This change is a cleanup to improve consistency, it's not urgent.

Moreover, the function isn't actually documented as positional-only, so we cannot claim that the current behavior of the pure Python implementation was against stated intent.

Therefore, the responsible thing to do here would be to deprecate keyword argument use (through an awkward *args, **kwargs wrapper that would discover keyword usage and warn). Best case scenario, we might ask Thomas if he'd be okay with this new deprecation would be okay for 3.13 still, even though it's officially too late for that. But as Kirill says, the blast radius is limited as the function is shadowed by the C implementation for the vast majority of users.

ambv avatar Jul 15 '24 18:07 ambv

Thanks Łukasz for sharing your thoughts!

Moreover, the function isn't actually documented as positional-only, so we cannot claim that the current behavior of the pure Python implementation was against stated intent.

Actually it's documented as positional-only, but only in 3.13+ branches: https://docs.python.org/3.13/library/functools.html#functools.reduce

I absolutely agree with you that if we can deprecate it for 3.13, that would be fine. If not, that would also be fine (should we then revert the change to the docs mentioned above?). cc @Yhg1s

Eclips4 avatar Jul 15 '24 19:07 Eclips4

No, I don't think this should be done in 3.13. The potential impact is low, but so is the benefit. It's fine to leave it documented as positional-only, but changes in behaviour (if warranted at all, and following the normal deprecation process) should go into 3.14 instead

Yhg1s avatar Jul 15 '24 19:07 Yhg1s

@hugovk as a release manager for 3.14, what do you think about this PR? :)

Eclips4 avatar Sep 27 '24 22:09 Eclips4

I don't find any results in the top 8k PyPI projects (searching for "functools.reduce.*(function=|sequence=)" or " reduce\(.*(function=|sequence=)").


FYI pypy accepts keyword arguments:

Is there a good reason not to go the other way, and allow keyword arguments for the C implementation?

hugovk avatar Sep 28 '24 00:09 hugovk

Is there a good reason not to go the other way, and allow keyword arguments for the C implementation?

In principle, this adds some overhead. But from my tests on cmath module (see this) - it's something like 2-3%. Perhaps, for reduce this will be even less important for most workloads.

skirpichev avatar Sep 28 '24 02:09 skirpichev

I don't find any results in the top 8k PyPI projects (searching for "functools.reduce.*(function=|sequence=)" or " reduce\(.*(function=|sequence=)").

FYI pypy accepts keyword arguments:

Is there a good reason not to go the other way, and allow keyword arguments for the C implementation?

PyPy accepts keyword arguments because it reuses the CPython pure python implementation of functools, so this is not done purposely by the PyPy team. I guess there is no reason not to extend the reduce signature to support keyword arguments.

Eclips4 avatar Sep 28 '24 08:09 Eclips4

PyPy accepts keyword arguments because it reuses the CPython pure python implementation of functools, so this is not done purposely by the PyPy team. I guess there is no reason not to extend the reduce signature to support keyword arguments.

That maybe true in this specific case, but more generally, PyPy's RPython-implemented functions have keyword arguments for everything by default. We need to do extra work to not expose them, and in general we would prefer everything to have keyword arguments.

cfbolz avatar Sep 28 '24 09:09 cfbolz

I'm in favor of making keyword args supported for _functools.reduce. If others don't complain about it, I'll rework this PR!

Eclips4 avatar Sep 28 '24 12:09 Eclips4

I missed this. Is there an agreement on this? I am not sure what to do about my PR.

sayandipdutta avatar Oct 24 '24 08:10 sayandipdutta

What's the status of this PR? I'm now confused :-(

vstinner avatar Oct 29 '24 16:10 vstinner

What's the status of this PR? I'm now confused :-(

I'm waiting for PR #125917 to be merged, so the initial argument would be pos-and-keyword. Therefore, I will change this PR to raise a DeprecationWarning if the Python version of functools.reduce is called with a function or sequence as a keyword argument. What do you think?

Eclips4 avatar Oct 29 '24 18:10 Eclips4

Now https://github.com/python/cpython/pull/125917 is merged. I think that @vstinner review should be dismissed as this pr reworked to allow initial kwarg.

skirpichev avatar Nov 14 '24 12:11 skirpichev

Thanks to all who participated! ❤️

Eclips4 avatar Jan 01 '25 11:01 Eclips4