Skip to content

optimize.newton rejects functions that return an array #1518

@jorenham

Description

@jorenham

Discussed in #1517

Originally posted by wilgri April 7, 2026
Hello,

I'm using scipy-stubs and I'm new to optype. The following call of newton is flagged by basedpyright in strict mode:

def func(x: ArrayND[np.float64]) -> ArrayND[np.float64]:
    return -2 * x + 1

newton(func, x0=np.zeros((2, 4), dtype=float))

I gives

Type "(x: ArrayND[float64, AnyShape]) -> ArrayND[float64, AnyShape]" is not assignable to type "_Fun1D[ToFloat]"
   Function return type "ArrayND[float64, AnyShape]" is incompatible with type "ToFloat"

Looking at newton's doc and in practice, func accepts ndarray of floats. What am I doing wrong ?

Looking at scipy.optimize.newton typing, I see that func can be a generic callable _Fun1D with a type variable named _ToFloatT . I'm not sure to understand it entirely but it seems that basedpyright does not find an appropriate overload and defaults _ToFloatT to ToFloat which is incompatible with ArrayND[np.float64]. Is my understanding correct ? In addition I don't understand why arguments are onp.Array1D[np.float64] instead of ArrayND ? I feel like I'm missing something.

How would you type this basic example ?

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions