Repository navigation
Preserve inferred ParamSpecs in second-pass generic inference - #22519
Closed
zhengdaodeguL wants to merge 1 commit into
Closed
zhengdaodeguL wants to merge 1 commit into
zhengdaodeguL wants to merge 1 commit into
Conversation
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
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]
|
Member
|
Please stop spamming. If you continue, I will submit your profile for an org-level ban. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #22515
In
ExpressionChecker.infer_function_type_arguments_both_sides()(mypy/checkexpr.py, added by #22394): when a generic ParamSpec call such asasyncio.to_thread(...)is nested inside another call supplying outer type context (create_task(...)), the second inference pass generated constraints against the original uninferredCallable[P, T], losing the parameter shape established by the first pass. Overload matching then picked typeshed's firstshutil.rmtreeoverload (which requiresonerror), and constraint filtering replaced the correct argument-shape constraint — producing the spuriousMissing 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_argumentscall (ordinary type variables stay free to be refined), remap formals to actuals viamap_actuals_to_formals, and merge the newly inferred arguments back over the first-pass ones.Verification:
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.testTwoSidedInferenceWithParamSpecOverload: 1 failed before, 1 passed after; paramspec + inference-context suites: 237 passed.pytest mypy/test/testcheck.py: 8,320 passed, 27 skipped, 7 xfailed; mypy self-check oncheckexpr.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.pyplus targeted suites.