Skip to content
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

GH-100242: bring functools.py partial implementation more in line with C code #100244

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

cfbolz
Copy link
Contributor

@cfbolz cfbolz commented Dec 14, 2022

in partial.__new__, before checking for the existence of the attribute 'func', first check whether the argument is an instance of partial.

cfbolz added 2 commits Dec 14, 2022
in partial.__new__, before checking for the existence of the attribute
'func', first check whether the argument is an instance of partial.
@netlify
Copy link

netlify bot commented Dec 14, 2022

Deploy Preview for python-cpython-preview canceled.

Name Link
🔨 Latest commit 97b9966
🔍 Latest deploy log https://app.netlify.com/sites/python-cpython-preview/deploys/6399fa45bbd72a0008fec2cf

Lib/functools.py Outdated
@@ -284,7 +284,7 @@ def __new__(cls, func, /, *args, **keywords):
if not callable(func):
raise TypeError("the first argument must be callable")

if hasattr(func, "func"):
if isinstance(func, partial) and hasattr(func, "func"):
Copy link
Member

@AlexWaygood AlexWaygood Dec 14, 2022

Choose a reason for hiding this comment

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

Will an instance of partial ever not have a func attribute? Could this be simplified to just an isinstance() check?

Suggested change
if isinstance(func, partial) and hasattr(func, "func"):
if isinstance(func, partial):

Copy link
Contributor Author

@cfbolz cfbolz Dec 14, 2022

Choose a reason for hiding this comment

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

yes, that works for me too. (you can certainly remove the func attribute from a pure python partial instance, but the resulting object is not very useful)

Copy link
Contributor Author

@cfbolz cfbolz Dec 14, 2022

Choose a reason for hiding this comment

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

if we do that, maybe it's also a good idea to just use an isinstance check in the C implementation as well? then the two approaches are straightforwardly identical.

Copy link
Member

@AlexWaygood AlexWaygood Dec 14, 2022

Choose a reason for hiding this comment

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

you can certainly remove the func attribute from a pure python partial instance, but the resulting object is not very useful

Yeah, if people are deleting the func attribute from partial objects, I have a lot of questions about their broader approach to programming in general :)

maybe it's also a good idea to just use an isinstance check in the C implementation as well?

I'm not qualified to comment on the C implementation, so I'll defer to you and @rhettinger on that question :)

Copy link
Contributor Author

@cfbolz cfbolz Dec 14, 2022

Choose a reason for hiding this comment

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

Yeah, if people are deleting the func attribute from partial objects, I have a lot of questions about their broader approach to programming in general :)

this argument convinced me ;-). I just went with the isinstance check in both places.

@rhettinger rhettinger self-assigned this Dec 15, 2022
@rhettinger
Copy link
Contributor

rhettinger commented Dec 15, 2022

As a first approximation this looks good. In the next couple of days, I can devote some time to give it more thought.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants