Repository navigation
Fix #11472: TypeVarTuple escapes method with very nested recursive tuple aliases - #11497
Conversation
The tuple type-var transformer bailed out early (returning the type unchanged) whenever a tuple's container depth exceeded a fixed limit of 10. This was meant to prevent the analyzer from hanging on tuple types that grow without bound during constraint solving. However, legitimate code can produce bounded-but-deeply-nested tuples via recursive type aliases, and bailing out there silently dropped already-solved TypeVar substitutions, causing a solved TypeVar (e.g. OO) to escape into the inferred type and produce a spurious assignment error. Container depth cannot distinguish a legitimately deep tuple from a runaway one: a single substitution of a bounded recursive alias can jump the depth past the limit just as runaway recursion does. The real protection against unbounded tuple growth is already provided by the apply() recursion-count limit and the pending-type-var-scope mechanism (which leaves same-scope recursive type vars unexpanded), so the depth guard is redundant and is removed. Fixes microsoft#11472 Co-authored-by: Copilot <[email protected]>
This comment has been minimized.
This comment has been minimized.
|
Fix looks correct and well-targeted. Two non-blocking suggestions: (1) the removed depth guard was a documented performance safeguard in vendored Pyright core — please confirm the original motivating workload still terminates and consider coordinating upstream; (2) strengthen the test with a reveal_type assertion to pin the inferred type rather than only asserting zero errors. |
Pin the inferred type of the motivating constructor call so the fix's correctness is self-evident and a future regression where the solved "OO" TypeVar leaks back into the inferred type (without necessarily producing an assignment error) is caught. Co-authored-by: Copilot <[email protected]>
This comment has been minimized.
This comment has been minimized.
| return classType; | ||
| } | ||
|
|
||
| if (classType.priv.tupleTypeArgs) { |
There was a problem hiding this comment.
Consider narrowing this guard instead of deleting it. The real defect is that the early-return skipped TypeVar substitution and returned the bare unspecialized classType, letting OO escape. Rather than removing the depth bound entirely, you could short-circuit only when the subtree has no in-scope TypeVars (or keep substituting under a cap, returning Unknown for truncated args). That fixes the escape while preserving parity with the sibling depth guards (maxInferredContainerDepth in tuples.ts, and the guards in typeEvaluator.ts). The hang-regression concern was empirically disproven, so this is not a blocker — but at minimum note in the PR why full deletion is preferred over a bounded bail-out.
There was a problem hiding this comment.
Good call — switched from full deletion to the narrowed/safe-bailout variant in 2664550. The depth guard (maxTupleTypeArgRecursionDepth) is back, but the early return now only fires when the deeply nested tuple has no TypeVars left to substitute:
if (getContainerDepth(classType) > maxTupleTypeArgRecursionDepth && !requiresSpecialization(classType)) {
return classType;
}Since we never bail while type vars remain, OO can no longer escape unsolved at any depth, and the performance safeguard (plus parity with the sibling depth guards) is preserved. The fast path for deep-but-concrete tuples is kept, since requiresSpecialization is false there.
| ); | ||
| }); | ||
|
|
||
| test('Match1', () => { |
There was a problem hiding this comment.
This assert.strictEqual pins a ~250-char fully-expanded nested-tuple reveal_type string. Since the stated intent is only to catch the OO TypeVar leaking back in, this couples the test to exact type-printer output and will break on benign rendering/ordering changes with a near-unreadable diff. Consider asserting the absence of the leak (e.g. !/\bOO\b/.test(message)) or a concrete-shape substring instead of the full expansion. validateResults(..., 0, 0, 1) already guards that the reveal exists.
There was a problem hiding this comment.
Agreed — the full-expansion strictEqual was too brittle. Replaced it in 2664550 with assertions that pin the concrete outer shape and verify the leaked TypeVar is absent:
const revealedType = analysisResults[0].infos[0].message;
assert.ok(
revealedType.startsWith('Type of "inferred" is "ForwardRefParser['),
`Unexpected inferred type: ${revealedType}`
);
assert.ok(!/\bOO\b/.test(revealedType), `"OO" TypeVar escaped into inferred type: ${revealedType}`);validateResults(..., 0, 0, 1) still guards that the reveal exists, and this no longer breaks on benign type-printer/ordering changes while still catching the OO leak regression.
|
Looks fundamentally sound — approving. The central question reviewers converged on is whether deleting this global tuple-depth guard is safe; the hang regression was empirically disproven, so this isn't blocking. To strengthen the change, consider narrowing the guard (substitute-but-cap) rather than deleting it outright, and/or adding a timeout-bounded regression sample for the original infinite-nesting scenario the guard protected. |
|
Looks fundamentally sound — approving. The main thing worth a second look is whether to narrow the depth guard rather than delete it outright (it was originally hang-protection), plus reducing the brittleness of the exact-string reveal_type assertion. Neither is blocking. |
Address review feedback on microsoft#11497: - typeUtils.ts: re-introduce maxTupleTypeArgRecursionDepth but make the bail-out safe by only short-circuiting when the deeply nested tuple has no TypeVars left to substitute. This preserves the performance safeguard (and parity with the sibling depth guards) while still preventing the unsolved TypeVar from escaping at any depth. - typeEvaluator6.test.ts: replace the brittle full nested-tuple reveal_type string match with assertions on the concrete outer shape and the absence of the leaked "OO" TypeVar, so the test no longer couples to exact type-printer output. Co-authored-by: Copilot <[email protected]>
|
Approving. The fix correctly stops the TypeVar from escaping, but the main feedback is that fully deleting the depth guard (vs. narrowing it so substitution still happens) removes a hang-protection bound — worth a brief justification and a hang-shaped regression sample. The exact-string test assertion is also brittle. |
Heejae Chang (heejaechang)
left a comment
There was a problem hiding this comment.
Approved via Review Center.
|
Diff from mypy_primer, showing the effect of this PR on open source code: sympy (https://github.com/sympy/sympy)
- .../projects/sympy/sympy/solvers/ode/nonhomogeneous.py:468:28 - error: Argument of type "Expr | Unknown | None" cannot be assigned to parameter "expr" of type "Expr" in function "make_args"
+ .../projects/sympy/sympy/solvers/ode/nonhomogeneous.py:468:28 - error: Argument of type "Unknown | None" cannot be assigned to parameter "expr" of type "Expr" in function "make_args"
- Type "Expr | Unknown | None" is not assignable to type "Expr"
+ Type "Unknown | None" is not assignable to type "Expr"
- Attribute "pop" is unknown (reportAttributeAccessIssue)
- .../projects/sympy/sympy/solvers/ode/ode.py:610:22 - error: Cannot access attribute "pop" for class "tuple[()]"
- Attribute "pop" is unknown (reportAttributeAccessIssue)
- .../projects/sympy/sympy/solvers/ode/ode.py:610:22 - error: Cannot access attribute "pop" for class "tuple[str, ...]"
- .../projects/sympy/sympy/solvers/ode/ode.py:1579:38 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:1580:38 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:1590:9 - error: No overloads for "update" match the provided arguments (reportCallIssue)
- .../projects/sympy/sympy/solvers/ode/ode.py:1590:12 - error: "update" is not a known attribute of "None" (reportOptionalMemberAccess)
- .../projects/sympy/sympy/solvers/ode/ode.py:1590:19 - error: Argument of type "Unknown | dict[Unknown, Unknown] | None" cannot be assigned to parameter "m" of type "Iterable[tuple[str, Unknown]]" in function "update"
- Type "Unknown | dict[Unknown, Unknown] | None" is not assignable to type "Iterable[tuple[str, Unknown]]"
- "None" is incompatible with protocol "Iterable[tuple[str, Unknown]]"
- "__iter__" is not present (reportArgumentType)
- .../projects/sympy/sympy/solvers/ode/ode.py:1597:43 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:1597:64 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:1603:9 - error: No overloads for "update" match the provided arguments (reportCallIssue)
- .../projects/sympy/sympy/solvers/ode/ode.py:1603:12 - error: "update" is not a known attribute of "None" (reportOptionalMemberAccess)
- .../projects/sympy/sympy/solvers/ode/ode.py:1603:19 - error: Argument of type "Unknown | dict[Unknown, Unknown] | None" cannot be assigned to parameter "m" of type "Iterable[tuple[str, Unknown]]" in function "update"
- Type "Unknown | dict[Unknown, Unknown] | None" is not assignable to type "Iterable[tuple[str, Unknown]]"
- "None" is incompatible with protocol "Iterable[tuple[str, Unknown]]"
- "__iter__" is not present (reportArgumentType)
- .../projects/sympy/sympy/solvers/ode/ode.py:1610:43 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:1610:74 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:1616:9 - error: No overloads for "update" match the provided arguments (reportCallIssue)
- .../projects/sympy/sympy/solvers/ode/ode.py:1616:12 - error: "update" is not a known attribute of "None" (reportOptionalMemberAccess)
- .../projects/sympy/sympy/solvers/ode/ode.py:1616:19 - error: Argument of type "Unknown | dict[Unknown, Unknown] | None" cannot be assigned to parameter "m" of type "Iterable[tuple[str, Unknown]]" in function "update"
- Type "Unknown | dict[Unknown, Unknown] | None" is not assignable to type "Iterable[tuple[str, Unknown]]"
- "None" is incompatible with protocol "Iterable[tuple[str, Unknown]]"
- "__iter__" is not present (reportArgumentType)
- .../projects/sympy/sympy/solvers/ode/ode.py:1623:49 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:1623:70 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
+ .../projects/sympy/sympy/solvers/ode/ode.py:1753:36 - error: Cannot access attribute "lhs" for class "Expr"
+ Attribute "lhs" is unknown (reportAttributeAccessIssue)
+ .../projects/sympy/sympy/solvers/ode/ode.py:1753:55 - error: Cannot access attribute "rhs" for class "Expr"
+ Attribute "rhs" is unknown (reportAttributeAccessIssue)
+ .../projects/sympy/sympy/solvers/ode/ode.py:1754:36 - error: Cannot access attribute "lhs" for class "Expr"
+ Attribute "lhs" is unknown (reportAttributeAccessIssue)
+ .../projects/sympy/sympy/solvers/ode/ode.py:1755:17 - error: No overloads for "__setitem__" match the provided arguments (reportCallIssue)
+ .../projects/sympy/sympy/solvers/ode/ode.py:1755:17 - error: Argument of type "Equality | BooleanFalse | BooleanTrue | Unknown | Expr" cannot be assigned to parameter "value" of type "Equality | BooleanFalse | BooleanTrue" in function "__setitem__"
+ Type "Equality | BooleanFalse | BooleanTrue | Unknown | Expr" is not assignable to type "Equality | BooleanFalse | BooleanTrue"
+ Type "Expr" is not assignable to type "Equality | BooleanFalse | BooleanTrue"
+ "Expr" is not assignable to "Equality"
+ "Expr" is not assignable to "BooleanFalse"
+ "Expr" is not assignable to "BooleanTrue" (reportArgumentType)
- .../projects/sympy/sympy/solvers/ode/ode.py:3442:7 - error: "update" is not a known attribute of "None" (reportOptionalMemberAccess)
- .../projects/sympy/sympy/solvers/ode/ode.py:3442:38 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:3443:7 - error: "update" is not a known attribute of "None" (reportOptionalMemberAccess)
- .../projects/sympy/sympy/solvers/ode/ode.py:3443:38 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:3444:14 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:3445:14 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:3446:14 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:3458:50 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:3459:50 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:3460:50 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:3500:5 - error: No overloads for "update" match the provided arguments (reportCallIssue)
- .../projects/sympy/sympy/solvers/ode/ode.py:3500:7 - error: "update" is not a known attribute of "None" (reportOptionalMemberAccess)
- .../projects/sympy/sympy/solvers/ode/ode.py:3500:14 - error: Argument of type "Unknown | dict[Unknown, Unknown] | None" cannot be assigned to parameter "m" of type "Iterable[tuple[str, Unknown]]" in function "update"
- Type "Unknown | dict[Unknown, Unknown] | None" is not assignable to type "Iterable[tuple[str, Unknown]]"
- "None" is incompatible with protocol "Iterable[tuple[str, Unknown]]"
- "__iter__" is not present (reportArgumentType)
- .../projects/sympy/sympy/solvers/ode/ode.py:3501:18 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:3501:43 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:3502:9 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:3502:16 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:3502:24 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:3502:31 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:3503:9 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:3503:15 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:3503:23 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:3503:30 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:3504:7 - error: "update" is not a known attribute of "None" (reportOptionalMemberAccess)
- .../projects/sympy/sympy/solvers/ode/ode.py:3504:46 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:3504:54 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:3505:9 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:3505:19 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:3505:29 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:3506:10 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
... (truncated 292 lines) ...
|
| // constructing such deep types is expensive). Only do this when there | ||
| // are no type variables left to substitute; bailing out while type | ||
| // variables remain would return the unspecialized class and let those | ||
| // TypeVars "escape" unsolved (see microsoft/pyright#11472). |
There was a problem hiding this comment.
The new comment ends with an inline issue reference (see microsoft/pyright#11472). Issue links (including GitHub shorthand) are prohibited in production source per the repo's issue-reference policy. Remove (see microsoft/pyright#11472) and keep that traceability in the PR/commit metadata instead. The rest of the comment—explaining why the bail-out is gated on !requiresSpecialization—is good and should stay.
| revealedType.startsWith('Type of "inferred" is "ForwardRefParser['), | ||
| `Unexpected inferred type: ${revealedType}` | ||
| ); | ||
| assert.ok(!/\bOO\b/.test(revealedType), `"OO" TypeVar escaped into inferred type: ${revealedType}`); |
There was a problem hiding this comment.
revealedType.startsWith(...) plus !/\bOO\b/.test(revealedType) are partial assertions on deterministic reveal_type output, which the repo's no-partial-asserts test rule prohibits. reveal_type of a statically-typed expression is fully deterministic, so the non-determinism exception does not apply. Capture the full inferred-type string (e.g. via the Jest inspector) and assert it exactly with assert.strictEqual(revealedType, '<full exact string>'). If shape-only assertions are genuinely wanted here, that requires amending the rule, not bypassing it in this test.
|
Core fix looks reasonable (narrowing the depth bail-out with |
|
Code logic looks sound — note that the actual diff narrows the depth guard with |
Description
Fixes a bug where a
TypeVarTuplecould "escape" a method's signature when the receiver's type arguments contained deeply nested recursive tuple type aliases. In these cases, an unsolved TypeVar (e.g.OOfromunpack_then) leaked into the inferred type of an assignment, producing a spurious type error.How you figured out what to do
The repro chained parser combinators (
then/unpack_then) on values whose types involved recursive tuple aliases (Whitespace,Implementations,BlockItem). Tracing the failure showed thatTypeVarTransformerbailed out early when transforming tuple types whose container depth exceeded a hard-coded limit (maxTupleTypeArgRecursionDepth = 10). When that early-return fired mid-transformation, the method's TypeVars were never substituted, so they escaped into the result.Implementation
Removed the
maxTupleTypeArgRecursionDepthguard intypeUtils.ts:maxTupleTypeArgRecursionDepthconstant.getContainerDepth(classType) > maxTupleTypeArgRecursionDepthearly-return inTypeVarTransformer's tuple handling, so deeply nested tuples are transformed fully instead of being returned unmodified.The depth bail-out was originally a guard against effectively-infinite nesting, but the existing recursion-count limit already protects against runaway types, and the early return caused incorrect TypeVar solving in legitimate code.
Testing
Added
typeVarTuple31.pysample reproducing the escaping TypeVar with nested recursive tuple aliases, and wired it up asTypeVarTuple31intypeEvaluator6.test.ts(Python 3.12). The sample asserts zero errors; the final chained assignment previously failed becauseOOescaped. Ran the type-evaluator test suite to confirm the new test passes and no existingTypeVarTupletests regressed.Addresses
Fixes https://github.com/microsoft/pylance-release/issues/11472
Generated by fix_all_my_issues pipeline