Skip to content

Fix reflected operator priority when the right operand is a subclass - #11662

Closed
Nithin (nithin42) wants to merge 5 commits into
microsoft:mainfrom
nithin42:fix-reflected-operator-priority
Closed

Nithin (nithin42) wants to merge 5 commits into
microsoft:mainfrom
nithin42:fix-reflected-operator-priority

Conversation

@nithin42

Copy link
Copy Markdown

Fixes #11515

Root cause

There are two separate problems behind the reported symptom.

1. The reflected form of the operator is never given priority.

Python evaluates x op y by calling type(x).__op__ first, except in one case: if type(y) is a proper subclass of type(x) and it overrides the reflected method __rop__, then type(y).__rop__ is called first. validateArithmeticOperation in operations.ts always tried the left operand's method first and only fell back to the reflected form if that failed. Since int.__and__ accepts any int, it always won, and the more precise result from IntFlag.__rand__ was never considered.

2. An undeclared class member does not override a declared method in a base class.

getTypeOfClassMemberName looks for a member with a declared type first, walking the whole MRO before falling back to a lookup that also accepts inferred types. When a derived class defines a member through a plain assignment, that member has no declared type, so the lookup skipped it and returned a declared member from a base class instead.

typeshed defines IntFlag's reflected methods with the alias idiom:

class IntFlag(int, ReprEnum, Flag, boundary=KEEP):
    def __and__(self, other: int) -> Self: ...
    __rand__ = __and__

int already declares __rand__, so Flags.A.__rand__ resolved to int.__rand__ ((value: int, /) -> int) rather than the alias. Even with the operator ordering fixed, the reflected call would still have produced int.

Fix

operations.ts now checks, before calling the left operand's magic method, whether the right operand's type is a proper subclass of the left operand's type and overrides the reflected magic method. If so, the reflected method is tried first, with the existing evaluation order used as a fallback so nothing regresses when the reflected call doesn't apply.

typeEvaluator.ts uses the existing but previously unused skippedUndeclaredType flag on ClassMember. When the declared-types-only lookup skipped an undeclared symbol in a class earlier in the MRO and the declared symbol it found is a method, the overriding symbol is used instead. This is deliberately limited to methods so that reportIncompatibleVariableOverride behavior for variable overrides is unchanged.

Verification

New sample operator13.py registered in typeEvaluator8.test.ts covers:

  • int() & Flags.A, | and ^ now evaluate to Flags
  • int() & int() still evaluates to int
  • a subclass that overrides __radd__ takes priority over the base class __add__
  • a subclass that does not override __radd__ keeps the normal evaluation order
  • a right operand that is not a subclass keeps the normal evaluation order
  • a method alias assignment overriding a base class method of the same name

Reverting either source change makes the new test fail. With both applied, all 1285 tests across the typeEvaluator* and checker suites pass with no regressions.

@heejaechang

Heejae Chang (heejaechang) commented Aug 19, 2026 •

Copy link
Copy Markdown
Collaborator

🔒 Automated review in progress — Heejae Chang (@heejaechang) is auto-reviewing this PR.

@heejaechang Heejae Chang (heejaechang) added the review-auto:changes-requested Automated review: posted blocking findings to address. label Aug 19, 2026
@heejaechang

Heejae Chang (heejaechang) commented Sep 23, 2026 •

Copy link
Copy Markdown
Collaborator

🔴 Pyright CLI QA — 37b58481 — verdict: red

Automated CLI checks compared this change with its base revision.
18 focused check(s) completed.

Needs review: Diagnostics changed for this example

The base and PR report different diagnostics for the same example. Is this change intended?

Reproduction and observed results

Create these files in an empty directory:

main.py

from typing import Callable, cast, assert_type, assert_never
reveal_type(0)
class A:
    def __add__(self, other: object) -> int: return 1
    def __radd__(self, other: object, /) -> str: return 'r'
class B(A):
    __radd__ = cast(Callable[[A, object], str], A.__radd__)
def check(a: A, b: B):
    assert_type(a + b, int)
    (a + b).bit_length()
    v = a + b
    if not isinstance(v, int): assert_never(v)

pyrightconfig.json

{
  "include": [
    "main.py"
  ],
  "pythonVersion": "3.13",
  "typeCheckingMode": "basic"
}

From that directory, run each built Pyright revision:

node <pyright-cli-entrypoint> --outputjson -p .

Base 611afe615b8659a154aa9656ee5b8cfddf57cdac

{
  "exit_code": 0,
  "diagnostics": [
    {
      "file": "main.py",
      "severity": "information",
      "rule": null,
      "message": "Type of \"0\" is \"Literal[0]\"",
      "range": {
        "start": [
          1,
          12
        ],
        "end": [
          1,
          13
        ]
      }
    }
  ]
}

PR 37b58481cfd391dd813659c1a3d4535530c2281f

{
  "exit_code": 1,
  "diagnostics": [
    {
      "file": "main.py",
      "severity": "error",
      "rule": "reportArgumentType",
      "message": "Argument of type \"str\" cannot be assigned to parameter \"arg\" of type \"Never\" in function \"assert_never\"\n  Type \"str\" is not assignable to type \"Never\"",
      "range": {
        "start": [
          11,
          44
        ],
        "end": [
          11,
          45
        ]
      }
    },
    {
      "file": "main.py",
      "severity": "error",
      "rule": "reportAttributeAccessIssue",
      "message": "Cannot access attribute \"bit_length\" for class \"str\"\n  Attribute \"bit_length\" is unknown",
      "range": {
        "start": [
          9,
          12
        ],
        "end": [
          9,
          22
        ]
      }
    },
    {
      "file": "main.py",
      "severity": "information",
      "rule": null,
      "message": "Type of \"0\" is \"Literal[0]\"",
      "range": {
        "start": [
          1,
          12
        ],
        "end": [
          1,
          13
        ]
      }
    },
    {
      "file": "main.py",
      "severity": "error",
      "rule": "reportAssertTypeFailure",
      "message": "\"assert_type\" mismatch: expected \"int\" but received \"str\"",
      "range": {
        "start": [
          8,
          16
        ],
        "end": [
          8,
          21
        ]
      }
    }
  ]
}

Diagnostic ranges use zero-based line and character positions.

Python calls the reflected form of a binary operator first when the right
operand's type is a proper subclass of the left operand's type and overrides
the reflected magic method. Pyright always tried the left operand's method
first, so `int() & Flags.A` for an IntFlag subclass evaluated to int instead
of Flags.
@rchiodo
Rich Chiodo (rchiodo) force-pushed the fix-reflected-operator-priority branch from c0913e2 to ee02130 Compare October 26, 2026 20:06
Restore inherited member contracts and bound-method handling. Preserve
function alias identity, handle prioritized NotImplemented fallbacks,
and bound ancestry checks with the existing MRO.

Add regression coverage for dispatch, aliases, and inherited attributes.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@rchiodo

Copy link
Copy Markdown
Collaborator

I tightened the reflected-method check to distinguish actual overrides from aliases of the same implementation, while keeping the original IntFlag improvement. A prioritized method returning NotImplemented now falls back to the left-hand method without dropping successful result types. I also removed the broad member fallback because it bypassed existing attribute checks and method binding, and switched to a bounded MRO scan to avoid repeatedly traversing deep diamond hierarchies.

The regression cases and both affected evaluator suites pass (344 tests), along with the TypeScript build, changed-file lint/format checks, and CPython checks. Targeted timing comparisons no longer show the identified scaling regression. I haven't run the full repository suite, and the pre-existing metaclass and inherited comparison-priority gaps are still outside this change.

@nithin42

Copy link
Copy Markdown
Author

Thanks for tightening this up, Rich Chiodo (@rchiodo)! Distinguishing real overrides from aliased base methods, falling back cleanly on NotImplemented, and dropping the getTypeOfClassMemberName fallback in favor of the bounded MRO scan makes complete sense and resolves all the QA bot regressions cleanly. Appreciate the review and help landing this!

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Classify these regressions as Pyright inference limitations (B). Restore validated alias parameter and generic types while resolving callable identity only for operator dispatch. Require genuine subclass evidence before prioritizing reflected methods on synthesized intersections, and handle class operands through their effective metaclasses.

Add exact signature, consumer, alias-chain, intersection, and metaclass regressions without weakening existing type expectations.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Class B / Pyright limitation: ordinary arithmetic could leak the Any-derived NotImplementedType sentinel and disable downstream assignment and return checking. Preserve subclass dispatch priority while combining successful results and following valid fallback paths, including same-runtime-class restrictions. Add precise regression coverage for chained arithmetic, partial decline, Never, None, direct calls, and metaclasses. No typeshed changes.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Classify the xarray false positives as a Pyright inference limitation (B), not a typeshed change. Reuse bounded call-site inference for inherited unannotated methods inferred as NotImplemented, supplying the actual bound receiver and synthetic argument types.

Retain explicit return contracts and reject fully Unknown refinements. Cover normal, reflected, and metaclass calls, py.typed libraries, and unsafe Self or decorated-overload shortcuts without weakening existing type expectations.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@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/algebras/quaternion.py:726:16 - error: Type "Expr" is not assignable to return type "Quaternion"
-     "Expr" is not assignable to "Quaternion" (reportReturnType)
-   .../projects/sympy/sympy/algebras/quaternion.py:1700:16 - error: Type "Expr" is not assignable to return type "Quaternion"
-     "Expr" is not assignable to "Quaternion" (reportReturnType)
+   .../projects/sympy/sympy/assumptions/refine.py:199:31 - error: Argument of type "One | NegativeOne | Zero | Integer | Rational | NaN | ComplexInfinity | Float | Expr | Infinity | NegativeInfinity | Unknown" cannot be assigned to parameter "element" of type "Rational" in function "add"
+     Type "One | NegativeOne | Zero | Integer | Rational | NaN | ComplexInfinity | Float | Expr | Infinity | NegativeInfinity | Unknown" is not assignable to type "Rational"
+       "Expr" is not assignable to "Rational" (reportArgumentType)
+   .../projects/sympy/sympy/assumptions/refine.py:208:31 - error: Cannot access attribute "as_two_terms" for class "NaN"
+     Attribute "as_two_terms" is unknown (reportAttributeAccessIssue)
+     Attribute "as_two_terms" is unknown (reportAttributeAccessIssue)
+   .../projects/sympy/sympy/assumptions/refine.py:208:31 - error: Cannot access attribute "as_two_terms" for class "ComplexInfinity"
+     Attribute "as_two_terms" is unknown (reportAttributeAccessIssue)
+   .../projects/sympy/sympy/assumptions/refine.py:208:31 - error: Cannot access attribute "as_two_terms" for class "Rational"
+     Attribute "as_two_terms" is unknown (reportAttributeAccessIssue)
+   .../projects/sympy/sympy/assumptions/refine.py:208:31 - error: Cannot access attribute "as_two_terms" for class "Zero"
+     Attribute "as_two_terms" is unknown (reportAttributeAccessIssue)
+   .../projects/sympy/sympy/assumptions/refine.py:208:31 - error: Cannot access attribute "as_two_terms" for class "Infinity"
+     Attribute "as_two_terms" is unknown (reportAttributeAccessIssue)
+   .../projects/sympy/sympy/assumptions/refine.py:208:31 - error: Cannot access attribute "as_two_terms" for class "NegativeInfinity"
+     Attribute "as_two_terms" is unknown (reportAttributeAccessIssue)
+   .../projects/sympy/sympy/assumptions/refine.py:208:31 - error: Cannot access attribute "as_two_terms" for class "Float"
+     Attribute "as_two_terms" is unknown (reportAttributeAccessIssue)
+   .../projects/sympy/sympy/assumptions/refine.py:208:31 - error: Cannot access attribute "as_two_terms" for class "One"
+     Attribute "as_two_terms" is unknown (reportAttributeAccessIssue)
+   .../projects/sympy/sympy/assumptions/refine.py:208:31 - error: Cannot access attribute "as_two_terms" for class "NegativeOne"
+     Attribute "as_two_terms" is unknown (reportAttributeAccessIssue)
+   .../projects/sympy/sympy/assumptions/refine.py:208:31 - error: Cannot access attribute "as_two_terms" for class "Integer"
-   .../projects/sympy/sympy/combinatorics/character_table.py:504:30 - error: Argument of type "Expr | Unknown | One | Poly | Any" cannot be assigned to parameter "x" of type "ConvertibleToInt" in function "__new__"
+   .../projects/sympy/sympy/combinatorics/character_table.py:504:30 - error: Argument of type "Expr | Unknown | One | Poly" cannot be assigned to parameter "x" of type "ConvertibleToInt" in function "__new__"
-     Type "Expr | Unknown | One | Poly | Any" is not assignable to type "ConvertibleToInt"
+     Type "Expr | Unknown | One | Poly" is not assignable to type "ConvertibleToInt"
-   .../projects/sympy/sympy/combinatorics/character_table.py:504:30 - error: Argument of type "Expr | Unknown | One | Poly | Any" cannot be assigned to parameter "x" of type "ConvertibleToInt" in function "__new__"
+   .../projects/sympy/sympy/combinatorics/character_table.py:504:30 - error: Argument of type "Expr | Unknown | One | Poly" cannot be assigned to parameter "x" of type "ConvertibleToInt" in function "__new__"
-     Type "Expr | Unknown | One | Poly | Any" is not assignable to type "ConvertibleToInt"
+     Type "Expr | Unknown | One | Poly" is not assignable to type "ConvertibleToInt"
+   .../projects/sympy/sympy/concrete/tests/test_products.py:101:12 - error: No overloads for "simplify" match the provided arguments (reportCallIssue)
-     Operator "*" not supported for types "tuple[Unknown, ...]" and "tuple[Unknown, ...]" when expected type is "Expr"
+     Operator "*" not supported for types "tuple[Unknown, ...]" and "tuple[Unknown, ...]" when expected type is "Basic"
-     Operator "*" not supported for types "tuple[Unknown, ...]" and "Basic*" when expected type is "Expr"
+     Operator "*" not supported for types "tuple[Unknown, ...]" and "Basic*" when expected type is "Basic"
-     Operator "*" not supported for types "tuple[Unknown, ...]" and "Product" when expected type is "Expr"
+     Operator "*" not supported for types "tuple[Unknown, ...]" and "Product" when expected type is "Basic"
-     Operator "*" not supported for types "tuple[Unknown, ...]" and "Basic" when expected type is "Expr"
+     Operator "*" not supported for types "tuple[Unknown, ...]" and "Basic" when expected type is "Basic"
-     Operator "*" not supported for types "tuple[Unknown, ...]" and "Expr" when expected type is "Expr"
+     Operator "*" not supported for types "tuple[Unknown, ...]" and "Expr" when expected type is "Basic"
-     Operator "*" not supported for types "tuple[Unknown, ...]" and "Equality" when expected type is "Expr"
+     Operator "*" not supported for types "tuple[Unknown, ...]" and "Equality" when expected type is "Basic"
-     Operator "*" not supported for types "tuple[Unknown, ...]" and "NaN" when expected type is "Expr"
+     Operator "*" not supported for types "tuple[Unknown, ...]" and "NaN" when expected type is "Basic"
-     Operator "*" not supported for types "Basic*" and "tuple[Unknown, ...]" when expected type is "Expr"
+     Operator "*" not supported for types "Basic*" and "tuple[Unknown, ...]" when expected type is "Basic"
+   .../projects/sympy/sympy/concrete/tests/test_products.py:101:21 - error: Argument of type "Unknown | tuple[Unknown, ...] | One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Expr" cannot be assigned to parameter "expr" of type "Basic" in function "simplify"
+     Type "Unknown | tuple[Unknown, ...] | One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Expr" is not assignable to type "Basic"
+       "tuple[Unknown, ...]" is not assignable to "Basic" (reportArgumentType)
-   .../projects/sympy/sympy/concrete/tests/test_products.py:158:25 - error: Operator "/" not supported for types "tuple[Unknown, ...] | Unknown | Self@Basic | Product | Basic | Expr | One | BooleanFalse | BooleanTrue | Equality | NaN" and "Unknown | tuple[Unknown, ...] | One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Expr | Infinity | NegativeInfinity | Float | NotImplementedType"
+   .../projects/sympy/sympy/concrete/tests/test_products.py:158:25 - error: Operator "/" not supported for types "tuple[Unknown, ...] | Unknown | Self@Basic | Product | Basic | Expr | One | BooleanFalse | BooleanTrue | Equality | NaN" and "Unknown | tuple[Unknown, ...] | One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Expr"
+   .../projects/sympy/sympy/core/add.py:333:17 - error: Argument of type "Number | One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Expr" cannot be assigned to parameter "value" of type "Number" in function "__setitem__"
+     Type "Number | One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Expr" is not assignable to type "Number"
+       "Expr" is not assignable to "Number" (reportArgumentType)
-   .../projects/sympy/sympy/core/add.py:1084:46 - error: Operator "+" not supported for types "One | Zero | Unknown | None" and "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | NotImplementedType | Expr | Unknown"
+   .../projects/sympy/sympy/core/add.py:1084:46 - error: Operator "+" not supported for types "One | Zero | Unknown | None" and "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Expr | Unknown"
+   .../projects/sympy/sympy/core/add.py:1183:9 - error: Method "as_content_primitive" overrides class "Expr" in an incompatible manner
+     Return type mismatch: base method returns type "tuple[One, Expr]", override returns type "tuple[One | NegativeOne | Zero | Integer | Expr | ComplexInfinity | NaN | Rational | Infinity | NegativeInfinity | Float | Unknown, Expr | Unknown | Add]"
+       "tuple[One | NegativeOne | Zero | Integer | Expr | ComplexInfinity | NaN | Rational | Infinity | NegativeInfinity | Float | Unknown, Expr | Unknown | Add]" is not assignable to "tuple[One, Expr]"
+         Tuple entry 1 is incorrect type
+           Type "One | NegativeOne | Zero | Integer | Expr | ComplexInfinity | NaN | Rational | Infinity | NegativeInfinity | Float | Unknown" is not assignable to type "One"
+             "Expr" is not assignable to "One" (reportIncompatibleMethodOverride)
+   .../projects/sympy/sympy/core/add.py:1183:9 - error: Method "as_content_primitive" overrides class "Basic" in an incompatible manner
+     Return type mismatch: base method returns type "tuple[One, Self@Basic]", override returns type "tuple[One | NegativeOne | Zero | Integer | Expr | ComplexInfinity | NaN | Rational | Infinity | NegativeInfinity | Float | Unknown, Expr | Unknown | Add]"
+       "tuple[One | NegativeOne | Zero | Integer | Expr | ComplexInfinity | NaN | Rational | Infinity | NegativeInfinity | Float | Unknown, Expr | Unknown | Add]" is not assignable to "tuple[One, Self@Basic]"
+         Tuple entry 1 is incorrect type

... (truncated 933 lines) ...

core (https://github.com/home-assistant/core)
-   .../projects/core/homeassistant/components/aprilaire/climate.py:115:16 - error: Type "int" is not assignable to return type "ClimateEntityFeature"
-     "int" is not assignable to "ClimateEntityFeature" (reportReturnType)
-   .../projects/core/homeassistant/components/zwave_js/cover.py:98:41 - error: Cannot assign to attribute "_attr_supported_features" for class "CoverPositionMixin*"
-     Type "CoverEntityFeature | int" is not assignable to type "CoverEntityFeature | None"
-       Type "int" is not assignable to type "CoverEntityFeature | None"
-         "int" is not assignable to "CoverEntityFeature"
-         "int" is not assignable to "None" (reportAttributeAccessIssue)
-   .../projects/core/homeassistant/components/zwave_js/cover.py:111:13 - error: Operator "|=" not supported for types "CoverEntityFeature | None" and "Literal[CoverEntityFeature.STOP]"
-     Operator "|" not supported for types "None" and "Literal[CoverEntityFeature.STOP]" (reportOperatorIssue)
-   .../projects/core/homeassistant/components/zwave_js/cover.py:266:41 - error: Cannot assign to attribute "_attr_supported_features" for class "CoverTiltMixin*"
-     Type "CoverEntityFeature | int" is not assignable to type "CoverEntityFeature | None"
-       Type "int" is not assignable to type "CoverEntityFeature | None"
-         "int" is not assignable to "CoverEntityFeature"
-         "int" is not assignable to "None" (reportAttributeAccessIssue)
-   .../projects/core/homeassistant/components/zwave_js/cover.py:279:13 - error: Operator "|=" not supported for types "CoverEntityFeature | None" and "Literal[CoverEntityFeature.STOP_TILT]"
-     Operator "|" not supported for types "None" and "Literal[CoverEntityFeature.STOP_TILT]" (reportOperatorIssue)
- 43394 errors, 79 warnings, 0 informations
+ 43389 errors, 79 warnings, 0 informations

xarray (https://github.com/pydata/xarray)
+   .../projects/xarray/xarray/coding/cftime_offsets.py:164:16 - error: Operator "+" not supported for types "BaseCFTimeOffset*" and "Unknown | BaseCFTimeOffset*"
+     Operator "+" not supported for types "BaseCFTimeOffset*" and "BaseCFTimeOffset*" (reportOperatorIssue)
- 2618 errors, 219 warnings, 0 informations
+ 2619 errors, 219 warnings, 0 informations

pytensor (https://github.com/pymc-devs/pytensor)
-     Parameter 4 name mismatch: base parameter is named "cotangents", override parameter is named "gout" (reportIncompatibleMethodOverride)
+     Parameter 4 name mismatch: base parameter is named "cotangents", override parameter is named "gout"
+     Return type mismatch: base method returns type "list[Variable[Unknown, Unknown]]", override returns type "list[Unknown] | list[Unknown | Variable[Unknown, Unknown] | list[Variable[Unknown, Unknown]] | None]"
+       Type "list[Unknown] | list[Unknown | Variable[Unknown, Unknown] | list[Variable[Unknown, Unknown]] | None]" is not assignable to type "list[Variable[Unknown, Unknown]]"
+         "list[Unknown | Variable[Unknown, Unknown] | list[Variable[Unknown, Unknown]] | None]" is not assignable to "list[Variable[Unknown, Unknown]]"
+           Type parameter "_T@list" is invariant, but "Unknown | Variable[Unknown, Unknown] | list[Variable[Unknown, Unknown]] | None" is not the same as "Variable[Unknown, Unknown]"
+           Consider switching from "list" to "Sequence" which is covariant (reportIncompatibleMethodOverride)
-   .../projects/pytensor/pytensor/tensor/linalg/solvers/linear_control.py:440:53 - error: Operator "*" not supported for types "float" and "TensorVariable[Unknown, Unknown] | Unknown | Variable[Unknown, Unknown] | list[Variable[Unknown, Unknown]] | Any | NotImplementedType | None"
+   .../projects/pytensor/pytensor/tensor/linalg/solvers/linear_control.py:440:53 - error: Operator "*" not supported for types "float" and "TensorVariable[Unknown, Unknown] | Unknown | Variable[Unknown, Unknown] | list[Variable[Unknown, Unknown]] | Any | None"
-   .../projects/pytensor/pytensor/tensor/linalg/solvers/linear_control.py:443:9 - error: Operator ">" not supported for types "TensorVariable[Unknown, Unknown] | Unknown | Variable[Unknown, Unknown] | list[Variable[Unknown, Unknown]] | Any | NotImplementedType | None" and "None"
+   .../projects/pytensor/pytensor/tensor/linalg/solvers/linear_control.py:443:9 - error: Operator ">" not supported for types "TensorVariable[Unknown, Unknown] | Unknown | Variable[Unknown, Unknown] | list[Variable[Unknown, Unknown]] | Any | None" and "None"
-   .../projects/pytensor/pytensor/tensor/math.py:2788:21 - error: Operator "*" not supported for types "int | Unknown | NotImplementedType" and "Unbound | TensorConstant[Unknown] | Unknown | None"
+   .../projects/pytensor/pytensor/tensor/math.py:2788:21 - error: Operator "*" not supported for types "int | Unknown" and "Unbound | TensorConstant[Unknown] | Unknown | None"
-   .../projects/pytensor/pytensor/tensor/rewriting/math.py:1879:23 - error: Operator "*" not supported for types "Unknown | NotImplementedType | TensorConstant[Unknown] | None" and "Variable[Unknown, Unknown] | list[Variable[Unknown, Unknown]] | Unknown"
+   .../projects/pytensor/pytensor/tensor/rewriting/math.py:1879:23 - error: Operator "*" not supported for types "Unknown | TensorConstant[Unknown] | None" and "Variable[Unknown, Unknown] | list[Variable[Unknown, Unknown]] | Unknown"
-   .../projects/pytensor/pytensor/tensor/rewriting/math.py:1881:23 - error: Operator "/" not supported for types "Variable[Unknown, Unknown] | list[Variable[Unknown, Unknown]] | Unknown" and "Unknown | NotImplementedType | TensorConstant[Unknown] | None"
+   .../projects/pytensor/pytensor/tensor/rewriting/math.py:1881:23 - error: Operator "/" not supported for types "Variable[Unknown, Unknown] | list[Variable[Unknown, Unknown]] | Unknown" and "Unknown | TensorConstant[Unknown] | None"
+       Type "int" is not assignable to type "slice[Any, Any, Any] | TensorVariable[Unknown, Unknown]"
+         "int" is not assignable to "slice[Any, Any, Any]"
+         "int" is not assignable to "TensorVariable[Unknown, Unknown]" (reportReturnType)
+   .../projects/pytensor/pytensor/tensor/subtensor.py:445:24 - error: Type "tuple[int | Incomplete | Unknown, Literal[1]]" is not assignable to return type "tuple[slice[Any, Any, Any] | TensorVariable[Unknown, Unknown], int | TensorVariable[Unknown, Unknown]]"
+     Type "int | Incomplete | Unknown" is not assignable to type "slice[Any, Any, Any] | TensorVariable[Unknown, Unknown]"
- 5515 errors, 68 warnings, 0 informations
+ 5516 errors, 68 warnings, 0 informations

pandas-stubs (https://github.com/pandas-dev/pandas-stubs)
+   .../projects/pandas-stubs/tests/scalars/test_scalars.py:581:54 - error: Unnecessary "# pyright: ignore" rule: "reportAssertTypeFailure" (reportUnnecessaryTypeIgnoreComment)
+   .../projects/pandas-stubs/tests/scalars/test_scalars.py:622:54 - error: Unnecessary "# pyright: ignore" rule: "reportAssertTypeFailure" (reportUnnecessaryTypeIgnoreComment)
- 208 errors, 3 warnings, 0 informations
+ 210 errors, 3 warnings, 0 informations

jax (https://github.com/google/jax)
+   .../projects/jax/jax/_src/pallas/mosaic_gpu/lowering.py:4426:13 - error: Argument of type "FragmentedArray" cannot be assigned to parameter "start" of type "int | Array" in function "__init__"
+     Type "FragmentedArray" is not assignable to type "int | Array"
+       "FragmentedArray" is not assignable to "int"
+       "FragmentedArray" is not assignable to "Array" (reportArgumentType)
+   .../projects/jax/jax/_src/pallas/mosaic_gpu/lowering.py:4431:9 - error: No overloads for "__setitem__" match the provided arguments (reportCallIssue)
+   .../projects/jax/jax/_src/pallas/mosaic_gpu/lowering.py:4431:9 - error: Argument of type "FragmentedArray" cannot be assigned to parameter "value" of type "Slice" in function "__setitem__"
+     "FragmentedArray" is not assignable to "Slice" (reportArgumentType)
- 3706 errors, 72 warnings, 0 informations
+ 3709 errors, 72 warnings, 0 informations

@github-actions

Copy link
Copy Markdown
Contributor

mypy_primer analysis

Advisory AI analysis of primer run 40600722580, attempt 1
for commit 37b58481cfd391dd813659c1a3d4535530c2281f. This is not an approval or proof of correctness.

Regression review required. Potential regressions or recorded warning signals are listed first; causation must be established separately.

Potential regressions

sympy: Receiver-bound operator reinference removes NotImplemented/Any alternatives but produces broad new errors across SymPy; precision improves, yet polymorphic operator bodies may now be over-specialized.

Regression warning signals require review, regardless of the AI assessment:

  • Changed diagnostic details introduce Any/Unknown.

AI assessment: Possible regression (medium confidence). PR attribution: Likely caused by the PR.

Before / baseline evidence: The recorded baseline often retained NotImplementedType or Any in operator-result unions. At assumptions/refine.py:199/208 there were no diagnostics for the Rational.add argument or as_two_terms access; quaternion.py:726 and :1700 instead reported Expr returned where Quaternion was required. The manifest's gradual signal is chiefly Any/NotImplemented disappearing, not an observed container becoming Any.

After / PR evidence: The PR run reports a concrete union of SymPy numeric/Expr subclasses plus Unknown at refine.py:199 and flags as_two_terms on many union members at :208. Many operator diagnostics are same-location rewrites with NotImplementedType removed, but new call, override, attribute, and iteration diagnostics also appear; the two Quaternion return errors disappear.

Why this matters: Users gain checking that Any/NotImplemented previously diluted, but can no longer rely on inherited unannotated operators remaining declined/gradual. If the bound receiver does not determine every dynamic helper path, valid symbolic code can acquire attribute, overload, and override errors. Conversely, the removed Quaternion errors may be useful corrected inference rather than suppression.

Causal analysis:

At the exact head, getNotImplementedInferenceReceiver selects an inherited unannotated method only when its inferred return is types.NotImplementedType and the bound receiver differs from the declaring class. _getInferredReturnTypeResult then invokes call-site inference with that receiver, and inferReturnTypeForCallSite supplies the concrete receiver and argument types. SymPy's heavily inherited arithmetic therefore loses NotImplemented branches and often Any, exposing concrete Expr subclasses to normal argument/member checks. That causal path explains both the widespread NotImplemented-only message rewrites and newly reachable diagnostics. It does not by itself prove the new unions are sound: helper overrides and reflected-operator priority are highly polymorphic, while the raw diff contains mixed additions and removals. The gradual-detail signal is counterevidence to type erasure because precision generally increases, but Unknown remains and several old checks vanish, so this should not be blanket-classified as improvement.

Uncertainty / next check: The exact synthetic merge SHA and ecosystem checkout were not visible in the retrieved logs. Minimize one affected hierarchy: inherited __add__ delegates to an overridable helper; use a positive subclass whose helper returns a concrete symbolic type and a negative subclass that conditionally returns NotImplemented. Compare direct, reflected, and base-typed calls plus the refine.py:208 member access.

xarray: A new error appears inside BaseCFTimeOffset.__rsub__ for -self + other; the PR preserves Unknown for a polymorphic base path, so valid subclass dispatch may now be rejected.

AI assessment: Possible regression (medium confidence). PR attribution: Likely caused by the PR.

Before / baseline evidence: The recorded baseline emitted no diagnostic at cftime_offsets.py:164. This is not evidence of an observed reveal type, only that the method body was accepted under the baseline operator inference.

After / PR evidence: The PR run reports that BaseCFTimeOffset + (Unknown | BaseCFTimeOffset) is unsupported at cftime_offsets.py:164, with a detail specifically rejecting BaseCFTimeOffset + BaseCFTimeOffset. The PR's own analogous polymorphic-offset test expects an intermediate result to remain Unknown and marks the surrounding operator path as an error.

Why this matters: Authors of extensible offset bases can no longer rely on an inherited operator body that delegates to subclass behavior type-checking cleanly. A runtime-valid concrete subclass may be rejected while the body is checked against the base receiver, forcing an annotation, cast, or ignore and reducing usefulness of subclass-specific operator inference.

Causal analysis:

The new gate refines only when the bound receiver differs from the method's declaring class. Concrete subclass calls can therefore be re-analyzed with the subclass receiver, but analysis of the polymorphic BaseCFTimeOffset method body still sees the base contract. The exact-head operator18 control deliberately keeps a similar '-self + other' result Unknown and expects an error for the base/helper contract. That decision plausibly causes the new line-164 diagnostic. Counterevidence is that a bare BaseCFTimeOffset may genuinely decline addition at runtime, so rejecting Base + Base could be correct. The concern is whether the implementation body is intended only as a template for concrete offsets; the raw diff alone cannot establish that intent.

Uncertainty / next check: Resolve the exact xarray revision and inspect BaseCFTimeOffset.apply plus concrete overrides. Positive control: a concrete final offset whose helper accepts/returns itself should type as that offset. Negative control: a base instance or subclass that retains NotImplemented should still report unsupported +. Also test __rsub__ through a base-typed parameter.

pytensor: Most changes are precision rewrites removing NotImplementedType, but a newly exposed invariant-list override mismatch and subtensor return error need call-path validation.

Regression warning signals require review, regardless of the AI assessment:

  • Changed diagnostic details introduce Any/Unknown.

AI assessment: Needs human review (medium confidence). PR attribution: Likely caused by the PR.

Before / baseline evidence: Several existing operator errors included NotImplementedType in unions at linear_control.py:440/:443, tensor/math.py:2788, and rewriting/math.py:1879/:1881. The override diagnostic reported only a parameter-name mismatch. No return error was reported at subtensor.py:445.

After / PR evidence: The same operator locations remain errors but lose NotImplementedType, so those are message/detail rewrites rather than new checks. The override now additionally reports list[Unknown | Variable | list[Variable] | None] incompatible with list[Variable]. A new subtensor.py:445 return error says the first tuple element may be int | Incomplete | Unknown rather than slice | TensorVariable.

Why this matters: Removing a decline-only branch gives more useful operator types. However, downstream implementers can no longer rely on the override passing apart from its parameter name, and a subtensor helper can no longer return its inferred tuple without a cast/annotation. These may expose real annotation gaps or may be false positives from receiver-specific reinference.

Causal analysis:

At the exact head, inherited unannotated methods cached as NotImplemented are re-analyzed with the bound receiver and actual arguments. This directly explains NotImplementedType disappearing at unchanged operator locations and can make formerly gradual return paths concrete enough for invariant list and declared-return checks. The manifest's gradual signal therefore reflects increased precision, not type erasure. The new override details and subtensor return diagnostic are potentially useful restored checking, but the raw concise diff omits the override header/location and cannot safely associate its detail lines with a specific declaration. Nor was the complete operator/helper chain for subtensor available at the recorded ecosystem revision. That missing context prevents deciding whether the declarations are wrong or the checker over-specialized a dynamic path.

Uncertainty / next check: Locate the override header in full baseline/new output and inspect its base/derived return declarations. For subtensor.py:445, reveal each tuple element before return under a concrete TensorVariable receiver and under a base/unknown receiver; verify the int branch is truly impossible before calling this an exposed typing issue.

jax: Receiver-bound operator inference now feeds FragmentedArray into index/slice APIs, adding argument and overload errors; this may preserve the wrong retained candidate for JAX's DSL.

AI assessment: Possible regression (medium confidence). PR attribution: Likely caused by the PR.

Before / baseline evidence: The recorded baseline emitted no errors at mosaic_gpu/lowering.py:4426 or :4431. The raw diff does not expose the baseline expression type, so it is unknown whether these calls previously relied on NotImplemented fallback, Any, or a different overload result.

After / PR evidence: At :4426 a FragmentedArray is rejected as slice.__init__ start (expected int | Array). At :4431 __setitem__ has no matching overload and rejects FragmentedArray where Slice is expected. The outer FragmentedArray API is retained rather than erased to Any/Unknown, but it no longer satisfies the downstream indexing contracts.

Why this matters: JAX lowering code that uses overloaded FragmentedArray arithmetic to construct DSL indices can no longer type-check. Users lose reliance on those expressions being accepted by slice/index overloads; if FragmentedArray is intentionally converted or supported by the DSL, these are false positives rather than useful errors.

Causal analysis:

The exact-head PR re-analyzes inherited, unannotated operators cached as NotImplemented with the concrete receiver and actual operand types. It also guards against accepting a wholly Unknown refinement. That can replace a declined/gradual operator result with FragmentedArray, after which normal argument checking reaches slice and __setitem__ and reports the observed incompatibilities. This is causally consistent and not type erasure. However, the available evidence does not show the operator declaration or whether JAX's indexing abstraction intentionally accepts this value through conversion. The added diagnostics could expose a missing annotation/overload, but blaming JAX would require inspecting that full overload set at the exact recorded revision, so a checker regression remains credible.

Uncertainty / next check: Inspect FragmentedArray's relevant normal/reflected operator and the target __setitem__/Slice overloads at the exact checkout. Positive control: the operator result should satisfy the intended DSL index protocol and preserve FragmentedArray members. Negative control: passing a raw unrelated FragmentedArray to built-in slice must still fail.

Other project assessments

core: Inherited IntFlag-style operators now preserve Home Assistant feature-enum results, removing spurious int assignments and optional bitwise errors rather than erasing checking.

AI assessment: Expected improvement (medium confidence). PR attribution: Likely caused by the PR.

Before / baseline evidence: The baseline treated feature expressions as int in aprilaire/climate.py:115 and as CoverEntityFeature | int in zwave_js/cover.py:98 and :266. It consequently rejected enum-typed returns/attributes and then also rejected |= at :111/:279 because the destination could be None.

After / PR evidence: Those diagnostics disappear. There is no evidence that the values became Any or Unknown; the removed details specifically identify the incompatible alternative as int, consistent with receiver-bound operator inference retaining the feature enum type.

Why this matters: Downstream entity code can combine feature flags and return them under ClimateEntityFeature/CoverEntityFeature annotations without casts or ignores. It still relies on the enum operator implementation being modeled as returning the concrete enum rather than bare int.

Causal analysis:

The exact-head change re-analyzes an inherited unannotated operator when its cached result is only NotImplemented, supplying the concrete subclass receiver and call argument types. That allows a feature enum/IntFlag subclass path to infer its concrete result instead of falling back to an int-containing result. The observed removals line up: int is the only problematic branch in the return and attribute diagnostics, and eliminating it also prevents the cascading optional |= complaints. No removed-check or type-erasure signal is recorded for this project. The conclusion is about static inference, not runtime behavior. Confidence is limited because the workflow's exact synthetic merge SHA and exact Home Assistant checkout were not recovered; the pinned source is a nearby revision.

Uncertainty / next check: Confirm on the recorded checkout that the expressions at all five locations dispatch through the inherited unannotated operator selected by the new gate. A negative control should use an explicitly annotated operator returning int; its assignment to the feature enum must still fail.

pandas-stubs: Two assert_type suppressions become unnecessary, indicating inherited scalar arithmetic now reaches the expected concrete result rather than failing the assertion.

AI assessment: Expected improvement (medium confidence). PR attribution: Likely caused by the PR.

Before / baseline evidence: At test_scalars.py:581 and :622 the baseline needed pyright ignores specifically for reportAssertTypeFailure. An assertion's target type is an expectation, not an observed baseline reveal; the evidence establishes only that baseline Pyright disagreed with those expected scalar-operation types.

After / PR evidence: The PR run reports both ignores as unnecessary. Thus the same assert_type checks no longer emit reportAssertTypeFailure, consistent with receiver-bound reinference selecting the concrete pandas scalar result. No new Any/Unknown or erased outer API is reported.

Why this matters: Stub consumers can rely on these scalar arithmetic expressions retaining the asserted pandas/stdlib scalar API without suppressing Pyright. Member access and subsequent operator checks on the concrete result remain available rather than being weakened to a gradual type.

Causal analysis:

The exact-head change detects inherited unannotated operators whose cached return is only NotImplementedType, then analyzes their body using the concrete bound receiver and call arguments. Pandas scalar/reflected arithmetic frequently depends on subclass dispatch and operator priority, so this can change the selected inferred result to the concrete type expected by assert_type. The only recorded diagnostics are unnecessary-ignore reports, which is positive evidence that checking became more precise rather than disappearing. Still, the concise diff does not print the assertion expressions or expected types, and the exact ecosystem checkout was not recovered; therefore the conclusion is medium-confidence and does not claim a runtime fix.

Uncertainty / next check: Inspect the exact recorded lines to name both expected result types. Positive controls should access a member unique to each asserted result; a negative control should pass an unsupported operand and still produce an operator error rather than Any/Unknown.

Full evidence and limitations: download mypy-primer-analysis-report from this analysis run.
The original raw primer comment is unchanged. Treat uncertainty and possible regressions as requests for human review.

@github-actions

Copy link
Copy Markdown
Contributor

Compared candidate 1523dd52ed9a4ee42802ed3a28fba4da44811c79 against its first parent 611afe615b8659a154aa9656ee5b8cfddf57cdac.

View the full release history charts with the base and PR results.

Execution time history

Execution time history

Peak memory history

Peak memory history

Download the chart artifact.

Type checker benchmark

🟢 No performance regressions detected.

Regression threshold: 20.0%
Statistic: median
Variance guard: >1.0s time and >100.0 MB memory

Package Checker Files checked Time Time delta Peak memory Memory delta Status
ansible pyright 583 15.443s -2.1% 1154.1 MB -3.4% 🟢 Pass
click pyright 17 1.847s +0.7% 358.2 MB -0.3% 🟢 Pass
homeassistant pyright 9850 106.521s -2.7% 6194.1 MB -0.3% 🟢 Pass
numpy pyright 356 35.254s -0.5% 1312.1 MB +6.7% 🟢 Pass
pandas pyright 1459 142.249s +6.9% 3514.3 MB -9.4% 🟢 Pass
pytest pyright 243 12.776s +1.0% 840.1 MB -1.2% 🟢 Pass
requests pyright 19 2.231s +0.1% 342.9 MB +0.0% 🟢 Pass
torch pyright 2400 130.366s -3.5% 4802.0 MB +1.7% 🟢 Pass
transformers pyright 2901 87.889s -8.1% 5276.9 MB +5.7% 🟢 Pass

Pyright stats

Package Parsed/bound Checked Find Read Tokenize Parse Imports Bind Check Cycles
ansible 988 583 0.033s 0.070s 0.333s 0.610s 0.190s 0.803s 12.963s 0.000s
click 111 17 0.000s 0.010s 0.067s 0.130s 0.023s 0.160s 1.240s 0.000s
homeassistant 11150 9850 0.537s 0.620s 2.413s 3.953s 1.727s 5.307s 90.693s 0.000s
numpy 606 356 0.010s 0.060s 0.373s 0.743s 0.263s 0.900s 32.253s 0.000s
pandas 1899 1459 0.070s 0.237s 1.230s 2.653s 0.280s 2.837s 131.720s 0.000s
pytest 545 243 0.020s 0.053s 0.373s 0.550s 0.107s 0.710s 10.510s 0.000s
requests 167 19 0.000s 0.017s 0.107s 0.213s 0.047s 0.240s 1.283s 0.000s
torch 3078 2400 0.177s 0.363s 1.933s 3.530s 0.583s 4.293s 119.380s 0.000s
transformers 3536 2901 0.127s 0.373s 1.653s 3.060s 0.813s 7.470s 78.663s 0.000s

@rchiodo

Copy link
Copy Markdown
Collaborator

Hmm, having a hard time getting the primer to pass. Going to try something different.

@crypt0lith

Copy link
Copy Markdown
Contributor

Rich Chiodo (@rchiodo) a question about review order, not this PR: since late September, outside PRs have merged in PR-number order, and nothing behind #11662 has merged while it's been in progress. That includes #11666 and #11691, which have been approved and mergeable for a while. Is the queue meant to wait on the PR at its head while that PR needs changes, or should approved PRs behind it be able to go in?

@rchiodo

Copy link
Copy Markdown
Collaborator

Rich Chiodo (Rich Chiodo (@rchiodo)) a question about review order, not this PR: since late September, outside PRs have merged in PR-number order, and nothing behind #11662 has merged while it's been in progress. That includes #11666 and #11691, which have been approved and mergeable for a while. Is the queue meant to wait on the PR at its head while that PR needs changes, or should approved PRs behind it be able to go in?

I'm trying to do them in order, but some fixes seem more important than others (like the hang one you were working on). This one in particular is stuck on me trying to fix the loss of type information when returning 'self' typed parameters.

So yes, I'm trying to do them from oldest to newest in case the older ones fix PRs that were added later.

@rchiodo

Copy link
Copy Markdown
Collaborator

Sorry closing this in favor of #11977. I think that fix was simpler than this one.

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.

Type narrowing not done correctly for bitwise operations with int and instance of subclass of enum.IntFlag

4 participants