Skip to content

Conversation

@maverickcodex18
Copy link
Contributor

@maverickcodex18 maverickcodex18 commented Dec 6, 2025

Walkthrough: Updating NumPy Type Hints

I have updated the type hints in yt to use numpy.typing.NDArray (aliased as npt.NDArray) instead of np.ndarray. This change improves type checking precision and adheres to modern NumPy typing standards. The alias of numpy.typing is updated from npt to nptesting in testing.py.

Changes Overview

The following files were modified to use import numpy.typing as nptype and nptype.NDArray:

yt Core and Utilities

  • yt/utilities/io_handler.py
  • yt/loaders.py

yt/visualization

  • yt/visualization/plot_window.py
  • yt/visualization/fixed_resolution.py
  • yt/visualization/plot_modifications.py

yt/frontends

  • yt/frontends/rockstar/data_structures.py
  • yt/frontends/artio/data_structures.py
  • yt/frontends/ramses/io.py
  • yt/frontends/ramses/hilbert.py
  • yt/frontends/ramses/particle_handlers.py
  • yt/frontends/stream/misc.py

Verification

I verified the changes by:

  1. Confirming that import numpy.typing as npt is replaced by import numpy.typing as nptype.
  2. Confirming that usages of npt.NDArray are replaced by nptype.NDArray.
  3. Ensuring no conflicts with numpy.testing as npt.

Files Checked But Not Modified

I inspected the following files and found they were either already compliant or only used np.ndarray in docstrings:

  • yt/visualization/fixed_resolution_filters.py (Already compliant)
  • yt/visualization/_handlers.py (Already compliant)
  • yt/geometry/geometry_handler.py (Already compliant)
  • yt/frontends/amrvac/io.py (Docstrings only)
  • yt/utilities/lib/cykdtree/plot.py (Docstrings only)

Excluded File where np.ndarray is present

  • yt/testing.py (Updated helper functions like fake_random_sph_ds, integrate_kernel, cubicspline_python, etc.)

Fixes: #5144

@welcome
Copy link

welcome bot commented Dec 6, 2025

Hi! Welcome, and thanks for opening this pull request. We have some guidelines for new pull requests, and soon you'll hear back about the results of our tests and continuous integration checks. Thank you for your contribution!

@maverickcodex18 maverickcodex18 changed the title Refactor: replace np.ndarray with nptype.NDArray in type hints Refactor: replace np.ndarray with nptype.NDArray in type hints Fixes #5144 Dec 6, 2025
@maverickcodex18 maverickcodex18 deleted the fixIssue5144 branch December 6, 2025 21:39
@maverickcodex18 maverickcodex18 restored the fixIssue5144 branch December 6, 2025 21:40
@maverickcodex18 maverickcodex18 changed the title Refactor: replace np.ndarray with nptype.NDArray in type hints Fixes #5144 Refactor: replace np.ndarray with nptype.NDArray in type hints Dec 6, 2025
@maverickcodex18
Copy link
Contributor Author

Fix Mypy Type Errors

Objective: Resolve type errors reported by mypy, specifically "Bad number of arguments for type alias" and "Incompatible type" errors.

Changes

A. Fix npt.NDArray Type Arguments

The error Bad number of arguments for type alias, expected 1, given 2 was occurring because npt.NDArray was being used with two arguments (e.g., npt.NDArray[Any, np.dtype[Type]]). numpy.typing.NDArray expects a single type argument representing the scalar type.
Files Corrected:

  • yt/geometry/coordinates/coordinate_handler.py: Corrected pixelize return type.
  • yt/frontends/ramses/hilbert.py: Corrected signatures for hilbert3d, get_intersecting_cpus, and get_cpu_list_cuboid.
  • yt/frontends/rockstar/data_structures.py: Corrected _Npart and _read_member.
    Fix Pattern:
# Before
ijk: "nptype.NDArray[Any, np.dtype[np.int64]]"
# After
ijk: "nptype.NDArray[np.int64]"

@chrishavlin chrishavlin self-requested a review December 8, 2025 15:15
Copy link
Contributor

@chrishavlin chrishavlin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here's a quick note. also, i will not have time to review in detail for a few days.

yt/loaders.py Outdated
from urllib.parse import urlsplit

import numpy as np
import numpy.typing as nptype
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's use import numpy.typing as npt everywhere. You've got a mix of imports across files, please just stick to import numpy.typing as npt.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

npt was already in use as an alias at some files , so I thought nptype would be better choice to avoid any issues .

Also can you help me add label to the pr , I don't know how to add it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you point out a spot where it's being used as an alias? I don't see it in this file or the other couple I just checked. npt is preferred. i'll add the label when I get to doing a full review.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alias nptype was chosen to avoid conflicts with numpy.testing, which is often imported as npt.

it's true that we use npt for both namespaces, however I don't think they are ever imported in the same module (we don't have type hints in tests, and we shouldn't be using numpy.typing anywhere else), so, even if the meaning of npt is context-dependent, I don't think it's actually ambiguous.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @neutrinoceros
In testing.py , We are importing both numpy.typing and numpy.testing , otherwise everything looks good on using npt as alias.
Should I stick to npt alias for numpy.typing and revert type hints changes in 'testing.py' ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, you mean this section?

yt/yt/testing.py

Lines 1880 to 1897 in 2973f4d

def _deprecated_numpy_testing_reexport(func):
import numpy.testing as npt
npt_func = getattr(npt, func.__name__)
@wraps(npt_func)
def retf(*args, **kwargs):
__tracebackhide__ = True # Hide traceback for pytest
issue_deprecation_warning(
f"yt.testing.{func.__name__} is a pure re-export of "
f"numpy.testing.{func.__name__}, it will stop working in the future. "
"Please import this function directly from numpy instead.",
since="4.2",
stacklevel=3,
)
return npt_func(*args, **kwargs)
return retf

ya, that's an unfortunate re-definition of npt in that function... I'd say (1) keep the import numpy.typing as npt at the top of the file and revert your changes and (2) adjust that testing import for clarity:

    import numpy.testing as nptesting

    npt_func = getattr(nptesting, func.__name__)

Looks like there are a couple other minor spots where we do import numpy.testing as npt, but not in any of the files you're touching, so I'll submit a separate PR to fix those locations so we reserve npt for numpy.typing

Thanks! and FYI I'm hoping to take a detailed look this afternoon or tomorrow.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still need to update this import

Suggested change
import numpy.typing as nptype
import numpy.typing as npt

@chrishavlin chrishavlin added enhancement Making something better refactor improve readability, maintainability, modularity labels Dec 11, 2025
@chrishavlin chrishavlin mentioned this pull request Dec 11, 2025
2 tasks
Copy link
Contributor

@chrishavlin chrishavlin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks pretty good! most of my comments are just a request to change all your imports to import numpy.typing as npt -- I didn't go through all the usages, so you'll need to update everywhere you've used nptype.

Also, there are a couple spots where I want to double check the dtype specification -- I will do that though since it will require a bit more familiarity with yt than I'd expect from a new contributor. Thanks for the work so far!

@chrishavlin chrishavlin added this to the 4.5.0 milestone Dec 12, 2025
@maverickcodex18
Copy link
Contributor Author

maverickcodex18 commented Dec 12, 2025

Hi @chrishavlin , I have already made changes in new PR.
changing nptype to npt
reverting changes in testing.py
changing alias of numpy.testing to nptesting and npt_func = getattr(nptesting, func.__name__)

Please take a look in the new PR

Copy link
Contributor

@chrishavlin chrishavlin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great! just one change that you missed

@maverickcodex18 maverickcodex18 changed the title Refactor: replace np.ndarray with nptype.NDArray in type hints Refactor: replace np.ndarray with npt.NDArray in type hints Dec 16, 2025
chrishavlin
chrishavlin previously approved these changes Dec 19, 2025
Copy link
Contributor

@chrishavlin chrishavlin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! I'll try to drum up a second review. Thanks for the work!

ijk: "np.ndarray[Any, np.dtype[np.int64]]", bit_length: int
) -> "np.ndarray[Any, np.dtype[np.float64]]":
ijk: "npt.NDArray[np.int64]", bit_length: int
) -> "npt.NDArray[np.int64]":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cphyc mind double checking my fix here? the hilbert index arrays will always be int arrays from what I can see.

(also if you're up for reviewing the rest of the PR it'd be welcome!)

ds,
region: YTRegion,
LE: Optional["np.ndarray[Any, np.dtype[np.float64]]"] = None,
LE: Optional["npt.NDArray[np.float64]"] = None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh! nothing to change here, but I just remembered: the Any in the original np.ndarray[Any, np.dtype[np.float64]] is a reference to the array shape. So the update here is equivalent as npt.NDArray[dtype] is a type alias to np.ndarray[tuple[Any,...], dtype]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrishavlin
Copy link
Contributor

@maverickcodex18 just FYI I just pushed changes to your branch to fix the failing pre-commit checks (with your updates there were some lingering unused imports of Any). Nothing for you to do, but if the second reviewer suggests changes you'll want to make sure you pull in my changes locally to avoid merge conflicts.

Thanks for you work! Hopefully we'll get a second review soon (I'll send some messages around).

@maverickcodex18
Copy link
Contributor Author

@maverickcodex18 just FYI I just pushed changes to your branch to fix the failing pre-commit checks (with your updates there were some lingering unused imports of Any). Nothing for you to do, but if the second reviewer suggests changes you'll want to make sure you pull in my changes locally to avoid merge conflicts.

Thanks for you work! Hopefully we'll get a second review soon (I'll send some messages around).

Thankyou @chrishavlin

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

Labels

enhancement Making something better refactor improve readability, maintainability, modularity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Updating type hints for numpy arrays

3 participants