Skip to content

Preserve inferred ParamSpecs in second-pass generic inference - #22519

Closed
zhengdaodeguL wants to merge 1 commit into
python:masterfrom
zhengdaodeguL:fix/to-thread-optional-params-22515
Closed

zhengdaodeguL wants to merge 1 commit into
python:masterfrom
zhengdaodeguL:fix/to-thread-optional-params-22515

Conversation

@zhengdaodeguL

Copy link
Copy Markdown

Fixes #22515

In ExpressionChecker.infer_function_type_arguments_both_sides() (mypy/checkexpr.py, added by #22394): when a generic ParamSpec call such as asyncio.to_thread(...) is nested inside another call supplying outer type context (create_task(...)), the second inference pass generated constraints against the original uninferred Callable[P, T], losing the parameter shape established by the first pass. Overload matching then picked typeshed's first shutil.rmtree overload (which requires onerror), and constraint filtering replaced the correct argument-shape constraint — producing the spurious Missing positional argument "onerror" in call to "to_thread" [call-arg]. The intermediate-variable form uses the normal inference path and was unaffected.

Fix: when the callee has ParamSpec type variables, apply the already-solid inferred ParamSpec arguments to the callee type before the second infer_function_type_arguments call (ordinary type variables stay free to be refined), remap formals to actuals via map_actuals_to_formals, and merge the newly inferred arguments back over the first-pass ones.

Verification:

  • Repro from the issue: before, repro_inline.py:6: error: Missing positional argument "onerror" in call to "to_thread" [call-arg]; after, Success: no issues found. Both forms pass on the parent of the regressing commit, confirming the regression.
  • New test testTwoSidedInferenceWithParamSpecOverload: 1 failed before, 1 passed after; paramspec + inference-context suites: 237 passed.
  • Full pytest mypy/test/testcheck.py: 8,320 passed, 27 skipped, 7 xfailed; mypy self-check on checkexpr.py, Black, Ruff clean.

Not verified locally (left to CI): Python 3.13 itself was unavailable (ran 3.12.3 targeting 3.13); full repo-wide suite not run, only testcheck.py plus targeted suites.

When a generic call with a ParamSpec (e.g. asyncio.to_thread) is nested
inside another call supplying outer type context, the two-sided inference
added in python#22394 generated second-pass constraints against the original
Callable[P, T], losing the parameter shape established by the first pass.
Overload matching could then select an incompatible overload (e.g. the
shutil.rmtree overload requiring onerror), producing a spurious
'Missing positional argument' error for an optional parameter.

Apply the already solid inferred ParamSpec arguments to the callee type
before the second inference pass (leaving ordinary type variables free
to be refined), remap formals to actuals, and merge the newly inferred
arguments back over the first-pass ones.

Fixes python#22515
@github-actions

Copy link
Copy Markdown
Contributor

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

trio (https://github.com/python-trio/trio)
- src/trio/_socket.py:1311: error: Unused "type: ignore" comment  [unused-ignore]
- src/trio/_socket.py:1312: error: Argument 3 to "_nonblocking_helper" of "_SocketType" has incompatible type "*list[object]"; expected "Buffer"  [arg-type]
- src/trio/_socket.py:1312: error: Argument 3 to "_nonblocking_helper" of "_SocketType" has incompatible type "*list[object]"; expected "tuple[Any, ...] | str | Buffer"  [arg-type]

@ilevkivskyi

Copy link
Copy Markdown
Member

Please stop spamming. If you continue, I will submit your profile for an org-level ban.

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.

[regression] Optional argument of callable passed to to_thread missing in call to to_thread

2 participants