Skip to content

Handle functools.Placeholder in partial#21399

Open
MestreY0d4-Uninter wants to merge 1 commit intopython:masterfrom
MestreY0d4-Uninter:fix-functools-placeholder
Open

Handle functools.Placeholder in partial#21399
MestreY0d4-Uninter wants to merge 1 commit intopython:masterfrom
MestreY0d4-Uninter:fix-functools-placeholder

Conversation

@MestreY0d4-Uninter
Copy link
Copy Markdown

Fixes #21313.

Summary

This updates the functools.partial plugin to treat functools.Placeholder as an unbound positional slot instead of a normal bound argument.

The generated partial signature now keeps placeholder-backed parameters available for later calls, while still using non-placeholder arguments for type inference.

Test plan

  • PYTHONPATH=. uv run --no-project --isolated --with-requirements test-requirements.txt python -m pytest -q mypy/test/testcheck.py::TypeCheckSuite::check-functools.test
  • PYTHONPATH=. uv run --no-project --isolated --with-requirements test-requirements.txt python -m mypy --config-file mypy_self_check.ini mypy/plugins/functools.py

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 2, 2026

Diff from mypy_primer, showing the effect of this PR on open source code:

trio (https://github.com/python-trio/trio)
+ src/trio/_tests/test_threads.py:881: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
+ https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
+ Please report a bug at https://github.com/python/mypy/issues
+ version: 2.1.0+dev.32e79425cb79ef91dfcdfdc2f765454bd386695b
+ src/trio/_tests/test_threads.py:881: note: use --pdb to drop into pdb
+ Traceback (most recent call last):
+   File "", line 6, in <module>
+     sys.exit(console_entry())
+   File "/__main__.py", line 16, in console_entry
+     main()
+   File "/main.py", line 154, in main
+     res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
+   File "/main.py", line 244, in run_build
+     res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
+   File "/build.py", line 422, in build
+     result = build_inner(
+   File "/build.py", line 537, in build_inner
+     graph = dispatch(sources, manager, stdout, connect_threads)
+   File "/build.py", line 4150, in dispatch
+     process_graph(graph, manager)
+   File "/build.py", line 4618, in process_graph
+     done, still_working, results = manager.wait_for_done(graph)
+   File "/build.py", line 1492, in wait_for_done
+     process_stale_scc(graph, next_scc, self)
+   File "/build.py", line 4792, in process_stale_scc
+     graph[id].type_check_first_pass()
+   File "/build.py", line 3414, in type_check_first_pass
+     self.type_checker().check_first_pass(recurse_into_functions=recurse_into_functions)
+   File "/checker.py", line 618, in check_first_pass
+     self.accept(d)
+   File "/checker.py", line 765, in accept
+     stmt.accept(self)
+   File "/nodes.py", line 1159, in accept
+     return visitor.visit_func_def(self)
+   File "/checker.py", line 1332, in visit_func_def
+     self.visit_func_def_impl(defn)
+   File "/checker.py", line 1336, in visit_func_def_impl
+     self.check_func_item(defn, name=defn.name)
+   File "/checker.py", line 1369, in check_func_item
+     self.check_func_def(defn, typ, name, allow_empty)
+   File "/checker.py", line 1551, in check_func_def
+     self.accept(item.body)
+   File "/checker.py", line 765, in accept
+     stmt.accept(self)
+   File "/nodes.py", line 1816, in accept
+     return visitor.visit_block(self)
+   File "/checker.py", line 3275, in visit_block
+     self.accept(s)
+   File "/checker.py", line 765, in accept
+     stmt.accept(self)
+   File "/nodes.py", line 1159, in accept
+     return visitor.visit_func_def(self)
+   File "/checker.py", line 1332, in visit_func_def
+     self.visit_func_def_impl(defn)
+   File "/checker.py", line 1336, in visit_func_def_impl
+     self.check_func_item(defn, name=defn.name)
+   File "/checker.py", line 1369, in check_func_item
+     self.check_func_def(defn, typ, name, allow_empty)
+   File "/checker.py", line 1551, in check_func_def
+     self.accept(item.body)
+   File "/checker.py", line 765, in accept
+     stmt.accept(self)
+   File "/nodes.py", line 1816, in accept
+     return visitor.visit_block(self)
+   File "/checker.py", line 3275, in visit_block
+     self.accept(s)
+   File "/checker.py", line 765, in accept
+     stmt.accept(self)
+   File "/nodes.py", line 2146, in accept
+     return visitor.visit_try_stmt(self)
+   File "/checker.py", line 5433, in visit_try_stmt
+     self.accept(s.finally_body)
+   File "/checker.py", line 765, in accept
+     stmt.accept(self)
+   File "/nodes.py", line 1816, in accept
+     return visitor.visit_block(self)
+   File "/checker.py", line 3275, in visit_block
+     self.accept(s)
+   File "/checker.py", line 765, in accept
+     stmt.accept(self)
+   File "/nodes.py", line 2179, in accept
+     return visitor.visit_with_stmt(self)
+   File "/checker.py", line 5873, in visit_with_stmt
+     self.accept(s.body)
+   File "/checker.py", line 765, in accept
+     stmt.accept(self)
+   File "/nodes.py", line 1816, in accept
+     return visitor.visit_block(self)
+   File "/checker.py", line 3275, in visit_block
+     self.accept(s)
+   File "/checker.py", line 765, in accept
+     stmt.accept(self)
+   File "/nodes.py", line 2146, in accept
+     return visitor.visit_try_stmt(self)
+   File "/checker.py", line 5428, in visit_try_stmt
+     self.visit_try_without_finally(s, try_frame=bool(s.finally_body))
+   File "/checker.py", line 5469, in visit_try_without_finally
+     self.accept(s.body)
+   File "/checker.py", line 765, in accept
+     stmt.accept(self)
+   File "/nodes.py", line 1816, in accept
+     return visitor.visit_block(self)
+   File "/checker.py", line 3275, in visit_block
+     self.accept(s)
+   File "/checker.py", line 765, in accept
+     stmt.accept(self)
+   File "/nodes.py", line 1836, in accept
+     return visitor.visit_expression_stmt(self)
+   File "/checker.py", line 5139, in visit_expression_stmt
+     expr_type = self.expr_checker.accept(s.expr, allow_none_return=True, always_allow_any=True)
+   File "/checkexpr.py", line 6132, in accept
+     typ = self.visit_await_expr(node, allow_none_return=True)
+   File "/checkexpr.py", line 6305, in visit_await_expr
+     actual_type = get_proper_type(self.accept(e.expr, expected_type))
+   File "/checkexpr.py", line 6183, in accept
+     typ = self.accept_maybe_cache(node, type_context=type_context)
+   File "/checkexpr.py", line 6218, in accept_maybe_cache
+     typ = node.accept(self)
+   File "/nodes.py", line 2539, in accept
+     return visitor.visit_call_expr(self)
+   File "/checkexpr.py", line 502, in visit_call_expr
+     return self.visit_call_expr_inner(e, allow_none_return=allow_none_return)
+   File "/checkexpr.py", line 633, in visit_call_expr_inner
+     ret_type = self.check_call_expr_with_callee_type(
+   File "/checkexpr.py", line 1483, in check_call_expr_with_callee_type
+     ret_type, callee_type = self.check_call(
+   File "/checkexpr.py", line 1576, in check_call
+     return self.check_callable_call(
+   File "/checkexpr.py", line 1755, in check_callable_call
+     callee = self.infer_function_type_arguments(
+   File "/checkexpr.py", line 2120, in infer_function_type_arguments
+     arg_types = self.infer_arg_types_in_context(
+   File "/checkexpr.py", line 2006, in infer_arg_types_in_context
+     res.append(self.accept(arg, ctx))
+   File "/checkexpr.py", line 6183, in accept
+     typ = self.accept_maybe_cache(node, type_context=type_context)
+   File "/checkexpr.py", line 6218, in accept_maybe_cache
+     typ = node.accept(self)
+   File "/nodes.py", line 2539, in accept
+     return visitor.visit_call_expr(self)
+            ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
+   File "/checkexpr.py", line 502, in visit_call_expr
+     return self.visit_call_expr_inner(e, allow_none_return=allow_none_return)
+            ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   File "/checkexpr.py", line 633, in visit_call_expr_inner
+     ret_type = self.check_call_expr_with_callee_type(
+         callee_type, e, fullname, object_type, member
+     )
+   File "/checkexpr.py", line 1483, in check_call_expr_with_callee_type
+     ret_type, callee_type = self.check_call(
+                             ~~~~~~~~~~~~~~~^
+         callee_type,
+         ^^^^^^^^^^^^
+     ...<6 lines>...
+         object_type=object_type,
+         ^^^^^^^^^^^^^^^^^^^^^^^^
+     )
+     ^
+   File "/checkexpr.py", line 1576, in check_call
+     return self.check_callable_call(
+            ~~~~~~~~~~~~~~~~~~~~~~~~^
+         callee,
+         ^^^^^^^
+     ...<6 lines>...
+         object_type,
+         ^^^^^^^^^^^^
+     )
+     ^
+   File "/checkexpr.py", line 1859, in check_callable_call
+     new_ret_type = self.apply_function_plugin(
+         callee,
+     ...<7 lines>...
+         context,
+     )
+   File "/checkexpr.py", line 1257, in apply_function_plugin
+     return callback(
+         FunctionContext(
+     ...<8 lines>...
+         )
+     )
+   File "/plugins/functools.py", line 159, in partial_new_callback
+     return handle_partial_with_callee(ctx, callee=ctx.arg_types[0][0])
+   File "/plugins/functools.py", line 307, in handle_partial_with_callee
+     inferred_args = infer_type_arguments(
+         fn_type.variables, fn_type.arg_types[i], bound.arg_types[i]
+     )
+   File "/infer.py", line 76, in infer_type_arguments
+     constraints = infer_constraints(template, actual, SUPERTYPE_OF if is_supertype else SUBTYPE_OF)
+   File "/constraints.py", line 318, in infer_constraints
+     return _infer_constraints(template, actual, direction, skip_neg_op)
+   File "/constraints.py", line 427, in _infer_constraints
+     return template.accept(ConstraintBuilderVisitor(actual, direction, skip_neg_op))
+            ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   File "/types.py", line 1237, in accept
+     return visitor.visit_unpack_type(self)
+            ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
+   File "/constraints.py", line 716, in visit_unpack_type
+     raise RuntimeError("Mypy bug: unpack should be handled at a higher level.")
+ RuntimeError: Mypy bug: unpack should be handled at a higher level.
+ 

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MyPy treats Placeholder as _PlaceholderType

1 participant