Skip to content

Updating type hints for numpy arrays #5144

@chrishavlin

Description

@chrishavlin

There's a number of places where the codebase uses np.ndarray in type hints, these should be replaced by the more correct usage of numpy.typing.NDArray, creating this as a placeholder issue. See #5137 (review) for more info on using NDArray.

Possible files to update, may contain false positives when np.ndarray is used in a docstring (I haven't double checked them all):

  • yt/visualization/plot_window.py
  • yt/visualization/fixed_resolution_filters.py
  • yt/visualization/_handlers
  • yt/visualization/fixed_resolution.py
  • yt/visualization/plot_modifications.py
  • yt/_typing.py
  • yt/frontends/amrvac/io.py
  • yt/frontends/stream/misc.py
  • yt/frontends/ramses/io.py
  • yt/frontends/ramses/hilbert.py
  • yt/frontends/ramses/particle_handlers.py
  • yt/frontends/rockstar/data_structures.py
  • yt/frontends/artio/data_structures.py
  • yt/loaders.py
  • yt/utilities/io_handler.py
  • yt/geometry/grid_geometry_handler.py
  • yt/geometry/coordinates/coordinate_handler.py
  • yt/geometry/geometry_handler.py
  • yt/geometry/oct_geometry_handler.py
  • yt/testing.py -- should be fixed by update testing.integrate_kernel type hints #5137
  • yt/utilities/lib/cykdtree/plot.py : only used in docstrings

The results of the full grep&sed command to find these is shown below, including line numbers.

Detailed occurrences of `np.ndarray` in type hints

Here's a bunch of specific places where np.ndarray is used in type hints, found by running

$ grep -rn np.ndarray --include \*.py yt  > possible_ndarray_typehints.txt 
$ sed '/isinstance/d' possible_ndarray_typehints.txt 

on the main branch as of 2025-03-27 (commit 66af53c). Note that some of these are valid usages, but most are type hint usages that should be updated.

yt/visualization/plot_window.py:1396:    def sanitize_normal_vector(ds, normal) -> str | np.ndarray:
yt/visualization/fixed_resolution_filters.py:63:    def apply(self, buff: np.ndarray) -> np.ndarray:
yt/visualization/fixed_resolution_filters.py:66:    def __call__(self, buff: np.ndarray) -> np.ndarray:
yt/visualization/_handlers.py:294:    def get_norm(self, data: np.ndarray, *args, **kw) -> Normalize:
yt/visualization/tests/test_plotwindow.py:858:    # They should be returned as np.ndarrays, but the norm and orientation
yt/visualization/fixed_resolution.py:230:    def _apply_filters(self, buffer: np.ndarray) -> np.ndarray:
yt/visualization/plot_modifications.py:1060:        levels: np.ndarray | int
yt/_typing.py:15:DomainDimensions = tuple[int, ...] | list[int] | np.ndarray
yt/_typing.py:19:    tuple[np.ndarray, np.ndarray, np.ndarray],  # xyz
yt/_typing.py:20:    float | np.ndarray,  # hsml
yt/_typing.py:33:# np.ndarray[...] syntax is runtime-valid from numpy 1.22, we quote it until our minimal
yt/_typing.py:36:MaskT = Optional["np.ndarray[Any, np.dtype[np.bool_]]"]
yt/_typing.py:37:AlphaT = Optional["np.ndarray[Any, np.dtype[np.float64]]"]
yt/frontends/amrvac/io.py:98:        data : np.ndarray
yt/frontends/stream/misc.py:7:    cell_widths: list[np.ndarray],
yt/frontends/stream/misc.py:9:) -> list[np.ndarray]:
yt/frontends/ramses/io.py:36:    ds, conformal_time: np.ndarray
yt/frontends/ramses/io.py:45:    conformal_time : np.ndarray
yt/frontends/ramses/io.py:50:    physical_age : np.ndarray
yt/frontends/ramses/hilbert.py:52:    ijk: "np.ndarray[Any, np.dtype[np.int64]]", bit_length: int
yt/frontends/ramses/hilbert.py:53:) -> "np.ndarray[Any, np.dtype[np.float64]]":
yt/frontends/ramses/hilbert.py:73:    LE: Optional["np.ndarray[Any, np.dtype[np.float64]]"] = None,
yt/frontends/ramses/hilbert.py:77:    bound_keys: Optional["np.ndarray[Any, np.dtype[np.float64]]"] = None,
yt/frontends/ramses/hilbert.py:122:    X: "np.ndarray[Any, np.dtype[np.float64]]",
yt/frontends/ramses/hilbert.py:123:    bound_keys: "np.ndarray[Any, np.dtype[np.float64]]",
yt/frontends/ramses/particle_handlers.py:70:        dict[tuple[str, str], np.ndarray],
yt/frontends/ramses/particle_handlers.py:162:        self, field: tuple[str, str], data_dict: dict[tuple[str, str], np.ndarray]
yt/frontends/ramses/particle_handlers.py:174:        data_dict : dict[tuple[str, str], np.ndarray]
yt/frontends/ramses/particle_handlers.py:348:        self, field: tuple[str, str], data_dict: dict[tuple[str, str], np.ndarray]
yt/frontends/ramses/particle_handlers.py:496:        self, field: tuple[str, str], data_dict: dict[tuple[str, str], np.ndarray]
yt/frontends/rockstar/data_structures.py:24:    _Npart: "np.ndarray[Any, np.dtype[np.int64]]"
yt/frontends/rockstar/data_structures.py:52:    ) -> Optional["np.ndarray[Any, np.dtype[np.int64]]"]:
yt/frontends/artio/data_structures.py:342:        icoords: np.ndarray,
yt/frontends/artio/data_structures.py:343:        ires: np.ndarray,
yt/frontends/artio/data_structures.py:345:    ) -> tuple[np.ndarray, np.ndarray]:
yt/loaders.py:690:    data: Mapping[AnyFieldKey, np.ndarray | tuple[np.ndarray, str]],
yt/loaders.py:829:    pdata: dict[AnyFieldKey, np.ndarray | tuple[np.ndarray, str]] = {}
yt/loaders.py:1072:    octree_mask : np.ndarray[uint8_t]
yt/loaders.py:1371:    connectivity = list(always_iterable(connectivity, base_type=np.ndarray))
yt/loaders.py:1819:    bbox: np.ndarray | None = None,
yt/tests/test_funcs.py:57:        "list/tuple/np.ndarray/YTArray/YTQuantity, received "
yt/utilities/io_handler.py:99:    ) -> Mapping[FieldKey, np.ndarray]:
yt/utilities/io_handler.py:168:    ) -> dict[FieldKey, np.ndarray]:
yt/utilities/io_handler.py:169:        data: dict[FieldKey, list[np.ndarray]] = {}
yt/utilities/io_handler.py:199:        rv: dict[FieldKey, np.ndarray] = {}  # the return dictionary
yt/utilities/parallel_tools/parallel_analysis_interface.py:765:        #   np.ndarray
yt/utilities/lib/cykdtree/plot.py:24:        seg (list of np.ndarray): Line segments to plot defining box edges.
yt/utilities/lib/cykdtree/plot.py:25:        pts (np.ndarray, optional): Points contained by the kdtree. Defaults to
yt/utilities/lib/cykdtree/plot.py:133:    pts: np.ndarray, optional
yt/utilities/lib/cykdtree/__init__.py:9:        pts (np.ndarray of float64): (n,m) Array of n mD points.
yt/funcs.py:1221:            "list/tuple/np.ndarray/YTArray/YTQuantity, "
yt/geometry/grid_geometry_handler.py:450:        icoords: np.ndarray,
yt/geometry/grid_geometry_handler.py:451:        ires: np.ndarray,
yt/geometry/grid_geometry_handler.py:453:    ) -> tuple[np.ndarray, np.ndarray]:
yt/geometry/coordinates/coordinate_handler.py:161:    ) -> "np.ndarray[Any, np.dtype[np.float64]]": ...
yt/geometry/coordinates/coordinate_handler.py:176:        "np.ndarray[Any, np.dtype[np.float64]]", "np.ndarray[Any, np.dtype[np.bool_]]"
yt/geometry/geometry_handler.py:54:        icoords: np.ndarray,
yt/geometry/geometry_handler.py:55:        ires: np.ndarray,
yt/geometry/geometry_handler.py:57:    ) -> tuple[np.ndarray, np.ndarray]:
yt/geometry/oct_geometry_handler.py:122:        icoords: np.ndarray,
yt/geometry/oct_geometry_handler.py:123:        ires: np.ndarray,
yt/geometry/oct_geometry_handler.py:125:    ) -> tuple[np.ndarray, np.ndarray]:
yt/testing.py:94:    x: float | np.ndarray,
yt/testing.py:95:) -> np.ndarray:
yt/testing.py:157:    pos3_i0: np.ndarray,
yt/testing.py:158:    pos3_i1: np.ndarray,
yt/testing.py:160:    periods: np.ndarray = _zeroperiods,
yt/testing.py:161:) -> np.ndarray:
yt/testing.py:825:    e1hat: np.ndarray = _xhat,
yt/testing.py:826:    e2hat: np.ndarray = _yhat,
yt/testing.py:827:    e3hat: np.ndarray = _zhat,
yt/testing.py:828:    offsets: np.ndarray = _floathalves,
yt/testing.py:831:    bbox: np.ndarray | None = None,
yt/testing.py:832:    recenter: np.ndarray | None = None,
yt/testing.py:863:    bbox: if np.ndarray, shape is (2, 3)
yt/testing.py:929:    data: Mapping[AnyFieldKey, tuple[np.ndarray, str]] = {
yt/testing.py:956:    bbox: np.ndarray,
yt/testing.py:1000:    data: Mapping[AnyFieldKey, tuple[np.ndarray, str]] = {

Metadata

Metadata

Labels

new contributor friendlyGood for new contributors!refactorimprove readability, maintainability, modularity

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions