Skip to content

Commit 8b1f6bd

Browse files
superbobrycopybara-github
authored andcommitted
Aligned the signature of new_slot with functools.partial.__new__
Specifically, both `cls` and `func` are now positional-only parameters. PiperOrigin-RevId: 830922244
1 parent 9e87d0f commit 8b1f6bd

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

pytype/overlays/functools_overlay.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def __init__(self, ctx: "context.Context", module: str):
4747
self._pytd_new = self.pytd_cls.Lookup("__new__")
4848

4949
def new_slot(
50-
self, node, cls, *args, **kwargs
50+
self, node, cls, func, /, *args, **kwargs
5151
) -> tuple[cfg.CFGNode, cfg.Variable]:
5252
# Make sure the call is well typed before binding the partial
5353
new = self.ctx.convert.convert_pytd_function(self._pytd_new)
@@ -56,7 +56,7 @@ def new_slot(
5656
node,
5757
new.to_variable(node),
5858
function.Args(
59-
(cls, *args),
59+
(cls, func, *args),
6060
kwargs,
6161
call_context.starargs,
6262
call_context.starstarargs,
@@ -67,19 +67,19 @@ def new_slot(
6767
type_arg = specialized_obj.get_formal_type_parameter("_T")
6868
[cls] = cls.data
6969
cls = abstract.ParameterizedClass(cls, {"_T": type_arg}, self.ctx)
70-
obj = bind_partial(node, cls, args, kwargs, self.ctx)
70+
obj = bind_partial(node, cls, func, args, kwargs, self.ctx)
7171
return node, obj.to_variable(node)
7272

7373
def get_own_new(self, node, value) -> tuple[cfg.CFGNode, cfg.Variable]:
7474
new = NativeFunction("__new__", self.new_slot, self.ctx)
7575
return node, new.to_variable(node)
7676

7777

78-
def bind_partial(node, cls, args, kwargs, ctx) -> BoundPartial:
78+
def bind_partial(node, cls, func, args, kwargs, ctx) -> BoundPartial:
7979
del node # Unused.
8080
obj = BoundPartial(ctx, cls)
81-
obj.underlying = args[0]
82-
obj.args = args[1:]
81+
obj.underlying = func
82+
obj.args = args
8383
obj.kwargs = kwargs
8484
obj.starargs = call_context.starargs
8585
obj.starstarargs = call_context.starstarargs

pytype/tests/test_functions1.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,17 @@ def f(a, b=None):
10791079
partial_f(0)
10801080
""")
10811081

1082+
def test_functools_partial_cls(self):
1083+
# This is a smoke test that **kwargs do not interfere with the receiver
1084+
# in partial.__new__.
1085+
self.Check("""
1086+
import functools
1087+
def f(cls):
1088+
return cls
1089+
partial_f = functools.partial(f, cls=int)
1090+
partial_f()
1091+
""")
1092+
10821093
def test_functools_partial_starstar(self):
10831094
self.Check("""
10841095
import functools
@@ -1180,7 +1191,7 @@ def __init__(self, a, b=None):
11801191
def test_functools_partial_bad_call(self):
11811192
errors = self.CheckWithErrors("""
11821193
import functools
1183-
functools.partial() # missing-parameter
1194+
functools.partial() # wrong-arg-count
11841195
functools.partial(42) # wrong-arg-types[e]
11851196
""")
11861197
self.assertErrorRegexes(errors, {"e": r"Callable.*int"})

0 commit comments

Comments
 (0)