Skip to content

Fix defaulted subclass type specialization - #11596

Merged
Rich Chiodo (rchiodo) merged 6 commits into
mainfrom
rchiodo-fix-generic-defaults
Aug 10, 2026
Merged

Rich Chiodo (rchiodo) merged 6 commits into
mainfrom
rchiodo-fix-generic-defaults

Conversation

@rchiodo

@rchiodo Rich Chiodo (rchiodo) commented Aug 6, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Generic class values remain unspecialized so they can still be subscripted, while their typeForm records the default-specialized type. During type[...] assignability, Pyright was building the inheritance chain from that unspecialized class value, so absent source type arguments were treated permissively and a defaulted subclass could be accepted through the wrong specialization.

When every retained class type parameter has an explicit, non-gradual default, specialize the temporary source used for class assignability with specializeWithDefaultTypeArgs. The normal variance-aware type-argument comparison then rejects incompatible type[...] specializations. Classes with defaultless parameters, or defaults that resolve to top-level Any, Unknown, a gradual callable, or a gradual unpacked tuple, retain their existing unspecialized inference behavior. Structurally concrete defaults such as tuple[Any, ...] remain enforceable.

Coverage

Add the generics_defaults_specialization.py conformance case: bare Child is accepted as type[Child[str]] and rejected as type[Child[int]] when the retained base type parameter defaults to str.

Add realistic framework and library coverage for:

  • registries and factories storing type[Subclass[...]]
  • partial specialization with a leading non-defaulted parameter
  • multi-level and multiple-inheritance subclass forwarding
  • aliases, explicit specialization, runtime class subscription, and constructor inference
  • invariant and covariant defaulted parameters
  • direct and transitive Any, unions containing Any, gradual ParamSpec and TypeVarTuple defaults, and defaultless/Unknown inference
  • nested structural defaults containing Any, including a psycopg-style Cursor[tuple[Any, ...]]
  • the real-world gradual ctypes.py_object[Any] counterexample

mypy_primer audit

All eight shards completed successfully on d72de0181.

  • SymPy: 38,394 -> 38,220 errors (net -174). The artifact contains 228 removed and 54 added diagnostic headers: 47 same-location text replacements, 181 pure removals, and 7 pure additions. Temporary class-name tracing found ctypes.py_object as the only eligible bare defaulted class reached in SymPy; its direct Any default is skipped by the final guard, so these sites have no semantic path through the new specialization. Classified as unrelated primer/run diagnostic churn, not a regression.
  • psycopg: four added assignments for sync/async cursor and server-cursor factories. Their retained default is the structurally concrete TupleRow = tuple[Any, ...]; these are expected corrections because a bare Cursor[TupleRow] is not valid for every connection Row.
  • scikit-learn: two same-diagnostic ndarray rendering replacements with no count change; benign text churn.
  • All other projects: no deltas; shards 2-7 were empty.

Precision regression report

  • Failure classification: B - Pyright limitation
  • Relevant typeshed commit: N/A
  • Precision regressions: none
  • Mitigation: concrete defaults are enforced only for exact bare class values whose retained parameters all have explicit, non-gradual defaults. Defaultless and gradual defaults preserve prior inference rather than materializing Unknown or Any.

Tests

  • All 1,184 type-evaluator tests
  • npx jest typeEvaluator5.test typeEvaluator8.test --runInBand --forceExit -t "TypeVarDefault|TypeForm" (25 tests)
  • npm run build in packages/pyright-internal
  • npm run build:cli:dev
  • npm run typecheck
  • npm run check:eslint
  • npm run check:prettier
  • Focused CLI checks for the conformance sample, realistic registry/factory sample, ctypes.py_object, mixed/defaultless inference, and nested structural Any defaults

@StellaHuang95

Stella Huang (StellaHuang95) commented Aug 6, 2026 •

Copy link
Copy Markdown
Collaborator

🔒 Automated review in progress — Stella Huang (@StellaHuang95) is auto-reviewing this PR.

@StellaHuang95 Stella Huang (StellaHuang95) added the review-auto:changes-requested Automated review: posted blocking findings to address. label Aug 6, 2026
@github-actions

This comment has been minimized.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved via Review Center.

@StellaHuang95 Stella Huang (StellaHuang95) added review-auto:approved Automated review: no blocking findings (approval posted). and removed review-auto:changes-requested Automated review: posted blocking findings to address. labels Aug 7, 2026
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved via Review Center.

Failure classification: B - Pyright limitation
Typeshed commit: N/A
Precision: Enforce concrete retained defaults while preserving inference for defaultless and gradual Any/Unknown defaults.

Co-authored-by: Copilot App <[email protected]>
@github-actions

This comment has been minimized.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved via Review Center.

@github-actions

Copy link
Copy Markdown
Contributor

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

sympy (https://github.com/sympy/sympy)
+   .../projects/sympy/sympy/solvers/diophantine/diophantine.py:176:44 - error: Cannot access attribute "expand" for class "Basic"
+     Attribute "expand" is unknown (reportAttributeAccessIssue)
+   .../projects/sympy/sympy/solvers/diophantine/tests/test_diophantine.py:314:30 - error: Cannot access attribute "as_independent" for class "Basic"
+     Attribute "as_independent" is unknown (reportAttributeAccessIssue)
+   .../projects/sympy/sympy/solvers/diophantine/tests/test_diophantine.py:383:30 - error: Cannot access attribute "as_independent" for class "Basic"
+     Attribute "as_independent" is unknown (reportAttributeAccessIssue)
+   .../projects/sympy/sympy/solvers/ode/hypergeometric.py:247:67 - error: Operator "**" not supported for types "Basic" and "Literal[2]" (reportOperatorIssue)
-   .../projects/sympy/sympy/solvers/ode/lie_group.py:619:61 - error: Operator "-" not supported for type "Unknown | Basic" (reportOperatorIssue)
-   .../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"
+   .../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"
-     Type "Unknown | None" is not assignable to type "Expr"
+     Type "Expr | Unknown | None" is not assignable to type "Expr"
-   .../projects/sympy/sympy/solvers/ode/ode.py:1433:17 - error: Argument of type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Unknown | Expr" cannot be assigned to parameter "value" of type "Zero" in function "__setitem__"
+   .../projects/sympy/sympy/solvers/ode/ode.py:1433:17 - error: Argument of type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | Expr" cannot be assigned to parameter "value" of type "Zero" in function "__setitem__"
-     Type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Unknown | Expr" is not assignable to type "Zero"
+     Type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | Expr" is not assignable to type "Zero"
+   .../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:2961:17 - error: Argument of type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Unknown | Expr" cannot be assigned to parameter "value" of type "Zero" in function "__setitem__"
+   .../projects/sympy/sympy/solvers/ode/ode.py:2961:17 - error: Argument of type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | Expr" cannot be assigned to parameter "value" of type "Zero" in function "__setitem__"
-     Type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Unknown | Expr" is not assignable to type "Zero"
+     Type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | Expr" is not assignable to type "Zero"
+   .../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)

... (truncated 514 lines) ...

psycopg (https://github.com/psycopg/psycopg)
+   .../projects/psycopg/psycopg/psycopg/connection.py:78:31 - error: Cannot assign to attribute "cursor_factory" for class "Connection[Row@Connection]*"
+     "type[Cursor[TupleRow]]" is not assignable to "type[Cursor[Row@Connection]]"
+     Type "type[Cursor[Row@Cursor]]" is not assignable to type "type[Cursor[Row@Connection]]"
+       Type parameter "Row@Cursor" is covariant, but "TupleRow" is not a subtype of "Row@Connection"
+         Type "TupleRow" is not assignable to type "Row@Connection" (reportAttributeAccessIssue)
+   .../projects/psycopg/psycopg/psycopg/connection.py:79:38 - error: Cannot assign to attribute "server_cursor_factory" for class "Connection[Row@Connection]*"
+     "type[ServerCursor[TupleRow]]" is not assignable to "type[ServerCursor[Row@Connection]]"
+     Type "type[ServerCursor[Row@ServerCursor]]" is not assignable to type "type[ServerCursor[Row@Connection]]"
+       Type parameter "Row@ServerCursor" is covariant, but "TupleRow" is not a subtype of "Row@Connection"
+         Type "TupleRow" is not assignable to type "Row@Connection" (reportAttributeAccessIssue)
+   .../projects/psycopg/psycopg/psycopg/connection_async.py:82:31 - error: Cannot assign to attribute "cursor_factory" for class "AsyncConnection[Row@AsyncConnection]*"
+     "type[AsyncCursor[TupleRow]]" is not assignable to "type[AsyncCursor[Row@AsyncConnection]]"
+     Type "type[AsyncCursor[Row@AsyncCursor]]" is not assignable to type "type[AsyncCursor[Row@AsyncConnection]]"
+       Type parameter "Row@AsyncCursor" is covariant, but "TupleRow" is not a subtype of "Row@AsyncConnection"
+         Type "TupleRow" is not assignable to type "Row@AsyncConnection" (reportAttributeAccessIssue)
+   .../projects/psycopg/psycopg/psycopg/connection_async.py:83:38 - error: Cannot assign to attribute "server_cursor_factory" for class "AsyncConnection[Row@AsyncConnection]*"
+     "type[AsyncServerCursor[TupleRow]]" is not assignable to "type[AsyncServerCursor[Row@AsyncConnection]]"
+     Type "type[AsyncServerCursor[Row@AsyncServerCursor]]" is not assignable to type "type[AsyncServerCursor[Row@AsyncConnection]]"
+       Type parameter "Row@AsyncServerCursor" is covariant, but "TupleRow" is not a subtype of "Row@AsyncConnection"
+         Type "TupleRow" is not assignable to type "Row@AsyncConnection" (reportAttributeAccessIssue)
- 1936 errors, 113 warnings, 0 informations
+ 1940 errors, 113 warnings, 0 informations

scikit-learn (https://github.com/scikit-learn/scikit-learn)
-     "type[ndarray[_ShapeT_co@ndarray, _DTypeT_co@ndarray]]" is not assignable to "type[coo_array[Any, tuple[Any, ...]]]"
+     "type[ndarray[_AnyShape, dtype[Any]]]" is not assignable to "type[coo_array[Any, tuple[Any, ...]]]"
-     "type[ndarray[_ShapeT_co@ndarray, _DTypeT_co@ndarray]]" is not assignable to "type[coo_array[Any, tuple[Any, ...]]]"
+     "type[ndarray[_AnyShape, dtype[Any]]]" is not assignable to "type[coo_array[Any, tuple[Any, ...]]]"

@StellaHuang95 Stella Huang (StellaHuang95) added review-auto:changes-requested Automated review: posted blocking findings to address. and removed review-auto:approved Automated review: no blocking findings (approval posted). labels Aug 10, 2026
@rchiodo
Rich Chiodo (rchiodo) merged commit 273a1a8 into main Aug 10, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review-auto:changes-requested Automated review: posted blocking findings to address.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants