Skip to content

SymPy: operator stubs incorrectly return only NotImplementedType #407

Open

Description

Summary

Several SymPy operator stubs declare NotImplementedType as their only return type. However, these methods return concrete values for supported operands.

The declarations are present on the current main branch:

Runtime Reproducer

Tested with SymPy 1.14.0:

from sympy import QQ, ZZ
from sympy.polys.matrices.domainmatrix import DM
from sympy.polys.rings import ring

matrix = DM([[1]], QQ)
_, x, y = ring("x,y", ZZ)

operations = [
    ("matrix + matrix", matrix + matrix),
    ("matrix - matrix", matrix - matrix),
    ("matrix / 2", matrix / 2),
    ("x * y", x * y),
    ("2 * x", 2 * x),
    ("2 - x", 2 - x),
    ("divmod(2, x)", divmod(2, x)),
    ("2 % x", 2 % x),
    ("0 / x", 0 / x),
]

for expression, result in operations:
    print(f"{expression}: {type(result).__name__}")

Output:

matrix + matrix: DomainMatrix
matrix - matrix: DomainMatrix
matrix / 2: DomainMatrix
x * y: PolyElement
2 * x: PolyElement
2 - x: PolyElement
divmod(2, x): tuple
2 % x: PolyElement
0 / x: PolyElement

Affected Declarations

These methods currently return only NotImplementedType in the stubs:

  • DomainMatrix: __add__ (L64), __sub__ (L65), __truediv__ (L79).
  • PolyElement: __mul__ (L131), __rmul__ (L132), __rsub__ (L130), __rdivmod__ (L136), __rmod__ (L138), __rtruediv__ (L140).

Expected Behavior

Include the concrete return types. Retain NotImplementedType where the method can reject unsupported operands.

For example, DomainMatrix.__add__ needs a matrix return type, and PolyElement.__mul__ needs a polynomial return type.

This surfaced while testing a change to Pyrefly's handling of NotImplemented. A checker that interprets sentinel-only returns as definitive rejection can report unsupported-operation errors for these valid expressions.

Activity

  1. Avasam commented on Nov 1, 2026

    @Avasam
    Contributor

    I seriously recommend just deleting these stubs from your pylance installation and using sympy's inline types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions