Skip to content

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

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

Merged
merged 4 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
args = func.args + args
keywords = {**func.keywords, **keywords}
func = func.func
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,19 @@ def test_nested_optimization(self):
flat = partial(signature, 'asdf', bar=True)
self.assertEqual(signature(nested), signature(flat))

def test_nested_optimization_bug(self):
partial = self.partial
class Builder:
def __call__(self, tag, *children, **attrib):
return (tag, children, attrib)

def __getattr__(self, tag):
return partial(self, tag)

B = Builder()
m = B.m
assert m(1, 2, a=2) == ('m', (1, 2), dict(a=2))

def test_nested_partial_with_attribute(self):
# see issue 25137
partial = self.partial
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Bring pure Python implementation ``functools.partial.__new__`` more in line
with the C-implementation by not just always checking for the presence of
the attribute ``'func'`` on the first argument of ``partial``.