Thanks to visit codestin.com
Credit goes to github.com

Skip to content

TYP: excessively vague return types from np.dtype and np.ndarray.dtype in numpy 2.2.0 #27945

@neutrinoceros

Description

@neutrinoceros

Describe the issue:

While upgrading gpgi's CI to numpy 2.2.0, I got a new typechecking error that seems spurious to me: an array's dtype appears to not have a defined type (as seen by mypy).
No error is raised with numpy 2.1.3.

Reproduce the code example:

import numpy as np

def get_float_dtype(reference: np.ndarray) -> np.dtype:
    dt = reference.dtype
    if dt.kind != "f":
        raise ValueError(f"Invalid data type {dt} (expected a float dtype)")
    return np.dtype(dt)
    # also tried with
    return dt

Error message:

gpgi.py:7: error: Returning Any from function declared to return "dtype[Any]"  [no-any-return]
Found 1 error in 1 file (checked 1 source file)

Python and NumPy Versions:

2.2.0
3.13.1 (main, Dec  4 2024, 16:25:14) [Clang 16.0.0 (clang-1600.0.26.4)]

Type-checker version and settings:

mypy==1.13.0

the --warn-return-any flag is needed to reproduce

Additional typing packages.

Included with mypy, but not used here (as far as I'm aware)

typing-extensions==4.12.2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions